Skip to content

Commit 1cc6d1e

Browse files
rhnvrmstamblerre
authored andcommitted
tools/gopls: add cmd support for prepare_rename
This change adds command line support for prepare_rename. Updates golang/go#32875 Change-Id: I7f155b9c8329c0faa26a320abab162730a7916ad GitHub-Last-Rev: 118e846 GitHub-Pull-Request: #188 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207579 Run-TryBot: Rebecca Stambler <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 59e2469 commit 1cc6d1e

File tree

7 files changed

+134
-7
lines changed

7 files changed

+134
-7
lines changed

internal/lsp/cmd/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func (app *Application) featureCommands() []tool.Application {
174174
&implementation{app: app},
175175
&imports{app: app},
176176
&links{app: app},
177+
&prepareRename{app: app},
177178
&query{app: app},
178179
&references{app: app},
179180
&rename{app: app},

internal/lsp/cmd/prepare_rename.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2019 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 cmd
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
12+
"golang.org/x/tools/internal/lsp/protocol"
13+
"golang.org/x/tools/internal/span"
14+
"golang.org/x/tools/internal/tool"
15+
)
16+
17+
// prepareRename implements the prepare_rename verb for gopls.
18+
type prepareRename struct {
19+
app *Application
20+
}
21+
22+
func (r *prepareRename) Name() string { return "prepare_rename" }
23+
func (r *prepareRename) Usage() string { return "<position>" }
24+
func (r *prepareRename) ShortHelp() string { return "test validity of a rename operation at location" }
25+
func (r *prepareRename) DetailedHelp(f *flag.FlagSet) {
26+
fmt.Fprint(f.Output(), `
27+
Example:
28+
29+
$ # 1-indexed location (:line:column or :#offset) of the target identifier
30+
$ gopls prepare_rename helper/helper.go:8:6
31+
$ gopls prepare_rename helper/helper.go:#53
32+
33+
gopls prepare_rename flags are:
34+
`)
35+
f.PrintDefaults()
36+
}
37+
38+
func (r *prepareRename) Run(ctx context.Context, args ...string) error {
39+
if len(args) != 1 {
40+
return tool.CommandLineErrorf("prepare_rename expects 1 argument (file)")
41+
}
42+
43+
conn, err := r.app.connect(ctx)
44+
if err != nil {
45+
return err
46+
}
47+
defer conn.terminate(ctx)
48+
49+
from := span.Parse(args[0])
50+
file := conn.AddFile(ctx, from.URI())
51+
if file.err != nil {
52+
return file.err
53+
}
54+
loc, err := file.mapper.Location(from)
55+
if err != nil {
56+
return err
57+
}
58+
p := protocol.PrepareRenameParams{
59+
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
60+
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
61+
Position: loc.Range.Start,
62+
},
63+
}
64+
result, err := conn.PrepareRename(ctx, &p)
65+
if err != nil {
66+
return fmt.Errorf("prepare_rename failed: %v", err)
67+
}
68+
if result == nil {
69+
return fmt.Errorf("request is not valid at the given position")
70+
}
71+
72+
resRange, ok := result.(protocol.Range)
73+
if !ok {
74+
return fmt.Errorf("prepare_rename failed to convert result to range, got: %T", result)
75+
}
76+
77+
l := protocol.Location{Range: resRange}
78+
s, err := file.mapper.Span(l)
79+
if err != nil {
80+
return err
81+
}
82+
83+
fmt.Println(s)
84+
return nil
85+
}

internal/lsp/cmd/test/cmdtest.go

-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completi
9595
//TODO: add command line completions tests when it works
9696
}
9797

98-
func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.PrepareItem) {
99-
//TODO: add command line prepare rename tests when it works
100-
}
101-
10298
func (r *runner) RunGoplsCmd(t testing.TB, args ...string) (string, string) {
10399
rStdout, wStdout, err := os.Pipe()
104100
if err != nil {
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2019 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 cmdtest
6+
7+
import (
8+
"fmt"
9+
"testing"
10+
11+
"golang.org/x/tools/internal/lsp/protocol"
12+
"golang.org/x/tools/internal/lsp/source"
13+
"golang.org/x/tools/internal/span"
14+
)
15+
16+
func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.PrepareItem) {
17+
m, err := r.data.Mapper(src.URI())
18+
if err != nil {
19+
t.Errorf("prepare_rename failed: %v", err)
20+
}
21+
22+
var (
23+
target = fmt.Sprintf("%v", src)
24+
args = []string{"prepare_rename", target}
25+
stdOut, stdErr = r.NormalizeGoplsCmd(t, args...)
26+
expect string
27+
)
28+
29+
if want.Text == "" {
30+
if stdErr != "" {
31+
t.Errorf("prepare_rename failed for %s,\nexpected:\n`%v`\ngot:\n`%v`", target, expect, stdErr)
32+
}
33+
return
34+
}
35+
36+
ws, err := m.Span(protocol.Location{Range: want.Range})
37+
if err != nil {
38+
t.Errorf("prepare_rename failed: %v", err)
39+
}
40+
41+
expect = r.Normalize(fmt.Sprintln(ws))
42+
if expect != stdOut {
43+
t.Errorf("prepare_rename failed for %s expected:\n`%s`\ngot:\n`%s`\n", target, expect, stdOut)
44+
}
45+
}

internal/lsp/protocol/tsserver.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/testdata/good/good1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package good //@diag("package", "no_diagnostics", "")
22

33
import (
44
_ "go/ast" //@prepare("go/ast", "_", "_")
5-
"golang.org/x/tools/internal/lsp/types" //@item(types_import, "types", "\"golang.org/x/tools/internal/lsp/types\"", "package"),prepare("types","\"", "types")
5+
"golang.org/x/tools/internal/lsp/types" //@item(types_import, "types", "\"golang.org/x/tools/internal/lsp/types\"", "package")
66
)
77

88
func random() int { //@item(good_random, "random", "func() int", "func")

internal/lsp/testdata/summary.txt.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TypeDefinitionsCount = 2
1616
HighlightsCount = 52
1717
ReferencesCount = 8
1818
RenamesCount = 23
19-
PrepareRenamesCount = 8
19+
PrepareRenamesCount = 7
2020
SymbolsCount = 1
2121
SignaturesCount = 23
2222
LinksCount = 8

0 commit comments

Comments
 (0)