Sub topic 11
Choosing between completions and next edit suggestions
This chapter assumes you have read the earlier chapters on inline completions, fill-in-the-middle prompting, post-processing, and next edit suggestions (NES). It also assumes you can read short Python and a unified diff. The goal is to decide which feature should respond to a given editing situation, and to understand what each choice costs.
Two features, two questions
An inline completion answers the question "what text goes at the cursor?" The model gets the text before and after the cursor. It returns a string that is inserted at that exact position, shown as ghost text.
A next edit suggestion answers a different question: "given what the user just changed, what change should happen next, and where?" The model gets recent edits as well as the surrounding code. It returns an edit, often with a location that is not the cursor. The editor usually shows that edit as a diff or as a highlighted jump target.
The two features share infrastructure: context assembly, debouncing, cancellation, and filtering of model output. They differ in what the model must predict and in how much the user has to check.
Comparing the two side by side
| Dimension | Inline completion | Next edit suggestion |
|---|---|---|
| Main input | Prefix and suffix at the cursor, plus related files | Recent edit history, current file, cursor, and often related files |
| Output | Text to insert at the cursor | One or more edits: a range to replace and the new text |
| Location | Always the cursor | Cursor or anywhere else in the file, sometimes another file |
| Typical display | Ghost text in the line | Inline diff, gutter arrow, or a "jump to edit" hint |
| Accept gesture | Accept all, word, or line | Jump to the location, then accept the diff |
| Trigger | Typing, pausing, or an explicit shortcut | A finished edit, a cursor move, or a pause after a change |
| Typical size | A token to a few lines | A few lines changed, sometimes in several places |
| Cost of a wrong suggestion | Low. You keep typing and it disappears | Higher. The user must read the diff and may lose their place |
The cost difference is the most useful row for design decisions. A wrong ghost-text suggestion is overwritten by the next keystroke. A wrong NES moves the user's attention away from where they were working, and they must read a diff to judge it.
What each feature is good at
Completions
Completions work best when the next piece of code is determined by the code just before the cursor and by what follows it.
- Finishing a line or a short block, such as a loop body or a return statement.
- Filling in a function body from its signature and docstring.
- Writing boilerplate that follows a pattern already visible in the file.
- Typing new code at the end of a file or in an empty region.
In these cases there is nothing "stale" elsewhere in the file. The user is adding new text, so fill-in-the-middle prompting is enough.
Next edit suggestions
NES works best when an edit creates a consequence somewhere else.
- A parameter was renamed in a definition, and its call sites still use the old name.
- A function gained a new argument, and the callers need updating.
- A constant changed, and a comment, test, or default value elsewhere now disagrees with it.
- The user made one change in a repeated pattern and is likely to repeat it a few lines down.
Ordinary completions are poor at these. The cursor is at the first edit, but the needed change is at a different line. The model needs to see the edit itself, not only the resulting text. That is why edit history is part of the NES input.
A simple routing heuristic
Some products run both features at once and let the editor decide which to show. A simple version of that decision can be written as a check for stale references. After an edit, look for identifiers that the edit removed but that still appear elsewhere. If any exist, a next edit is likely useful. Otherwise, fall back to a completion at the cursor.
import re
from dataclasses import dataclass
@dataclass
class Edit:
line: int # 0-based line the edit touched
old: str # text before the edit
new: str # text after the edit
def stale_references(document: str, edit: Edit) -> list[int]:
"""Return 0-based line numbers that still use an identifier the edit removed."""
word = r"[A-Za-z_]\w*"
removed = set(re.findall(word, edit.old)) - set(re.findall(word, edit.new))
hits = []
for number, text in enumerate(document.splitlines()):
if number == edit.line:
continue
if any(re.search(rf"\b{re.escape(name)}\b", text) for name in removed):
hits.append(number)
return hits
def choose_mode(document: str, cursor_line: int, last_edit: Edit | None):
if last_edit is not None:
stale = stale_references(document, last_edit)
if stale:
return "next_edit", stale
return "completion", [cursor_line]
document = (
"def area(width, height):\n"
" return w * h\n"
"\n"
"def describe():\n"
" return area(w=2, h=3)\n"
)
edit = Edit(line=0, old="def area(w, h):", new="def area(width, height):")
print(choose_mode(document, cursor_line=0, last_edit=edit))
print(choose_mode("x = 1\ny = ", cursor_line=1, last_edit=None))The first call prints ('next_edit', [1, 4]): the body and the call site still use w and h. The second prints ('completion', [1]).
This heuristic is deliberately crude. A real system uses a model to predict edits, not a regular expression. The point is the shape of the decision. NES is triggered by a change that has consequences, and completion is the default.
How the two fit together in time
The two features also differ in when they act. Completions react to typing in progress. NES reacts to a finished edit. A single editing session often moves between them.
Both branches end in filtering. Completions need the overlap trimming covered in the post-processing chapter. Edits need a check that the target range still matches the current document, because the user may have typed while the request was in flight.
Costs and trade-offs
Latency
Completions are expected to appear within a few hundred milliseconds. Users are typing, and a late suggestion is often already wrong. NES can tolerate a little more delay, because it triggers after an edit is finished, not during typing. NES also tends to send more context (the edit history) and generate more output (a range and replacement text), so it is usually slower per request.
Context size
A completion request needs the prefix, the suffix, and some related files. A NES request needs all of that plus the edits. Edit history can grow quickly, so systems usually keep only the last few edits and compress or drop older ones.
Interruption
Ghost text sits where the user is already looking. A NES may appear at a distant line and ask the user to move. Frequent low-quality NES hints are more irritating than frequent low-quality completions, because each one pulls attention away. Many products respond by showing NES only when confidence is high, or by showing a small hint first and the full diff only after the user steps to it.
Review burden
A completion is usually short enough to read in one glance. A multi-line diff needs real reading. The user has to confirm both what changes and that nothing else is touched.
Common mistakes when choosing
- Using completions for cross-location changes. The user accepts the first rename and then must hunt for each call site. If your tool does this, a NES-style feature is the missing piece.
- Using NES for plain new code. When there is no prior edit and nothing to update, a NES request has nothing to predict. Completion is cheaper and less intrusive.
- Showing both at once. If ghost text at the cursor and a diff elsewhere appear together, the Tab key is ambiguous. Pick one accept target per moment. A common rule is that the completion wins while the user is typing, and the NES wins when the cursor is idle after an edit.
- Ignoring stale results. A NES computed before the user typed another character may apply to text that no longer exists. Check the range and the document version before showing it.
Decision criteria
Use these questions in order when you design or configure a tool.
- Is the user adding new text where nothing else needs to change? Use completion.
- Did the last edit create an inconsistency elsewhere in the file or project? Use NES.
- Is the likely change one or two tokens at the cursor? Completion is enough.
- Is the change several lines, or in several places? Prefer NES, shown as a diff.
- Is latency under about 300 ms a hard requirement? Keep NES off the typing path and run it only after a pause.
- Do you have edit history available and reliable? If not, NES quality will be poor, and completion is the safer choice.
For users, the same logic maps to settings. Most editors let you turn either feature on or off separately, and some let you limit NES to certain languages. If you mostly write new code, completions give you most of the value. If you refactor often, NES saves the most time.
Exercise
You rename a function parameter from count to total in a function definition. Two lines later in the same function body, count is still used. Which feature should respond, and what two inputs does it need beyond the text at the cursor?
Show answer
A next edit suggestion should respond, because the rename left stale references at locations other than the cursor. Beyond the prefix and suffix at the cursor, it needs the recent edit (the old and new text of the rename) and the rest of the current file, so it can find and update the remaining uses of count.
Exercise
Why can a wrong NES cost the user more than a wrong inline completion? Give one design measure that reduces the cost.
Show answer
A wrong completion is replaced by the next keystroke and costs almost nothing. A wrong NES may point to a distant line, pulls the user's attention away, and requires reading a diff to reject. Measures include showing NES only above a confidence threshold, or showing a small jump hint first and the full diff only when the user steps to it.