fix: stop duplicating surrounding code in inline chat diffs - #203
Draft
laileni-aws wants to merge 1 commit into
Draft
fix: stop duplicating surrounding code in inline chat diffs#203laileni-aws wants to merge 1 commit into
laileni-aws wants to merge 1 commit into
Conversation
When inline chat is invoked with an empty selection (cursor on a blank line inside a function), the model is given the surrounding code as context and usually answers with the whole enclosing block. The diff was computed against the empty selection, so every line of the response, including code that already exists above and below the cursor, was rendered as an insertion. Accepting the suggestion duplicated the enclosing method, and the inserted block was mis-indented because the first line was re-based on the empty selection. Before diffing a complete response, widen the task selection to cover the document lines that the response repeats directly above and below it, and re-indent the response so it lines up with those lines. The echoed lines then diff as unchanged and only the new code is shown. When nothing is echoed and the selection is blank, keep the response's own indentation for every line instead of stripping the first line's indent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When inline chat is invoked with the cursor on a blank line (no selection) inside a function, the returned diff re-inserts the entire enclosing method with the new code inside it. Accepting the diff duplicates the lines that already exist above and below the cursor, and the inserted block is mis-indented compared to the surrounding code.
Root cause:
InlineTasktreats the cursor's (empty) line as the selected text, while the model is given the surrounding code as context and typically answers with the whole enclosing block.computeDiffthen diffs the full response against an empty string, so every line—including code that already exists—becomes an insertion. The first line's indentation is also re-based on the empty selection, so it lands at column 0 while later lines keep the model's indentation.Solution
Before diffing a complete response, anchor it against the document:
Partial (streaming) diffs are left as-is and are corrected when the final response is applied. Behavior for non-empty selections whose response does not repeat surrounding code is unchanged.
Adds unit tests for
anchorResponseToDocumentandcomputeDiffcovering: echoing the enclosing method with an empty selection, re-indentation, one-sided overlap, no overlap, blank-line trimming, non-empty selections, partial responses, and a regression check for a plain selection rewrite.feature/xbranches will not be squash-merged at release time.