Sub topic 9
Next edit suggestions: predicting the edit, not just the text
This chapter explains what a next edit suggestion (NES) is, what it needs as input, and what it produces. It assumes you have read the earlier chapters on inline completions, context assembly and accepting suggestions. You should be comfortable with diffs, and with reading short Python and TypeScript snippets.
From "what comes next" to "what changes next"
An inline completion answers one question: given the text before and after the cursor, what text belongs at the cursor? The output is always an insertion at the cursor position. The model never needs to decide where to edit, because the cursor already tells it.
A next edit suggestion answers a different question: given what you just changed, what change should happen next? The answer may be:
- an insertion at the cursor,
- a replacement of existing text,
- a deletion,
- a change on a line far from the cursor, possibly several lines away.
So the model predicts the edit itself, which has a location, a range of old text and a range of new text. The completion case is a special case where the location is fixed and the old text is empty.
A motivating example
Suppose you rename the parameters of a function from w, h to width, height. The body of the function now refers to names that no longer exist, and the call site still uses the old keyword arguments. An inline completion cannot help here. The cursor sits in the signature, and the text that needs to change is below it.
A next edit suggestion sees the rename in your recent edits and proposes the matching changes. The diff for the call site looks like this:
--- a/geometry.py
+++ b/geometry.py
@@ -1,6 +1,6 @@
-def area(w, h):
- return w * h
+def area(width, height):
+ return width * height
def describe():
- return area(w=2, h=3)
+ return area(width=2, height=3)The first changed pair of lines is the edit you made yourself. The editor shows the second pair, the call site, as a suggestion. Pressing the accept key applies it and, in most editors, jumps the cursor to the next suggestion.
Inputs: what the model sees
A completion request is mostly a prefix, a suffix and some related snippets. A NES request adds a record of what you have been doing.
| Input | Inline completion | Next edit suggestion |
|---|---|---|
| Text before the cursor | Yes, central | Yes, as part of a larger window |
| Text after the cursor | Yes, central | Yes, as part of a larger window |
| Cursor position | Yes | Yes |
| Related files | Often | Often |
| Recent edit history | Rarely | Yes, central |
| Trigger | Typing | Edits, cursor moves, accepted suggestions |
The recent edit history is the key addition. It is a list of your latest changes, usually stored as small diffs with file names. A typical request might contain:
- a window of the current file around the cursor, often the whole function or a few dozen lines above and below,
- the last handful of edits, newest last,
- optionally, snippets from other open files.
The window is usually marked up so the model knows which region it may rewrite. Different systems use different markers, so treat the format below as illustrative only.
from dataclasses import dataclass
@dataclass
class Edit:
path: str
old: str
new: str
def render_history(edits: list[Edit], limit: int = 5) -> str:
"""Format the most recent edits as small before/after pairs."""
lines = []
for edit in edits[-limit:]:
lines.append(f"file: {edit.path}")
lines.append(f"- {edit.old}")
lines.append(f"+ {edit.new}")
return "\n".join(lines)
def build_nes_prompt(history: list[Edit], window: str, cursor_line: int) -> str:
return (
"## Recent edits\n"
f"{render_history(history)}\n\n"
"## Editable region\n"
f"{window}\n\n"
f"## Cursor on line {cursor_line}\n"
"## Rewrite the editable region with the next edit applied\n"
)
history = [Edit("geometry.py", "def area(w, h):", "def area(width, height):")]
window = "def area(width, height):\n return w * h\n"
print(build_nes_prompt(history, window, cursor_line=1))The prompt asks for a rewrite of a bounded region. This is a common design choice: the model returns the new version of the region, and the editor computes the difference. The next chapter on edit location covers that step.
Outputs: an edit, not a string
A completion is a string plus an implied position. A NES result needs more structure:
- the file it applies to,
- the range of existing text to replace (it may be empty for a pure insertion),
- the replacement text (it may be empty for a pure deletion).
Editors usually represent this as a text edit with a range and new text, the same shape used by many language-server features. Because the edit can sit away from the cursor, the editor also needs a way to show it and a way to take the user there.
How the model is built to predict edits
Two broad approaches exist, and products mix them.
Rewrite a region. The model receives the code window and history, and it outputs the full updated window. A diff against the original gives the edit. This approach is simple and reuses the same kind of model as completions. It costs more tokens, because unchanged lines are generated too, and so it adds latency on long windows.
Emit edits directly. The model outputs only the changed parts, for example a line number and replacement text. This is shorter and faster, but the model must count lines or reference anchors correctly. Mistakes in line numbers produce edits in the wrong place.
Many systems train or fine-tune a model specifically on edit sequences taken from real editing sessions. The training signal is: given the history up to a point, predict the next edit that the developer actually made. That differs from completion training, where the signal is simply the next tokens in a file.
When a suggestion appears
A NES system has to decide whether it has something worth showing. Unlike ghost text, which fills a gap at the cursor and can be wrong cheaply, a suggestion on a distant line interrupts your reading. Systems therefore apply several checks:
- The model may output "no edit". This is a valid and frequent result.
- A suggestion identical to the existing text is dropped.
- A suggestion that only touches whitespace may be dropped.
- A suggestion far from the cursor may require higher confidence than one near it.
The process looks like this:
Note the loop from "Apply edit" back to the request. After you accept one edit, the new state is itself a recent edit, so the model can propose the next one. This is how a rename can ripple through a file with a few key presses.
Triggers
Completions fire mainly on typing. NES has more triggers, because the situation that creates a useful suggestion is not only a keystroke:
- you finish an edit and pause,
- you move the cursor to a new line after editing,
- you accept a previous suggestion,
- you save the file or switch back to it.
The same debouncing and cancellation rules still apply. A request that is superseded by newer edits is stale and should be cancelled, because its suggestion would be based on a history that is no longer current. A late result that is applied to changed text is a common source of wrong edits, so editors typically check that the document version is unchanged before showing a result.
Practical limits
The history window is finite. Old edits fall out, so a refactor that spans many minutes may lose its starting point. Long files force the system to choose a window, and edits outside that window cannot be predicted well. Related edits in files you have not opened are also invisible to the model unless the system adds them as context.
Suggestions can also be confidently wrong. A rename suggestion may touch an unrelated variable with the same name in another scope. Review each suggested diff as you would a pull request change, which later chapters on failure modes expand on.
Summary of the differences
- A completion inserts text at a known position. A NES predicts both the position and the content of an edit.
- A completion is driven by the prefix and suffix. A NES is driven by recent edits plus a code window.
- A completion appears as ghost text at the cursor. A NES may appear elsewhere in the file and needs a way to navigate to it.
- A NES result may be "nothing to suggest", and this is expected.
Exercise
You change a function's return type annotation from int to float. Three lines below, a variable assigned from that function still has the annotation int. Explain why an inline completion is unlikely to fix it, and which inputs let a next edit suggestion do so.
Show answer
An inline completion only generates text at the cursor, and the cursor is on the changed function, not on the variable below. It has no mechanism to choose a different location or replace existing text. A next edit suggestion receives the recent edit (int changed to float) and a code window that includes the variable. From that history it can predict a replacement of int with float on the later line and show it as a diff for you to accept.
Exercise
A NES request is sent after your edit, but you type two more characters before the response arrives. What should the editor do with the response, and why?
Show answer
It should discard it, or at least check that the document version matches the one used to build the request. The history and code window changed after the request was built, so the suggested edit may refer to ranges that moved or to text that no longer exists. Applying it could damage the file. Cancelling the superseded request also saves backend work.