Level 5Lesson 39⏱️ 75 min

Inline Code Completion Mastery

Ghost text isn't magic — it's pattern matching on context. Learn to write code that AI can complete brilliantly.

How Inline Completion Works

When you pause typing, the tool sends your file content (up to the context limit) to the model. The model predicts what comes next based on everything it sees. Your job is to give it the right signals.

What drives better completions:
  • Good function namesgetUserByEmail() predicts better than getUser()
  • Type annotations — TypeScript/Python types dramatically improve suggestions
  • Comments before code — A comment describing what you want is the strongest signal
  • Consistent patterns — Existing code in the file acts as few-shot examples
  • Open relevant files — In Cursor/Copilot, open files that share the pattern you want

The Comment-First Technique

Write your intent as a comment, then let AI write the code. This is the single highest-leverage technique for inline completion.

// BAD: ambiguous, poor completion
function process(data) {
  // ...
}

// GOOD: descriptive name + comment = excellent completion
// Validates email format and checks if domain is in the blocked list
// Returns { valid: boolean, reason: string }
function validateEmailAddress(email: string, blockedDomains: string[]) {
  // AI now knows exactly what to write

// EVEN BETTER: JSDoc comment triggers full implementation
/**
 * Calculates compound interest over a given period.
 * @param principal - Initial investment amount in USD
 * @param rate - Annual interest rate as decimal (e.g. 0.05 for 5%)
 * @param years - Investment duration in years
 * @param compoundsPerYear - Times compounded per year (12 for monthly)
 * @returns Final amount after compound interest
 */
function calculateCompoundInterest(
  principal: number,
  rate: number,
  years: number,
  compoundsPerYear: number = 12
): number {
  // Accept the AI's completion here
}

Keyboard Shortcuts (VS Code + Copilot)

Tab              → Accept full suggestion
Escape           → Dismiss suggestion
Alt+]            → Next suggestion (cycle through alternatives)
Alt+[            → Previous suggestion
Ctrl+Enter       → Open Copilot panel (see 10 alternatives)
Alt+            → Trigger suggestion manually (if it didn't appear)

// In Cursor:
Tab              → Accept full suggestion
Cmd+Right        → Accept one word at a time (very useful!)
Escape           → Dismiss
Pro tip — partial acceptance: In Cursor and newer Copilot versions, use Cmd+Right to accept just the next word. This lets you accept the structure but change specifics — much faster than retyping.

Writing AI-Readable Code

// Pattern 1: Show one example, let AI complete the rest
const STATUS_MESSAGES = {
  200: 'OK',
  201: 'Created',
  // Accept completion — AI will fill in 400, 401, 403, 404, 500...
}

// Pattern 2: Consistent naming = consistent completions
// After writing getUserById, getProductById, getOrderById...
// getInvoice| → AI autocompletes "ById" and the full implementation

// Pattern 3: Start the body, not just the signature
async function fetchUserProfile(userId: string) {
  const response = await fetch(
  // AI now knows it's an API call and completes the full fetch chain

// Pattern 4: Use types to constrain completions
type PaymentStatus = 'pending' | 'processing' | 'completed' | 'failed'
function handlePaymentStatus(status: PaymentStatus) {
  switch (status) {
    // AI generates all 4 cases with appropriate handling
  }
}

When to NOT Trust Inline Completion

Always verify AI-completed code for:
  • Security-sensitive logic — auth checks, input sanitisation, SQL queries
  • Business logic — pricing calculations, permission rules, financial operations
  • Edge cases — null checks, empty arrays, timezone handling, locale formatting
  • Library versions — AI may suggest APIs from older library versions
  • Your internal APIs — AI doesn't know your custom SDK unless you have it open

Hands-on: Complete a CRUD Module

Challenge: Write a TypeScript CRUD service using only inline completion.

  1. Create a new file user-service.ts
  2. Write a JSDoc comment for createUser(email, name, role) — accept the completion
  3. Write only the function signature for getUserById — see what you get
  4. Write the first CRUD function fully, then write just signatures for the rest — the AI should follow the pattern
  5. Add error handling by typing // throw custom error if and see what it suggests
Lesson 39 Quick Reference
Comment-first

Write intent as a comment, then let AI write the code — strongest completion signal

Type annotations

TypeScript/Python types dramatically improve suggestion accuracy

Tab

Accept full suggestion | Alt+] cycles alternatives | Ctrl+Enter shows 10 options

Cmd+Right

Accept one word at a time (Cursor) — great for partial acceptance

Show one, get many

Write one item in a pattern (array, switch, object) — AI fills the rest

Always verify

Security, business logic, edge cases — never blindly accept these