Skip to content

Commit 53ad329

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/lsp/source: move edit logic into the protocol package
Change-Id: I8a2dcafc109bf298b6edfe7a7029f8b9098880d6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/543920 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Findley <rfindley@google.com>
1 parent 3c677e3 commit 53ad329

File tree

14 files changed

+80
-75
lines changed

14 files changed

+80
-75
lines changed

gopls/internal/cmd/cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"golang.org/x/tools/gopls/internal/lsp/filecache"
3030
"golang.org/x/tools/gopls/internal/lsp/lsprpc"
3131
"golang.org/x/tools/gopls/internal/lsp/protocol"
32-
"golang.org/x/tools/gopls/internal/lsp/source"
3332
"golang.org/x/tools/gopls/internal/settings"
3433
"golang.org/x/tools/internal/constraints"
3534
"golang.org/x/tools/internal/diff"
@@ -585,7 +584,7 @@ func applyTextEdits(mapper *protocol.Mapper, edits []protocol.TextEdit, flags *E
585584
if len(edits) == 0 {
586585
return nil
587586
}
588-
newContent, renameEdits, err := source.ApplyProtocolEdits(mapper, edits)
587+
newContent, renameEdits, err := protocol.ApplyEdits(mapper, edits)
589588
if err != nil {
590589
return err
591590
}

gopls/internal/lsp/cache/mod_tidy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func switchDirectness(req *modfile.Require, m *protocol.Mapper, computeEdits set
457457
}
458458
// Calculate the edits to be made due to the change.
459459
edits := computeEdits(string(m.Content), string(newContent))
460-
return ToProtocolEdits(m, edits)
460+
return protocol.EditsFromDiffEdits(m, edits)
461461
}
462462

463463
// missingModuleForImport creates an error for a given import path that comes

gopls/internal/lsp/cache/pkg.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ var (
7575
RemoveIntermediateTestVariants = source.RemoveIntermediateTestVariants
7676
IsCommandLineArguments = source.IsCommandLineArguments
7777
BundleQuickFixes = source.BundleQuickFixes
78-
ToProtocolEdits = source.ToProtocolEdits
7978
NewFilterer = source.NewFilterer
8079
)
8180

gopls/internal/lsp/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func dropDependency(snapshot source.Snapshot, pm *source.ParsedModule, modulePat
471471
}
472472
// Calculate the edits to be made due to the change.
473473
diff := snapshot.Options().ComputeEdits(string(pm.Mapper.Content), string(newContent))
474-
return source.ToProtocolEdits(pm.Mapper, diff)
474+
return protocol.EditsFromDiffEdits(pm.Mapper, diff)
475475
}
476476

477477
func (c *commandHandler) Test(ctx context.Context, uri protocol.DocumentURI, tests, benchmarks []string) error {
@@ -677,7 +677,7 @@ func collectFileEdits(ctx context.Context, snapshot *cache.Snapshot, uri protoco
677677

678678
m := protocol.NewMapper(fh.URI(), oldContent)
679679
diff := snapshot.Options().ComputeEdits(string(oldContent), string(newContent))
680-
edits, err := source.ToProtocolEdits(m, diff)
680+
edits, err := protocol.EditsFromDiffEdits(m, diff)
681681
if err != nil {
682682
return nil, err
683683
}

gopls/internal/lsp/fake/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package fake
66

77
import (
88
"golang.org/x/tools/gopls/internal/lsp/protocol"
9-
"golang.org/x/tools/gopls/internal/lsp/source"
109
"golang.org/x/tools/internal/diff"
1110
)
1211

@@ -36,7 +35,7 @@ func EditToChangeEvent(e protocol.TextEdit) protocol.TextDocumentContentChangeEv
3635
// and returns a new slice containing the lines of the patched file.
3736
// It is a wrapper around diff.Apply; see that function for preconditions.
3837
func applyEdits(mapper *protocol.Mapper, edits []protocol.TextEdit, windowsLineEndings bool) ([]byte, error) {
39-
diffEdits, err := source.FromProtocolEdits(mapper, edits)
38+
diffEdits, err := protocol.EditsToDiffEdits(mapper, edits)
4039
if err != nil {
4140
return nil, err
4241
}

gopls/internal/lsp/mod/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ func Format(ctx context.Context, snapshot source.Snapshot, fh file.Handle) ([]pr
2727
}
2828
// Calculate the edits to be made due to the change.
2929
diffs := snapshot.Options().ComputeEdits(string(pm.Mapper.Content), string(formatted))
30-
return source.ToProtocolEdits(pm.Mapper, diffs)
30+
return protocol.EditsFromDiffEdits(pm.Mapper, diffs)
3131
}

gopls/internal/lsp/protocol/edits.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package protocol
6+
7+
import "golang.org/x/tools/internal/diff"
8+
9+
// EditsFromDiffEdits converts diff.Edits to a non-nil slice of LSP TextEdits.
10+
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEditArray
11+
func EditsFromDiffEdits(m *Mapper, edits []diff.Edit) ([]TextEdit, error) {
12+
// LSP doesn't require TextEditArray to be sorted:
13+
// this is the receiver's concern. But govim, and perhaps
14+
// other clients have historically relied on the order.
15+
edits = append([]diff.Edit(nil), edits...)
16+
diff.SortEdits(edits)
17+
18+
result := make([]TextEdit, len(edits))
19+
for i, edit := range edits {
20+
rng, err := m.OffsetRange(edit.Start, edit.End)
21+
if err != nil {
22+
return nil, err
23+
}
24+
result[i] = TextEdit{
25+
Range: rng,
26+
NewText: edit.New,
27+
}
28+
}
29+
return result, nil
30+
}
31+
32+
// EditsToDiffEdits converts LSP TextEdits to diff.Edits.
33+
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEditArray
34+
func EditsToDiffEdits(m *Mapper, edits []TextEdit) ([]diff.Edit, error) {
35+
if edits == nil {
36+
return nil, nil
37+
}
38+
result := make([]diff.Edit, len(edits))
39+
for i, edit := range edits {
40+
start, end, err := m.RangeOffsets(edit.Range)
41+
if err != nil {
42+
return nil, err
43+
}
44+
result[i] = diff.Edit{
45+
Start: start,
46+
End: end,
47+
New: edit.NewText,
48+
}
49+
}
50+
return result, nil
51+
}
52+
53+
// ApplyEdits applies the patch (edits) to m.Content and returns the result.
54+
// It also returns the edits converted to diff-package form.
55+
func ApplyEdits(m *Mapper, edits []TextEdit) ([]byte, []diff.Edit, error) {
56+
diffEdits, err := EditsToDiffEdits(m, edits)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
out, err := diff.ApplyBytes(m.Content, diffEdits)
61+
return out, diffEdits, err
62+
}

gopls/internal/lsp/regtest/marker.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"golang.org/x/tools/gopls/internal/lsp/lsprpc"
3434
"golang.org/x/tools/gopls/internal/lsp/protocol"
3535
"golang.org/x/tools/gopls/internal/lsp/safetoken"
36-
"golang.org/x/tools/gopls/internal/lsp/source"
3736
"golang.org/x/tools/gopls/internal/lsp/tests"
3837
"golang.org/x/tools/gopls/internal/lsp/tests/compare"
3938
"golang.org/x/tools/internal/diff"
@@ -1675,9 +1674,7 @@ func acceptCompletionMarker(mark marker, src protocol.Location, label string, go
16751674
}
16761675
filename := mark.path()
16771676
mapper := mark.mapper()
1678-
patched, _, err := source.ApplyProtocolEdits(mapper, append([]protocol.TextEdit{
1679-
*selected.TextEdit,
1680-
}, selected.AdditionalTextEdits...))
1677+
patched, _, err := protocol.ApplyEdits(mapper, append([]protocol.TextEdit{*selected.TextEdit}, selected.AdditionalTextEdits...))
16811678

16821679
if err != nil {
16831680
mark.errorf("ApplyProtocolEdits failed: %v", err)
@@ -1739,7 +1736,7 @@ func foldingRangeMarker(mark marker, g *Golden) {
17391736
mark.errorf("Editor.Mapper(%s) failed: %v", filename, err)
17401737
return
17411738
}
1742-
got, _, err := source.ApplyProtocolEdits(mapper, edits)
1739+
got, _, err := protocol.ApplyEdits(mapper, edits)
17431740
if err != nil {
17441741
mark.errorf("ApplyProtocolEdits failed: %v", err)
17451742
return
@@ -1766,7 +1763,7 @@ func formatMarker(mark marker, golden *Golden) {
17661763
mark.errorf("Editor.Mapper(%s) failed: %v", filename, err)
17671764
}
17681765

1769-
got, _, err = source.ApplyProtocolEdits(mapper, edits)
1766+
got, _, err = protocol.ApplyEdits(mapper, edits)
17701767
if err != nil {
17711768
mark.errorf("ApplyProtocolEdits failed: %v", err)
17721769
return
@@ -2008,7 +2005,7 @@ func applyDocumentChanges(env *Env, changes []protocol.DocumentChanges, fileChan
20082005
if err != nil {
20092006
return err
20102007
}
2011-
patched, _, err := source.ApplyProtocolEdits(mapper, change.TextDocumentEdit.Edits)
2008+
patched, _, err := protocol.ApplyEdits(mapper, change.TextDocumentEdit.Edits)
20122009
if err != nil {
20132010
return err
20142011
}
@@ -2400,7 +2397,7 @@ func inlayhintsMarker(mark marker, g *Golden) {
24002397
}
24012398

24022399
m := mark.mapper()
2403-
got, _, err := source.ApplyProtocolEdits(m, edits)
2400+
got, _, err := protocol.ApplyEdits(m, edits)
24042401
if err != nil {
24052402
mark.errorf("ApplyProtocolEdits: %v", err)
24062403
return

gopls/internal/lsp/source/change_quote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func ConvertStringLiteral(pgf *ParsedGoFile, fh file.Handle, rng protocol.Range)
6969
End: end,
7070
New: newText,
7171
}}
72-
pedits, err := ToProtocolEdits(pgf.Mapper, edits)
72+
pedits, err := protocol.EditsFromDiffEdits(pgf.Mapper, edits)
7373
if err != nil {
7474
bug.Reportf("failed to convert diff.Edit to protocol.TextEdit:%v", err)
7575
return protocol.CodeAction{}, false

gopls/internal/lsp/source/change_signature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
164164
}
165165
edits := diff.Bytes(before, after)
166166
mapper := protocol.NewMapper(uri, before)
167-
pedits, err := ToProtocolEdits(mapper, edits)
167+
pedits, err := protocol.EditsFromDiffEdits(mapper, edits)
168168
if err != nil {
169169
return nil, fmt.Errorf("computing edits for %s: %v", uri, err)
170170
}

0 commit comments

Comments
 (0)