Sub topic 11
Choosing Between Completions and Next Edit Suggestions
This chapter assumes you know how ghost text is shown at the cursor, how a request is built from prefix, suffix and related files, and what recent edit history is. Here you learn to pick the right feature for the change in front of you.
Two different questions
An inline completion answers: "What text belongs at the cursor?" A next edit suggestion (NES) answers: "Given what I just changed, what should change next, and where?"
| Aspect | Inline completion | Next edit suggestion |
|---|---|---|
| Main input | Prefix and suffix around the cursor, plus related files | Recent edits, the current file, and nearby code |
| Output | Text to insert at the cursor | A change at a predicted location, often shown as a diff |
| Location | Always the cursor | Anywhere in the file, sometimes another file |
| Display | Ghost text in the line | Highlighted change or an arrow pointing to the target |
| Best trigger | Typing | Finishing an edit |
Decision rules
Use a completion when you are producing new text. Typical cases are the body of a function, the rest of a line, a test case, or a docstring. The answer depends on the code around the cursor, and the cursor is where the change happens.
Use a next edit suggestion when an edit you already made implies other edits. Typical cases are renaming a parameter, changing a function signature, changing a type, or repeating a pattern across several lines. The following diff shows this. After you rename w and h in the definition, the call site is stale.
--- 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)A completion cannot fix describe(). Your cursor is not there, so the model is never asked about that line. NES treats your rename as evidence and proposes the matching edit at the call site.
Common errors
- Expecting a completion to propagate a change. After a rename, the old name remains elsewhere and fails at run time. Look for a NES, or use your editor's rename refactoring.
- Expecting NES in a new file. With no edit history there is nothing to extrapolate from. Completions work from the first keystroke.
- Accepting a NES without reading it. The change may sit far from the cursor. Read the whole diff, as you would review a pull request.
- Fighting over the same key. When both features are enabled, they may share an accept key such as Tab. Behavior differs between editors. Learn which suggestion is active before you press it.
- Judging both by one latency target. A completion competes with your typing and must be fast. A NES appears after a pause, so it can use more context and take longer.
A quick rule of thumb
Ask where the change happens. If it happens at the cursor, ask for a completion. If it happens somewhere you are not looking, but is caused by what you just did, expect a next edit suggestion.
Exercise
You change a function load(path) to load(path, encoding). Three call sites elsewhere in the file now fail. Which feature is the better fit for fixing them, and why would an inline completion not help?
Show answer
A next edit suggestion fits, because your signature change is recent edit history that implies matching changes at the call sites. An inline completion only generates text at the cursor, so it is never asked about the other call sites.