Skip to content

Commit 88641d9

Browse files
committed
internal/lsp: cleanup the diff/myers package
The only exposed symbol is now the ComputeEdits function, all other functionality is now moved up to the diff layer and using edits instead of operations. Change-Id: I149e4f3276592e1a7c2c52e6eaffc826cc22a9fa Reviewed-on: https://go-review.googlesource.com/c/tools/+/198518 Run-TryBot: Ian Cottrell <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent cc9bfb3 commit 88641d9

File tree

2 files changed

+29
-96
lines changed

2 files changed

+29
-96
lines changed

internal/lsp/diff/myers/diff.go

+29-34
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,42 @@ import (
99
"strings"
1010

1111
"golang.org/x/tools/internal/lsp/diff"
12+
"golang.org/x/tools/internal/span"
1213
)
1314

1415
// Sources:
1516
// https://blog.jcoglan.com/2017/02/17/the-myers-diff-algorithm-part-3/
1617
// https://www.codeproject.com/Articles/42279/%2FArticles%2F42279%2FInvestigating-Myers-diff-algorithm-Part-1-of-2
1718

18-
type Op struct {
19+
func ComputeEdits(uri span.URI, before, after string) []diff.TextEdit {
20+
ops := operations(splitLines(before), splitLines(after))
21+
edits := make([]diff.TextEdit, 0, len(ops))
22+
for _, op := range ops {
23+
s := span.New(uri, span.NewPoint(op.I1+1, 1, 0), span.NewPoint(op.I2+1, 1, 0))
24+
switch op.Kind {
25+
case diff.Delete:
26+
// Delete: unformatted[i1:i2] is deleted.
27+
edits = append(edits, diff.TextEdit{Span: s})
28+
case diff.Insert:
29+
// Insert: formatted[j1:j2] is inserted at unformatted[i1:i1].
30+
if content := strings.Join(op.Content, ""); content != "" {
31+
edits = append(edits, diff.TextEdit{Span: s, NewText: content})
32+
}
33+
}
34+
}
35+
return edits
36+
}
37+
38+
type operation struct {
1939
Kind diff.OpKind
2040
Content []string // content from b
2141
I1, I2 int // indices of the line in a
2242
J1 int // indices of the line in b, J2 implied by len(Content)
2343
}
2444

25-
func ApplyEdits(a []string, operations []*Op) []string {
26-
var b []string
27-
var prevI2 int
28-
for _, op := range operations {
29-
// catch up to latest indices
30-
if op.I1-prevI2 > 0 {
31-
for _, c := range a[prevI2:op.I1] {
32-
b = append(b, c)
33-
}
34-
}
35-
switch op.Kind {
36-
case diff.Equal, diff.Insert:
37-
b = append(b, op.Content...)
38-
}
39-
prevI2 = op.I2
40-
}
41-
// final catch up
42-
if len(a)-prevI2 > 0 {
43-
for _, c := range a[prevI2:len(a)] {
44-
b = append(b, c)
45-
}
46-
}
47-
return b
48-
}
49-
50-
// Operations returns the list of operations to convert a into b, consolidating
45+
// operations returns the list of operations to convert a into b, consolidating
5146
// operations for multiple lines and not including equal lines.
52-
func Operations(a, b []string) []*Op {
47+
func operations(a, b []string) []*operation {
5348
if len(a) == 0 && len(b) == 0 {
5449
return nil
5550
}
@@ -60,9 +55,9 @@ func Operations(a, b []string) []*Op {
6055
M, N := len(a), len(b)
6156

6257
var i int
63-
solution := make([]*Op, len(a)+len(b))
58+
solution := make([]*operation, len(a)+len(b))
6459

65-
add := func(op *Op, i2, j2 int) {
60+
add := func(op *operation, i2, j2 int) {
6661
if op == nil {
6762
return
6863
}
@@ -78,11 +73,11 @@ func Operations(a, b []string) []*Op {
7873
if len(snake) < 2 {
7974
continue
8075
}
81-
var op *Op
76+
var op *operation
8277
// delete (horizontal)
8378
for snake[0]-snake[1] > x-y {
8479
if op == nil {
85-
op = &Op{
80+
op = &operation{
8681
Kind: diff.Delete,
8782
I1: x,
8883
J1: y,
@@ -98,7 +93,7 @@ func Operations(a, b []string) []*Op {
9893
// insert (vertical)
9994
for snake[0]-snake[1] < x-y {
10095
if op == nil {
101-
op = &Op{
96+
op = &operation{
10297
Kind: diff.Insert,
10398
I1: x,
10499
J1: y,
@@ -201,7 +196,7 @@ func shortestEditSequence(a, b []string) ([][]int, int) {
201196
return nil, 0
202197
}
203198

204-
func SplitLines(text string) []string {
199+
func splitLines(text string) []string {
205200
lines := strings.SplitAfter(text, "\n")
206201
if lines[len(lines)-1] == "" {
207202
lines = lines[:len(lines)-1]

internal/lsp/diff/myers/myers.go

-62
This file was deleted.

0 commit comments

Comments
 (0)