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.
12 "golang.org/x/tools/internal/lsp/protocol"
13 "golang.org/x/tools/internal/span"
14 "golang.org/x/tools/internal/tool"
15 errors "golang.org/x/xerrors"
18 // prepareRename implements the prepare_rename verb for gopls.
19 type prepareRename struct {
23 func (r *prepareRename) Name() string { return "prepare_rename" }
24 func (r *prepareRename) Usage() string { return "<position>" }
25 func (r *prepareRename) ShortHelp() string { return "test validity of a rename operation at location" }
26 func (r *prepareRename) DetailedHelp(f *flag.FlagSet) {
27 fmt.Fprint(f.Output(), `
30 $ # 1-indexed location (:line:column or :#offset) of the target identifier
31 $ gopls prepare_rename helper/helper.go:8:6
32 $ gopls prepare_rename helper/helper.go:#53
34 gopls prepare_rename flags are:
39 // ErrInvalidRenamePosition is returned when prepareRename is run at a position that
40 // is not a candidate for renaming.
41 var ErrInvalidRenamePosition = errors.New("request is not valid at the given position")
43 func (r *prepareRename) Run(ctx context.Context, args ...string) error {
45 return tool.CommandLineErrorf("prepare_rename expects 1 argument (file)")
48 conn, err := r.app.connect(ctx)
52 defer conn.terminate(ctx)
54 from := span.Parse(args[0])
55 file := conn.AddFile(ctx, from.URI())
59 loc, err := file.mapper.Location(from)
63 p := protocol.PrepareRenameParams{
64 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
65 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
66 Position: loc.Range.Start,
69 result, err := conn.PrepareRename(ctx, &p)
71 return errors.Errorf("prepare_rename failed: %w", err)
74 return ErrInvalidRenamePosition
77 l := protocol.Location{Range: *result}
78 s, err := file.mapper.Span(l)