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 (LLM) predicts text from a prompt, and that you can read short Python or TypeScript snippets. Later chapters build on the ideas introduced here.
What an inline completion is
An inline completion is a suggestion that an editor shows at your cursor while you type. The suggestion extends or fills in the code around the cursor. You can accept it with one key, usually Tab, or ignore it by typing something else.
Editors draw the suggestion as ghost text. Ghost text is dimmed, read-only text rendered after the cursor. It is not part of the file. It becomes real text only when you accept it.
This differs from a classic completion popup. A popup lists names the editor knows from the language, such as methods on an object. Ghost text can suggest a whole line, a block, or a function body, because a model generates it from the surrounding code.
| Feature | Completion popup | Inline completion |
|---|---|---|
| Source | Language analysis and symbol tables | Language model |
| Typical size | One identifier | A token, a line, or a block |
| Display | List of choices | Ghost text at the cursor |
| Accept | Select an item, then Enter | Tab |
The request and response cycle
Each suggestion comes from one request to a model. The editor extension collects the text before the cursor (the prefix) and the text after it (the suffix). It may add context from other files. It sends this to a backend, which returns a candidate string. The extension then shows the string as ghost text.
The same cycle repeats after almost every keystroke, so each step has to be fast. Later chapters cover how requests are built, delayed, and cancelled.
A worked example
Suppose you type this Python function and stop with the cursor after return :
def add(a, b):
return
print(add(1, 2))The prefix is everything up to the cursor. The suffix is everything after it, which here begins with a blank line and the print call. A model that sees both can propose a + b. The editor displays it dimmed:
def add(a, b):
return a + b
print(add(1, 2))Only the a + b portion is ghost text. If you press Tab, the editor inserts it at the cursor. If you type a yourself, the editor typically checks whether your character matches the start of the suggestion. If it matches, the ghost text stays and shrinks. If not, the suggestion is dropped.
How ghost text is shown
The editor treats ghost text as a decoration, not as document content. Three consequences follow:
- Your file, undo history, and version control are unchanged until you accept.
- Save, format, and lint actions never see the suggestion.
- The suggestion is tied to a specific cursor position. If the cursor moves, the suggestion is stale and the editor hides it.
Editors usually expose commands for it, such as accept, dismiss, and show the next or previous alternative. Some also let you accept one word or one line at a time. That is covered in a later chapter.
Common mistakes
- Treating ghost text as inserted text. Until you accept, nothing has changed in the file.
- Assuming the model sees your whole project. It sees only the context the extension chooses to send, which is limited by the model's input size.
- Expecting every pause to produce a suggestion. Editors skip requests when the context looks unhelpful, such as inside a comment you are still writing, or when a request is cancelled because you kept typing.
Exercise
You type return inside add, and the editor shows a + b in dimmed text. What has changed in the file on disk and in the undo history, and what must you do to make the text real?
Show answer
Nothing has changed in the file or the undo history, because ghost text is a decoration drawn by the editor. You must accept the suggestion, usually with Tab, which inserts the text at the cursor as a normal edit.
Exercise
Name the two pieces of text an extension always collects around the cursor when it builds a completion request.
Show answer
The prefix (text before the cursor) and the suffix (text after the cursor). Related context from other files may be added as well.