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>
This commit is contained in:
Ian Cottrell 2019-04-05 12:16:45 -04:00
parent f558378bf8
commit b184d1ccfc
3 changed files with 14 additions and 19 deletions

View File

@ -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 @@ func ApplyEdits(a []string, operations []*Op) []string {
}
switch op.Kind {
case Equal, Insert:
b = append(b, op.Content)
b = append(b, op.Content...)
}
prevI2 = op.I2
}
@ -82,9 +78,8 @@ func Operations(a, b []string) []*Op {
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++

View File

@ -19,20 +19,20 @@ func TestDiff(t *testing.T) {
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},
},
},
} {

View File

@ -75,7 +75,7 @@ func computeTextEdits(ctx context.Context, file File, formatted string) (edits [
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