-
Notifications
You must be signed in to change notification settings - Fork 366
feat(tui): word-level highlighting in edit_file diff view #2900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rumpl
wants to merge
1
commit into
docker:main
Choose a base branch
from
rumpl:improve-diff-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package editfile | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/tools" | ||
| "github.com/docker/docker-agent/pkg/tui/types" | ||
| ) | ||
|
|
||
| // TestRenderEditFile_EndToEnd writes a temporary source file, builds an | ||
| // edit_file tool call against it, and renders both unified and split views. | ||
| // The test focuses on structural elements rather than exact escape sequences, | ||
| // which depend on the active theme. | ||
| func TestRenderEditFile_EndToEnd(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "main.go") | ||
|
|
||
| updated := `package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| x := 10 | ||
| y := 20 | ||
| fmt.Println(x + y) | ||
| } | ||
| ` | ||
| // Simulate the post-execution state: file already contains the new content. | ||
| require.NoError(t, os.WriteFile(path, []byte(updated), 0o644)) | ||
|
|
||
| args := map[string]any{ | ||
| "path": path, | ||
| "edits": []map[string]string{ | ||
| { | ||
| "oldText": "\tx := 1\n\ty := 2", | ||
| "newText": "\tx := 10\n\ty := 20", | ||
| }, | ||
| }, | ||
| } | ||
| encoded, err := json.Marshal(args) | ||
| require.NoError(t, err) | ||
|
|
||
| toolCall := tools.ToolCall{ | ||
| ID: "test-render-1", | ||
| Function: tools.FunctionCall{ | ||
| Name: "edit_file", | ||
| Arguments: string(encoded), | ||
| }, | ||
| } | ||
|
|
||
| // Reset cache so the test is hermetic across runs. | ||
| InvalidateCaches() | ||
| t.Cleanup(InvalidateCaches) | ||
|
|
||
| unified := renderEditFile(toolCall, 120, false, types.ToolStatusCompleted) | ||
| split := renderEditFile(toolCall, 120, true, types.ToolStatusCompleted) | ||
|
|
||
| for _, out := range []string{unified, split} { | ||
| assert.NotEmpty(t, out) | ||
| // Source content should appear in the diff regardless of theme escapes. | ||
| assert.True(t, strings.Contains(out, "10") || strings.Contains(out, "20")) | ||
| } | ||
|
|
||
| added, removed := countDiffLines(toolCall, types.ToolStatusCompleted) | ||
| assert.Equal(t, 2, added) | ||
| assert.Equal(t, 2, removed) | ||
| } | ||
|
|
||
| func TestRenderEditFile_TabIndentedLineDoesNotPanic(t *testing.T) { | ||
| // Regression: tab-indented modified lines used to feed raw (1-byte-tab) | ||
| // text into diffWords while chroma tokens were built from the | ||
| // tab-expanded variant, producing out-of-bounds slice indices in | ||
| // applyWordEmphasis. The fix routes both through prepareContent. | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "main.go") | ||
|
|
||
| updated := "package main\n\nfunc main() {\n\tx := 10\n}\n" | ||
| require.NoError(t, os.WriteFile(path, []byte(updated), 0o644)) | ||
|
|
||
| args := map[string]any{ | ||
| "path": path, | ||
| "edits": []map[string]string{ | ||
| {"oldText": "\tx := 1", "newText": "\tx := 10"}, | ||
| }, | ||
| } | ||
| encoded, _ := json.Marshal(args) | ||
| toolCall := tools.ToolCall{ | ||
| ID: "test-tab-1", | ||
| Function: tools.FunctionCall{Name: "edit_file", Arguments: string(encoded)}, | ||
| } | ||
|
|
||
| InvalidateCaches() | ||
| t.Cleanup(InvalidateCaches) | ||
|
|
||
| assert.NotPanics(t, func() { | ||
| _ = renderEditFile(toolCall, 120, false, types.ToolStatusCompleted) | ||
| _ = renderEditFile(toolCall, 120, true, types.ToolStatusCompleted) | ||
| }) | ||
| } | ||
|
|
||
| func TestRenderEditFile_MissingFileReturnsEmptyDiff(t *testing.T) { | ||
| args := map[string]any{ | ||
| "path": "/nonexistent/path/that/does/not/exist.go", | ||
| "edits": []map[string]string{ | ||
| {"oldText": "a", "newText": "b"}, | ||
| }, | ||
| } | ||
| encoded, _ := json.Marshal(args) | ||
| toolCall := tools.ToolCall{ | ||
| ID: "test-missing-1", | ||
| Function: tools.FunctionCall{Name: "edit_file", Arguments: string(encoded)}, | ||
| } | ||
|
|
||
| InvalidateCaches() | ||
| t.Cleanup(InvalidateCaches) | ||
|
|
||
| // Should not panic on a missing source file. | ||
| assert.NotPanics(t, func() { | ||
| _ = renderEditFile(toolCall, 100, false, types.ToolStatusCompleted) | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[HIGH] Tab expansion in
prepareContentcauses byte-offset mismatch with word-diff segments — wrong emphasis and potential panic on tab-indented linesprepareContent()expands each\tto 4 spaces before passing content tosyntaxHighlight. The chroma tokens produced therefore have byte offsets relative to the tab-expanded string. However,diffWordsis called with the raw (non-tab-expanded) content:applyWordEmphasismaps segment byte ranges onto chroma token byte ranges. For any line containing a\tbefore a changed word, the segment byte positions are shifted left by(tabWidth-1) * numTabsBeforerelative to the chroma token byte positions. This causes:text[cursor-tokStart : end-tokStart]can produce a negative or out-of-bounds index ifend(derived from the segment range) exceeds the actual chroma token's byte length.Since Go source files — including the test fixtures in this PR — are almost always tab-indented, this affects every real-world usage of the word-level emphasis.
Fix: Call
applyWordEmphasisusing segments derived fromprepareContent-processed input (i.e., passprepareContent(...)todiffWordsas well), or expand tabs in the content passed todiffWordsbefore computing segments.