Skip to content

Commit 3d99ebe

Browse files
committed
gopls/diff/unified: remove redundant information
Sometimes the unified diff would show the same line removed and added. That is avoided by this CL. Fixes: golang/go#59232 Change-Id: Ie6562e3c922b8f0c4319eefdde1913b2d9bc7878 Reviewed-on: https://go-review.googlesource.com/c/tools/+/489695 Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 479f5c6 commit 3d99ebe

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

internal/diff/diff.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func lineEdits(src string, edits []Edit) ([]Edit, error) {
115115
}
116116

117117
// Do all edits begin and end at the start of a line?
118-
// TODO(adonovan): opt: is this fast path necessary?
119-
// (Also, it complicates the result ownership.)
118+
// TODO(adonovan, pjw): why does omitting this 'optimization'
119+
// cause tests to fail? (TestDiff/insert-line,extra_newline)
120120
for _, edit := range edits {
121121
if edit.Start >= len(src) || // insertion at EOF
122122
edit.Start > 0 && src[edit.Start-1] != '\n' || // not at line start

internal/diff/difftest/difftest.go

+7
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ var TestCases = []struct {
272272
-
273273
A
274274
`,
275+
}, {
276+
Name: "unified_lines",
277+
In: "aaa\nccc\n",
278+
Out: "aaa\nbbb\nccc\n",
279+
Edits: []diff.Edit{{Start: 3, End: 3, New: "\nbbb"}},
280+
LineEdits: []diff.Edit{{Start: 0, End: 4, New: "aaa\nbbb\n"}},
281+
Unified: UnifiedPrefix + "@@ -1,2 +1,3 @@\n aaa\n+bbb\n ccc\n",
275282
},
276283
}
277284

internal/diff/unified.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,18 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
156156
last++
157157
}
158158
if edit.New != "" {
159-
for _, content := range splitLines(edit.New) {
160-
h.Lines = append(h.Lines, line{Kind: Insert, Content: content})
159+
for i, content := range splitLines(edit.New) {
161160
toLine++
161+
// Merge identical Delete+Insert.
162+
// This is an unwanted output of converting diffs to line diffs
163+
// that is easiest to fix by postprocessing.
164+
// e.g. issue #59232: ("aaa\nccc\n", "aaa\nbbb\nccc")
165+
// -> [Delete "aaa\n", Insert "aaa\n", Insert "bbb\n", ...].
166+
if i == 0 && last > start && h.Lines[len(h.Lines)-1].Content == content {
167+
h.Lines[len(h.Lines)-1].Kind = Equal
168+
continue
169+
}
170+
h.Lines = append(h.Lines, line{Kind: Insert, Content: content})
162171
}
163172
}
164173
}

0 commit comments

Comments
 (0)