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 <rstambler@golang.org>
This commit is contained in:
parent
de4a0b36f1
commit
6eedde5c1d
|
@ -18,7 +18,6 @@ import (
|
||||||
|
|
||||||
"golang.org/x/tools/go/packages/packagestest"
|
"golang.org/x/tools/go/packages/packagestest"
|
||||||
"golang.org/x/tools/internal/lsp/cache"
|
"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/protocol"
|
||||||
"golang.org/x/tools/internal/lsp/source"
|
"golang.org/x/tools/internal/lsp/source"
|
||||||
)
|
)
|
||||||
|
@ -378,30 +377,11 @@ func (f formats) test(t *testing.T, s *server) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
var ops []*diff.Op
|
buf, err := applyEdits(f.GetContent(context.Background()), edits)
|
||||||
for _, edit := range edits {
|
if err != nil {
|
||||||
start := int(edit.Range.Start.Line)
|
t.Error(err)
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
split := strings.SplitAfter(string(f.GetContent(context.Background())), "\n")
|
got := string(buf)
|
||||||
got := strings.Join(diff.ApplyEdits(split, ops), "")
|
|
||||||
if gofmted != got {
|
if gofmted != got {
|
||||||
t.Errorf("format failed for %s: expected '%v', got '%v'", filename, 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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue