internal/lsp: change diff Op.Content to be the unjoined strings
This also means we don't need the J2 becasue it is implied by len(Content)
Change-Id: I04e2cbaa3e1faa1e3add22ec2d478821b9062419
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170878
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/diff/diff.go b/internal/lsp/diff/diff.go
index 573190b..5e06ec4 100644
--- a/internal/lsp/diff/diff.go
+++ b/internal/lsp/diff/diff.go
@@ -5,19 +5,15 @@
// Package diff implements the Myers diff algorithm.
package diff
-import (
- "strings"
-)
-
// Sources:
// https://blog.jcoglan.com/2017/02/17/the-myers-diff-algorithm-part-3/
// https://www.codeproject.com/Articles/42279/%2FArticles%2F42279%2FInvestigating-Myers-diff-algorithm-Part-1-of-2
type Op struct {
Kind OpKind
- Content string
- I1, I2 int // indices of the line in a
- J1, J2 int // indices of the line in b
+ Content []string // content from b
+ I1, I2 int // indices of the line in a
+ J1 int // indices of the line in b, J2 implied by len(Content)
}
type OpKind int
@@ -53,7 +49,7 @@
}
switch op.Kind {
case Equal, Insert:
- b = append(b, op.Content)
+ b = append(b, op.Content...)
}
prevI2 = op.I2
}
@@ -82,9 +78,8 @@
return
}
op.I2 = i2
- op.J2 = j2
if op.Kind == Insert {
- op.Content = strings.Join(b[op.J1:op.J2], "")
+ op.Content = b[op.J1:j2]
}
solution[i] = op
i++
diff --git a/internal/lsp/diff/diff_test.go b/internal/lsp/diff/diff_test.go
index 9a615e3..966511f 100644
--- a/internal/lsp/diff/diff_test.go
+++ b/internal/lsp/diff/diff_test.go
@@ -19,20 +19,20 @@
a: []string{"A", "B", "C", "A", "B", "B", "A"},
b: []string{"C", "B", "A", "B", "A", "C"},
operations: []*Op{
- &Op{Kind: Delete, I1: 0, I2: 1, J1: 0, J2: 0},
- &Op{Kind: Delete, I1: 1, I2: 2, J1: 0, J2: 0},
- &Op{Kind: Insert, Content: "B", I1: 3, I2: 3, J1: 1, J2: 2},
- &Op{Kind: Delete, I1: 5, I2: 6, J1: 4, J2: 4},
- &Op{Kind: Insert, Content: "C", I1: 7, I2: 7, J1: 5, J2: 6},
+ &Op{Kind: Delete, I1: 0, I2: 1, J1: 0},
+ &Op{Kind: Delete, I1: 1, I2: 2, J1: 0},
+ &Op{Kind: Insert, Content: []string{"B"}, I1: 3, I2: 3, J1: 1},
+ &Op{Kind: Delete, I1: 5, I2: 6, J1: 4},
+ &Op{Kind: Insert, Content: []string{"C"}, I1: 7, I2: 7, J1: 5},
},
},
{
a: []string{"A", "B"},
b: []string{"A", "C", ""},
operations: []*Op{
- &Op{Kind: Delete, I1: 1, I2: 2, J1: 1, J2: 1},
- &Op{Kind: Insert, Content: "C", I1: 2, I2: 2, J1: 1, J2: 2},
- &Op{Kind: Insert, Content: "", I1: 2, I2: 2, J1: 2, J2: 3},
+ &Op{Kind: Delete, I1: 1, I2: 2, J1: 1},
+ &Op{Kind: Insert, Content: []string{"C"}, I1: 2, I2: 2, J1: 1},
+ &Op{Kind: Insert, Content: []string{""}, I1: 2, I2: 2, J1: 2},
},
},
} {
diff --git a/internal/lsp/source/format.go b/internal/lsp/source/format.go
index cda0916..bdcf64a 100644
--- a/internal/lsp/source/format.go
+++ b/internal/lsp/source/format.go
@@ -75,7 +75,7 @@
edits = append(edits, TextEdit{Span: s})
case diff.Insert:
// Insert: formatted[j1:j2] is inserted at unformatted[i1:i1].
- edits = append(edits, TextEdit{Span: s, NewText: op.Content})
+ edits = append(edits, TextEdit{Span: s, NewText: strings.Join(op.Content, "")})
}
}
return edits