Skip to content

Commit 7f7199c

Browse files
h9jianggopherbot
authored andcommitted
gopls/internal/protocol: patch RenameParams to extend PositionParams
The LSP states that RenameParams extends TextDocumentPositionParams. However, the source json definition flattens the text document and position fields directly to Renameparams, rather than preserving the inheritance. This change manually patches the parsed model ensuring the generated code correctly embeds PositionParams matching the LSP. Change-Id: I265ceab5fc5c7ce08cfca2ceb81cc9a52f0be503 Reviewed-on: https://go-review.googlesource.com/c/tools/+/730040 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Hongxiang Jiang <hxjiang@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 1fe6e2b commit 7f7199c

File tree

7 files changed

+68
-81
lines changed

7 files changed

+68
-81
lines changed

gopls/internal/cmd/rename.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ func (r *rename) Run(ctx context.Context, args ...string) error {
6161
return err
6262
}
6363
p := protocol.RenameParams{
64-
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
65-
Position: loc.Range.Start,
66-
NewName: args[1],
64+
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
65+
NewName: args[1],
6766
}
6867
edit, err := cli.server.Rename(ctx, &p)
6968
if err != nil {

gopls/internal/protocol/generate/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"os/exec"
2222
"path/filepath"
23+
"slices"
2324
"strings"
2425
)
2526

@@ -72,6 +73,28 @@ func processinline() {
7273

7374
model := parse(filepath.Join(*repodir, "protocol/metaModel.json"))
7475

76+
// Although the LSP specification defines RenameParams as extending
77+
// TextDocumentPositionParams, the metaModel.json definition flattens
78+
// these properties (likely due to specific comments in the TS definition).
79+
// See microsoft/vscode-languageserver-node#1698.
80+
//
81+
// TODO(hxjiang): delete the patch logic after releasing lsp 3.18.
82+
for _, s := range model.Structures {
83+
if s.Name == "RenameParams" {
84+
s.Properties = slices.DeleteFunc(s.Properties, func(t NameType) bool {
85+
return t.Name == "position" || t.Name == "textDocument"
86+
})
87+
if !slices.ContainsFunc(s.Extends, func(t *Type) bool {
88+
return t.Kind == "reference" && t.Name == "TextDocumentPositionParams"
89+
}) {
90+
s.Extends = append(s.Extends, &Type{
91+
Kind: "reference",
92+
Name: "TextDocumentPositionParams",
93+
})
94+
}
95+
}
96+
}
97+
7598
findTypeNames(model)
7699
generateOutput(model)
77100

gopls/internal/protocol/generate/output.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ func genCase(model *Model, method string, param, result *Type, dir string) {
122122
// If the parameter extends the TextDocumentPositionParam, verify the
123123
// position is within the provided range.
124124
if extends(nm, "TextDocumentPositionParams") {
125-
out.WriteString(` if params.Range != (Range{}) {
126-
if !params.Range.Contains(params.Position) {
127-
return nil, true, fmt.Errorf("position %%v is outside the provided range %%v.", params.Position, params.Range)
128-
}
125+
out.WriteString(` if !params.Range.Empty() && !params.Range.Contains(params.Position) {
126+
return nil, true, fmt.Errorf("position %%v is outside the provided range %%v.", params.Position, params.Range)
129127
}
130128
`)
131129

gopls/internal/protocol/tsprotocol.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/protocol/tsserver.go

Lines changed: 33 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/test/integration/fake/editor.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,20 +1396,18 @@ func (e *Editor) Rename(ctx context.Context, loc protocol.Location, newName stri
13961396
if e.Server == nil {
13971397
return nil
13981398
}
1399-
path := e.sandbox.Workdir.URIToPath(loc.URI)
14001399

14011400
// Verify that PrepareRename succeeds.
1402-
prepareParams := &protocol.PrepareRenameParams{}
1403-
prepareParams.TextDocument = e.TextDocumentIdentifier(path)
1404-
prepareParams.Position = loc.Range.Start
1401+
prepareParams := &protocol.PrepareRenameParams{
1402+
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
1403+
}
14051404
if _, err := e.Server.PrepareRename(ctx, prepareParams); err != nil {
14061405
return fmt.Errorf("preparing rename: %v", err)
14071406
}
14081407

14091408
params := &protocol.RenameParams{
1410-
TextDocument: e.TextDocumentIdentifier(path),
1411-
Position: loc.Range.Start,
1412-
NewName: newName,
1409+
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
1410+
NewName: newName,
14131411
}
14141412
wsedit, err := e.Server.Rename(ctx, params)
14151413
if err != nil {

gopls/internal/test/marker/marker_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,9 +2043,8 @@ func rename(env *integration.Env, loc protocol.Location, newName string) (map[st
20432043
// @rename markers.
20442044

20452045
wsedit, err := env.Editor.Server.Rename(env.Ctx, &protocol.RenameParams{
2046-
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
2047-
Position: loc.Range.Start,
2048-
NewName: newName,
2046+
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
2047+
NewName: newName,
20492048
})
20502049
if err != nil {
20512050
return nil, err

0 commit comments

Comments
 (0)