Sub topic 9
Next edit suggestions: predicting the edit, not just the text
This chapter assumes you know how inline completions work: the model gets the text before and after the cursor and returns ghost text at the cursor. It also assumes you can read a unified diff and basic Python or TypeScript.
What a next edit suggestion is
An inline completion answers one question: what text goes at the cursor? A next edit suggestion (NES) answers a different one: given what you just changed, what should you change next, and where?
Renaming a function parameter is the standard case. You edit the definition, and the call sites are now out of date. The cursor is not at the call site, so a normal completion cannot help. A next edit suggestion proposes the matching change there.
--- 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)In this diff, the first hunk is the edit you made. The describe change is the suggestion.
Inputs and outputs compared
The two features take different inputs and return different outputs.
| Aspect | Inline completion | Next edit suggestion |
|---|---|---|
| Main input | Prefix and suffix around the cursor | Recent edits plus the surrounding file |
| Output | Text to insert at the cursor | A location and a replacement, often shown as a diff |
| Can delete or replace text | Rarely | Yes |
| Where it appears | Ghost text at the cursor | Often away from the cursor, with a marker or diff preview |
| Typical trigger | Typing | A finished edit, or moving the cursor |
The request
The cursor position alone is not enough context. The request usually carries the following:
- The current file content, or a window of it around the area of interest.
- A short log of recent edits, each with its position, the old text and the new text.
- Sometimes related files, selected in the same way as for completions.
The edit log carries most of the signal. It shows the model the pattern behind your change. After w became width, the log says the same rename may be needed wherever w still appears.
What the model produces
The model has to produce more than text. It predicts a location and a change. Many systems have the model rewrite a region of the file, and the editor then computes the difference between the old region and the new one. Others ask for explicit edit operations. Either way, the editor turns the result into a diff you can preview before applying it.
A small sketch shows the diffing step in Python:
import difflib
old_region = ["def describe():\n", " return area(w=2, h=3)\n"]
new_region = ["def describe():\n", " return area(width=2, height=3)\n"]
for line in difflib.unified_diff(old_region, new_region, "a/geometry.py", "b/geometry.py"):
print(line, end="")Running this prints the changed line as a removal and an addition. A real editor uses the same idea to decide what to highlight.
Using suggestions
Because a suggestion can change code away from your cursor, check it before you accept it. Look at the diff, confirm the location is the one you meant, and then accept. Accepting often jumps the cursor to the edit, and a further press can move to the next suggestion in a chain.
Limits
- The model only sees edits recorded in the editor. Changes made by a script or in another tool do not appear in the log.
- Suggestions depend on a clear pattern. A one-off edit gives the model little to generalize from.
- Latency matters more here, because a late suggestion appears after you have moved on.
Exercise
You rename a parameter in a function definition, but your cursor stays there. Why can an inline completion not propose the update to the call site, and what does a next edit suggestion use instead?
Show answer
An inline completion only produces text to insert at the cursor, built from the prefix and suffix. The call site is elsewhere in the file. A next edit suggestion uses the recent edit history to infer the pattern, predicts the location of the next change, and returns a replacement shown as a diff.