Sub topic 1
What inline completions are and how ghost text works
Prerequisites
This chapter assumes you can edit code in an IDE such as VS Code or a JetBrains IDE. It also assumes you know that a large language model predicts text from a prompt, and that you can read short Python or TypeScript snippets. Later chapters build on the model described here, so the goal is a working picture of the whole loop rather than every detail.
What an inline completion is
An inline completion is a suggestion that an editor assistant shows at the cursor while you type. The suggestion is the text the assistant predicts you are about to write. It can be a few characters, the rest of a line, or a whole block of code.
The editor displays the suggestion as ghost text. Ghost text is dimmed, usually gray, text drawn after the cursor. It is not part of your file. The file on disk and the editor's text buffer do not change until you accept the suggestion.
Consider this Python file with the cursor at the end of the second line:
def add(a, b):
return The assistant might show a + b in gray after return . If you press Tab, the editor inserts a + b into the buffer. If you keep typing something else, the ghost text disappears or updates.
The request and response loop
Every inline completion follows the same cycle. The pieces of this cycle are the subject of the next chapters.
- You type, move the cursor, or pause. The editor decides whether this event should trigger a request.
- The extension collects context. This always includes the text before the cursor and the text after it, and it may include other files and recent edits.
- The extension builds a prompt and sends it to a model, either a remote service or a local one.
- The model returns text.
- The extension cleans the text, for example by removing duplicated brackets, and hands it to the editor.
- The editor renders it as ghost text.
- You accept it, reject it, or ignore it.
The diagram shows why latency matters. Every step between the keystroke and the ghost text happens while you are waiting, and you are still typing. A suggestion that arrives after you have moved on is useless, so the system must also be able to throw away stale work. That is covered in the chapter on triggering, debouncing and cancellation.
What the model sees
The central input to the request is the document split at the cursor position into two parts.
- The prefix is all the text before the cursor.
- The suffix is all the text after the cursor.
The model's job is to produce the text that fits between them. Older completion systems used only the prefix and predicted the continuation, as a language model normally does. That works when you are writing at the end of a file. It fails when you edit in the middle of existing code, because the continuation has to connect to what follows. Modern assistants send both parts. The chapter on fill-in-the-middle prompting explains how.
You can see the split with a few lines of Python:
document = "def add(a, b):\n return \n\nprint(add(1, 2))\n"
cursor = document.index("return ") + len("return ")
prefix = document[:cursor]
suffix = document[cursor:]
print("PREFIX:", repr(prefix))
print("SUFFIX:", repr(suffix))This prints the prefix 'def add(a, b):\n return ' and the suffix '\n\nprint(add(1, 2))\n'. A good completion here is a + b. It makes sense after return and it leaves the following blank line and print call intact.
Real requests are bigger than this example. Models have a limited context window, which is the maximum amount of text they can read in one request. For a large file the extension cannot send everything. It keeps the text closest to the cursor and drops the far ends, and it may add snippets from other open files. The next chapter covers how that selection works.
How ghost text works inside the editor
Ghost text is a display feature, not a text edit. The editor keeps a pending suggestion next to the real buffer and draws it with a different style. This separation has several consequences.
- The buffer stays clean. Undo history, version control and language servers see only your real text until you accept.
- Typing can match the suggestion. If the ghost text is
a + band you typea, most editors keep the suggestion and show the remaining+ b, without a new request. If you type a character that does not match, the suggestion is dismissed. - The suggestion is anchored to a position. If you move the cursor, the suggestion no longer applies and the editor hides it.
- Only one suggestion is usually visible. Some editors let you cycle through alternatives, but a single ghost text is the default.
An extension supplies the suggestion as an item with two parts: the text to insert and the range of the document it replaces. For a pure insertion at the cursor, the range is empty and starts and ends at the cursor position. The range can also cover existing text, which lets a suggestion replace something you already typed. Later chapters use this when handling auto-inserted brackets and when showing next edit suggestions.
Inline completion compared with other assistant features
Inline completion is one of several ways to use a model in an editor. Knowing the differences helps you pick the right one and understand the design constraints.
| Feature | Trigger | Output | Typical latency target |
|---|---|---|---|
| Inline completion | Typing or pausing | Text inserted at the cursor | Well under a second |
| Chat | You send a message | Conversation and code blocks | Seconds, streamed |
| Agent edits | You describe a task | Changes across files | Seconds to minutes |
| Next edit suggestion | An edit you just made | A change elsewhere, shown as a diff | Under a second or two |
Inline completion is the only one that runs without you asking. It must be fast, cheap per request, and quiet. A feature that makes one request per keystroke on a large model would be slow and expensive, so assistants usually use smaller, faster models for completions and save larger models for chat.
Design constraints that follow
The loop above produces several constraints. Each is expanded in a later chapter.
- Latency budget. The suggestion should appear before you have typed the next few characters. This limits model size and prompt length.
- Relevance. The prompt has a fixed budget, so the extension must choose which context to include.
- Fit. Raw model output often contains extra text such as a repeated closing bracket. It needs trimming before display.
- Control. You must be able to accept all of a suggestion, part of it, or none of it, with simple keys.
- Trust. Suggestions are predictions. They can be wrong, insecure, or copied from licensed code, so you must review them.
A minimal view of the data
Here is the shape of a completion result in TypeScript terms. This is a simplified model, not the full VS Code API:
interface Position {
line: number;
character: number;
}
interface Range {
start: Position;
end: Position;
}
interface CompletionItem {
insertText: string; // the ghost text
range: Range; // the part of the document it replaces
}
const cursor: Position = { line: 1, character: 11 };
const item: CompletionItem = {
insertText: "a + b",
range: { start: cursor, end: cursor }, // empty range means pure insertion
};
console.log(item);When range.start equals range.end, the suggestion only inserts. When the range spans text, accepting replaces that text. This one structure covers both plain completions and more advanced cases.
Common misunderstandings
- "The assistant edits my file as I type." It does not. Nothing changes in the buffer until you accept.
- "The model sees my whole project." It sees only what the extension chooses to put in the prompt, bounded by the context window.
- "A suggestion that looks right is right." Models generate plausible text. Plausible and correct are different, and the later chapter on failure modes covers how to review.
- "Ghost text appears at every keystroke." Extensions decide when to ask. They usually wait for a pause and skip requests in places where a suggestion would be unhelpful.
Summary of the model
An inline completion is a prediction of the text at the cursor. The editor sends the text before and after the cursor, plus related context, to a model. The model returns text. The extension cleans it up, and the editor shows it as ghost text that you accept with a key press. The rest of this tutorial looks at each stage of that loop and at next edit suggestions, which extend the idea from the cursor to other places in the file.
Exercise
You type return in a Python function and gray text a + b appears after the cursor. You press the Escape key. What happens to your file, and why?
Show answer
Nothing changes in the file. Ghost text is a display of a pending suggestion, not part of the text buffer. Escape dismisses the suggestion, so the buffer still contains only what you typed.
Exercise
A request is made with the cursor in the middle of a function, with code after it. Why should the request include the suffix and not only the prefix?
Show answer
The generated text has to connect to the code that follows the cursor. With only the prefix, the model would continue as if it were at the end of the file and could repeat or contradict code that already exists after the cursor.
Exercise
A CompletionItem has insertText "a + b" and a range whose start and end are the same position. What does accepting it do?
Show answer
It inserts "a + b" at that position without removing any existing text, because an empty range means a pure insertion.