Sub topic 1
What Inline Completions Are and How Ghost Text Works
Prerequisites
Assume you can edit code in an IDE, read basic Python, and understand that a large language model predicts likely text from context. You should also recognize a version-control diff.
Inline Completions and Ghost Text
An inline completion is text an editor suggests at the cursor. The suggestion appears as ghost text: faint text that is not yet part of the document. Pressing an accept key inserts it. Ignoring or rejecting it leaves the document unchanged.
The editor usually asks for a completion after a pause, a trigger character, or an explicit command. The model should generate only text that fits the current location. It may suggest one token, a line, or several lines.
Building the Request
The editor builds context from:
- The prefix, which is the document text before the cursor.
- The suffix, which is the text after the cursor.
- Related context, such as the current function, imports, nearby symbols, or an open related file.
The suffix matters when the cursor is inside existing code. Without it, the model may generate a closing bracket or statement that conflicts with text already present.
def build_fim_prompt(prefix: str, suffix: str) -> str:
# Different models use different sentinel tokens.
return f"<fim_prefix>{prefix}<fim_suffix>{suffix}<fim_middle>"
document = "def add(a, b):\n return \n\nprint(add(1, 2))\n"
cursor = document.index("return ") + len("return ")
prompt = build_fim_prompt(document[:cursor], document[cursor:])
print(prompt)Fill-in-the-Middle Prompting
Fill-in-the-middle, or FIM, prompting places both the prefix and suffix in the prompt. The model generates the missing middle. This matches code completion better than ordinary left-to-right generation because the editor already knows what follows the cursor.
The sentinel tokens in the example are model-specific. A client must use the format expected by its model.
Triggering, Debouncing, and Cancelling Requests
An editor should not send a request for every keystroke. Debouncing waits briefly after typing stops. If another keystroke occurs, the timer resets. Cancellation stops an older request when the document or cursor changes. Together, these controls reduce wasted work and prevent stale suggestions from appearing.
Exercise
Why does a completion request include the suffix?
Show answer
The suffix tells the model what already follows the cursor, helping it generate text that fits the surrounding code.