1
0
mirror of https://github.com/golang/go synced 2024-09-30 20:18:33 -06:00

internal/lsp: allow the diff alorithm to be specified per view

Change-Id: Ib9d44d2012253189f87bc3b15a88b400f76ae955
Reviewed-on: https://go-review.googlesource.com/c/tools/+/198378
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2019-10-01 18:06:10 -04:00 committed by Brad Fitzpatrick
parent 246a69f4f1
commit 6e0078a899
6 changed files with 19 additions and 14 deletions

View File

@ -7,9 +7,10 @@ package diff_test
import (
"testing"
"golang.org/x/tools/internal/lsp/diff"
"golang.org/x/tools/internal/lsp/diff/difftest"
)
func TestDiff(t *testing.T) {
difftest.DiffTest(t)
difftest.DiffTest(t, diff.MyersComputeEdits)
}

View File

@ -14,7 +14,7 @@ import (
"golang.org/x/tools/internal/span"
)
func DiffTest(t *testing.T) {
func DiffTest(t *testing.T, compute diff.ComputeEdits) {
t.Helper()
for _, test := range []struct{ name, in, out, unified string }{{
name: "empty",
@ -41,7 +41,7 @@ func DiffTest(t *testing.T) {
in: "one\nthree\n",
out: "one\ntwo\nthree\n",
}} {
edits := diff.ComputeEdits(span.FileURI("/"+test.name), test.in, test.out)
edits := compute(span.FileURI("/"+test.name), test.in, test.out)
got := diff.ApplyEdits(test.in, edits)
if got != test.out {
t.Logf("test %v had diff:%v\n", test.name, diff.ToUnified(test.name+".orig", test.name, test.in, edits))

View File

@ -18,9 +18,10 @@ type TextEdit struct {
NewText string
}
type ComputeEdits func(uri span.URI, before, after string) []TextEdit
var (
ComputeEdits func(uri span.URI, before, after string) []TextEdit
ToUnified func(from, to string, before string, edits []TextEdit) string
ToUnified func(from, to string, before string, edits []TextEdit) string
)
func SortTextEdits(d []TextEdit) {

View File

@ -13,11 +13,10 @@ import (
)
func init() {
ComputeEdits = myersComputeEdits
ToUnified = myersToUnified
}
func myersComputeEdits(uri span.URI, before, after string) []TextEdit {
func MyersComputeEdits(uri span.URI, before, after string) []TextEdit {
u := myers.SplitLines(before)
f := myers.SplitLines(after)
return myersDiffToEdits(uri, myers.Operations(u, f))

View File

@ -55,7 +55,7 @@ func Format(ctx context.Context, view View, f File) ([]protocol.TextEdit, error)
if err != nil {
return nil, err
}
return computeTextEdits(ctx, ph.File(), m, string(formatted))
return computeTextEdits(ctx, view, ph.File(), m, string(formatted))
}
fset := view.Session().Cache().FileSet()
@ -68,7 +68,7 @@ func Format(ctx context.Context, view View, f File) ([]protocol.TextEdit, error)
if err := format.Node(buf, fset, file); err != nil {
return nil, err
}
return computeTextEdits(ctx, ph.File(), m, buf.String())
return computeTextEdits(ctx, view, ph.File(), m, buf.String())
}
func formatSource(ctx context.Context, s Snapshot, f File) ([]byte, error) {
@ -134,7 +134,7 @@ func Imports(ctx context.Context, view View, f File) ([]protocol.TextEdit, error
if err != nil {
return nil, err
}
return computeTextEdits(ctx, ph.File(), m, string(formatted))
return computeTextEdits(ctx, view, ph.File(), m, string(formatted))
}
type ImportFix struct {
@ -198,7 +198,7 @@ func AllImportsFixes(ctx context.Context, view View, f File) (edits []protocol.T
if err != nil {
return err
}
edits, err = computeTextEdits(ctx, ph.File(), m, string(formatted))
edits, err = computeTextEdits(ctx, view, ph.File(), m, string(formatted))
if err != nil {
return err
}
@ -209,7 +209,7 @@ func AllImportsFixes(ctx context.Context, view View, f File) (edits []protocol.T
if err != nil {
return err
}
edits, err := computeTextEdits(ctx, ph.File(), m, string(formatted))
edits, err := computeTextEdits(ctx, view, ph.File(), m, string(formatted))
if err != nil {
return err
}
@ -277,7 +277,7 @@ func hasListErrors(errors []packages.Error) bool {
return false
}
func computeTextEdits(ctx context.Context, fh FileHandle, m *protocol.ColumnMapper, formatted string) ([]protocol.TextEdit, error) {
func computeTextEdits(ctx context.Context, view View, fh FileHandle, m *protocol.ColumnMapper, formatted string) ([]protocol.TextEdit, error) {
ctx, done := trace.StartSpan(ctx, "source.computeTextEdits")
defer done()
@ -285,7 +285,7 @@ func computeTextEdits(ctx context.Context, fh FileHandle, m *protocol.ColumnMapp
if err != nil {
return nil, err
}
edits := diff.ComputeEdits(fh.Identity().URI, string(data), formatted)
edits := view.Options().ComputeEdits(fh.Identity().URI, string(data), formatted)
return ToProtocolEdits(m, edits)
}

View File

@ -9,6 +9,7 @@ import (
"os"
"time"
"golang.org/x/tools/internal/lsp/diff"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/telemetry/tag"
errors "golang.org/x/xerrors"
@ -40,6 +41,7 @@ var (
FuzzyMatching: true,
Budget: 100 * time.Millisecond,
},
ComputeEdits: diff.MyersComputeEdits,
}
)
@ -71,6 +73,8 @@ type Options struct {
TextDocumentSyncKind protocol.TextDocumentSyncKind
Completion CompletionOptions
ComputeEdits diff.ComputeEdits
}
type CompletionOptions struct {