From 6eedde5c1db55f93bd1ff37e852bd25f6eeb56f8 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Mon, 11 Mar 2019 16:41:00 -0400 Subject: [PATCH] internal/lsp: using a non line based applyEdits in tests Change-Id: Ibbef0a74b53a95d0b08fae6e8ef24be8b0f78273 Reviewed-on: https://go-review.googlesource.com/c/tools/+/166881 Reviewed-by: Rebecca Stambler --- internal/lsp/lsp_test.go | 48 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 30d04b6dc6d..edde8484efb 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -18,7 +18,6 @@ import ( "golang.org/x/tools/go/packages/packagestest" "golang.org/x/tools/internal/lsp/cache" - "golang.org/x/tools/internal/lsp/diff" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/source" ) @@ -378,30 +377,11 @@ func (f formats) test(t *testing.T, s *server) { if err != nil { t.Error(err) } - var ops []*diff.Op - for _, edit := range edits { - start := int(edit.Range.Start.Line) - end := int(edit.Range.End.Line) - if start == end && edit.Range.End.Character > 1 { - end++ - } - if edit.NewText == "" { // deletion - ops = append(ops, &diff.Op{ - Kind: diff.Delete, - I1: start, - I2: end, - }) - } else if edit.Range.Start == edit.Range.End { // insertion - ops = append(ops, &diff.Op{ - Kind: diff.Insert, - Content: edit.NewText, - I1: start, - I2: end, - }) - } + buf, err := applyEdits(f.GetContent(context.Background()), edits) + if err != nil { + t.Error(err) } - split := strings.SplitAfter(string(f.GetContent(context.Background())), "\n") - got := strings.Join(diff.ApplyEdits(split, ops), "") + got := string(buf) if gofmted != got { t.Errorf("format failed for %s: expected '%v', got '%v'", filename, gofmted, got) } @@ -477,3 +457,23 @@ func TestBytesOffset(t *testing.T) { } } } + +func applyEdits(content []byte, edits []protocol.TextEdit) ([]byte, error) { + prev := 0 + result := make([]byte, 0, len(content)) + for _, edit := range edits { + start := bytesOffset(content, edit.Range.Start) + end := bytesOffset(content, edit.Range.End) + if start > prev { + result = append(result, content[prev:start]...) + } + if len(edit.NewText) > 0 { + result = append(result, []byte(edit.NewText)...) + } + prev = end + } + if prev < len(content) { + result = append(result, content[prev:]...) + } + return result, nil +}