Sub topic 10
Where the next edit goes and how it is shown as a diff
This chapter assumes you have read the chapters on next edit suggestions and on recent edit history as context. You should be comfortable reading a unified diff and know that the model receives your recent edits as part of its prompt.
Two questions: where and what
An inline completion always lands at the cursor, so the editor never has to decide where it goes. A next edit suggestion (NES) has two separate jobs:
- Predict the location: which line or range in the file should change.
- Predict the content: what the text there should become.
Both predictions come from the same signal, your recent edit history. If you renamed w and h in a function definition, the model sees that the old names still appear in a call site. The most likely next change is to update that call.
Finding the location
Systems differ in detail, but a typical pipeline has these steps.
- Record recent edits as small before/after records with file, line range, and text.
- Send the edits, plus a window of code around the cursor and possibly other open files, to the model.
- Ask the model to return the edited region, not only the new text.
- Match that region against the real file to get an exact range.
Step 4 matters. A model can return text that nearly matches the file but differs in whitespace or has drifted by a line. If the range cannot be found, a careful implementation drops the suggestion instead of guessing. A wrong location is worse than no suggestion, because it can overwrite code you did not intend to change.
Why a diff is the natural output
A model can return the whole rewritten region. That is simple, but it makes the editor compare old and new text to work out what changed. Asking for a diff, or a list of replacements, gives the editor ranges directly. It also keeps the output short, which lowers latency.
Here is a suggestion after the parameters of area were renamed. The first hunk is your edit. The second hunk is the predicted one.
--- 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)Each - line is removed and each + line is added. Unchanged lines act as context that anchors the hunk.
Showing the suggestion
Ghost text only works for insertions at the cursor. An edit elsewhere may delete or replace text, so editors use other cues:
- An arrow or gutter marker that points to the target line.
- Red and green highlighting of the lines that would change.
- A key to jump to the suggestion, and a key to accept it.
The jump step is deliberate. It shows you the location before any text changes, and the same key can then apply the edit.
Do not use the empty fence above. The flow of a suggestion is shown in this diagram instead.
Common errors
| Problem | Cause | Mitigation |
|---|---|---|
| Edit applied to the wrong lines | The file changed while the request was in flight | Check the range against the current text, and cancel stale requests |
| Suggestion repeats your last edit | The history shows the edit but the model treats it as unfinished | Filter suggestions that equal the current text |
| Large, hard-to-review diff | The model rewrote a wide region | Limit the number of changed lines, and show a preview |
Exercise
A next edit suggestion proposes a change at line 40 while your cursor is on line 12. Why can it not be shown as ghost text, and what must the editor do before applying it?
Show answer
Ghost text is only drawn at the cursor, but this edit is at a different location and may replace or delete text. The editor must show a marker or diff preview for line 40, confirm that the predicted range still matches the current file, and apply it only after you accept.