Sub topic 6
Accepting, rejecting and partially accepting suggestions
This chapter assumes you know how ghost text appears at the cursor and how a request is built from the prefix and suffix. It also assumes you can read short Python and JSON. It covers what happens after a suggestion is on screen: how the user takes it, ignores it or takes only part of it, and what the editor and the backend do in response.
Why the accept step needs design
A suggestion costs the user attention before it costs any keystrokes. The user must read it, judge it and decide. Accept controls are therefore built around three goals:
- Accepting a good suggestion should take one keystroke.
- Rejecting a bad one should take no extra effort, ideally none at all.
- A suggestion that is mostly right should not force an all-or-nothing choice.
Partial acceptance exists for the third goal. Without it, a user facing a five-line suggestion with one wrong identifier must either accept everything and fix it, or reject everything and type it by hand.
The life of a suggestion
A suggestion moves through a small set of states. Each state change is also a point where a client can record a signal.
The Stale state is easy to overlook. If the suggestion is total = sum(prices) and the user types tot, the editor can keep the ghost text and show only the remainder. If the user types x, the suggestion no longer matches and is dropped. The editor decides this by comparing what was typed with the start of the suggestion text.
Accepting
Most editors use Tab for a full accept. The editor inserts the suggestion text at the cursor and moves the cursor to the end of the inserted text. Two details matter in practice:
- The insertion is usually one undo step, so a single undo removes the whole accepted block. Verify this in your editor before relying on it.
- Tab is overloaded. It also indents and accepts normal autocomplete entries. Editors resolve the conflict with context keys, such as "a suggestion is visible", so Tab means accept only while ghost text shows.
When the ghost text overlaps text the editor already inserted, such as an auto-closed bracket, the provider should have trimmed the overlap earlier (see the post-processing chapter). If it did not, an accept can leave a doubled ). That is a post-processing bug that users experience as an accept bug.
Rejecting
There are two kinds of rejection, and they look different to the backend.
| Action | Typical trigger | What the client knows |
|---|---|---|
| Explicit dismiss | Esc, or a "hide suggestion" command | The user saw it and said no |
| Implicit reject | The user keeps typing and diverges from the text | The user probably saw it, or never read it |
| Cancelled before display | A newer keystroke cancelled the request | The user never saw it |
Treat these separately. Counting the third row as a rejection would lower the measured quality of the model for reasons unrelated to quality. The evaluation chapter returns to this.
Some editors also let users cycle through alternatives. In VS Code the defaults are Alt+] and Alt+[, but this depends on the extension supplying more than one item. Cycling is a form of rejection with a follow-up: the user declined the first candidate and asked for another.
Partial acceptance
Partial acceptance inserts a prefix of the suggestion and keeps the rest as ghost text. The common units are:
- Next word. In VS Code this is Ctrl+Right (Cmd+Right on macOS) while a suggestion shows. JetBrains IDEs offer a similar word-wise accept.
- Next line. VS Code has a command for this,
editor.action.inlineSuggest.acceptNextLine. It may have no default key in your version.
Word-wise acceptance is how a user keeps compute_total( from a suggestion and then types a different argument. Line-wise acceptance suits multi-line bodies, where the first two lines are correct and the third is not.
The logic is a split of the suggestion into an accepted part and a remainder. This is a simplified version of what editors do:
import re
# One "word": optional leading whitespace, then a run of word characters
# or a single punctuation character.
WORD = re.compile(r"\s*(?:\w+|[^\w\s])")
def split_accept(suggestion: str, unit: str = "word") -> tuple[str, str]:
"""Return (accepted, remainder) for a partial accept."""
if unit == "line":
newline = suggestion.find("\n")
end = len(suggestion) if newline == -1 else newline + 1
elif unit == "word":
match = WORD.match(suggestion)
end = match.end() if match else len(suggestion)
else:
raise ValueError(f"unknown unit: {unit}")
return suggestion[:end], suggestion[end:]
suggestion = "total = sum(prices)\nreturn total"
accepted_text = ""
remainder = suggestion
for unit in ["word", "word", "line", "line"]:
chunk, remainder = split_accept(remainder, unit)
accepted_text += chunk
print(unit, repr(chunk))
print(accepted_text == suggestion, repr(remainder))Running it prints word 'total', word ' =', line ' sum(prices)\n', line 'return total', and then True ''. The accepted chunks always concatenate back to the original text, which is the invariant any partial-accept implementation must keep.
Real editors use language-aware word boundaries, so camelCase and snake_case handling differs. This pattern also lets \s* cross a newline, so a word accept at the end of a line can jump to the next line's first word. Test that edge in the editor you use.
Binding your own keys
You can bind the line-wise command yourself in keybindings.json. Use a key that does not collide with your other bindings.
[
{
"key": "ctrl+alt+right",
"command": "editor.action.inlineSuggest.acceptNextLine",
"when": "inlineSuggestionVisible"
}
]The when clause keeps the binding inactive unless ghost text is on screen.
What partial accept does to the remainder
After a partial accept, the cursor has moved and the remainder is still displayed. Two designs exist:
- Keep the original response and slice it. This is fast and needs no new request. It is what the code above models.
- Issue a new request from the new cursor position. This is slower but can adapt if the user edits the accepted part.
Slicing is the default for good reason: the user has just confirmed the first part, so the rest is conditionally more likely to be right. If the user types something that differs from the remainder, the suggestion goes stale and the normal trigger rules start a fresh request.
Accepting next edit suggestions
Next edit suggestions (NES) change the accept step because the proposed change is not at the cursor. A typical flow in VS Code has two stages: the first Tab moves the cursor to the location of the proposed edit, and the second Tab applies it. The extra step exists so the user sees where the change will land before it happens. Partial acceptance is generally not offered, because an edit is a diff hunk rather than a stream of characters. Rejection works as with completions: Esc dismisses it, and continuing to type elsewhere discards it.
Telemetry that depends on the accept step
The accept step is where most quality metrics are born. A provider can attach a command to an inline item (InlineCompletionItem.command in the VS Code API), which runs after the item is accepted. Use it to log an acceptance event with the request ID. Decide up front how to count the cases above:
- A partial accept counts as a partial acceptance, with the number of characters taken. Counting it as a full accept inflates the rate.
- A request cancelled before display should not appear in the denominator for acceptance rate.
- Retention (is the accepted text still in the file after some minutes?) is measured later and needs the accepted range tracked as the document changes.
Reviewing before you accept
Acceptance is a review step done at typing speed. A few habits catch most problems:
- Read the whole suggestion before pressing Tab, including the last line. Models often get the first line right and drift later.
- Check names against your code. A suggested function that looks plausible may not exist in your project.
- Prefer a partial accept when the first part is right. It keeps you in control of the rest.
- After accepting a multi-line block, run the tests or the type checker instead of trusting the glance.
Edge cases
- Multiple cursors: many editors show ghost text only at the primary cursor, or disable suggestions. Do not assume an accept applies at every cursor.
- Selection active: suggestions are usually hidden when text is selected.
- Slow responses: a suggestion that arrives after the user has moved on must be discarded. The cancellation logic from the debouncing chapter covers this, and the stale check protects against it a second time.
- Whitespace-only suggestions: these are usually filtered upstream, because accepting one produces no visible change and confuses the user.
Exercise
A user sees the suggestion parse_config(path, strict=True)\n. They press Ctrl+Right three times, then type False in place of True. Which accept events should be logged, and what happens to the rest of the suggestion?
Show answer
Three partial-accept events should be logged, each with the characters taken (for example parse_config, (, path). The user then typed text that differs from the remainder (, strict=True)\n), but the remainder starts with ,, so it still matches until True. When the typed False diverges from True, the suggestion becomes stale and is dropped. A new request can then be triggered under the normal debounce rules. The final text is the user's, and the dropped remainder counts as an implicit reject of the rest, not as a full accept.
Exercise
Why does a client keep three outcomes (explicit dismiss, implicit reject, cancelled before display) instead of one "rejected" count?
Show answer
Only the first two mean a person judged or ignored a visible suggestion. A request cancelled before display was never seen, so including it in rejections would lower the measured acceptance rate for reasons unrelated to model quality.