| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"flag" |
| 10 |
"fmt" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
"golang.org/x/tools/gopls/internal/tool" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type rename struct { |
| 18 |
EditFlags |
| 19 |
app *Application |
| 20 |
} |
| 21 |
|
| 22 |
func (r *rename) Name() string { return "rename" } |
| 23 |
func (r *rename) Parent() string { return r.app.Name() } |
| 24 |
func (r *rename) Usage() string { return "[rename-flags] <position> <name>" } |
| 25 |
func (r *rename) ShortHelp() string { return "rename selected identifier" } |
| 26 |
func (r *rename) DetailedHelp(f *flag.FlagSet) { |
| 27 |
fmt.Fprint(f.Output(), ` |
| 28 |
Example: |
| 29 |
|
| 30 |
$ # 1-based location (:line:column or :#position) of the thing to change |
| 31 |
$ gopls rename helper/helper.go:8:6 Foo |
| 32 |
$ gopls rename helper/helper.go:#53 Foo |
| 33 |
|
| 34 |
rename-flags: |
| 35 |
`) |
| 36 |
printFlagDefaults(f) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func (r *rename) Run(ctx context.Context, args ...string) error { |
| 44 |
if len(args) != 2 { |
| 45 |
return tool.CommandLineErrorf("rename expects 2 arguments (position, new name)") |
| 46 |
} |
| 47 |
r.app.editFlags = &r.EditFlags |
| 48 |
cli, _, err := r.app.connect(ctx) |
| 49 |
if err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
defer cli.terminate(ctx) |
| 53 |
|
| 54 |
from := parseSpan(args[0]) |
| 55 |
file, err := cli.openFile(ctx, from.URI()) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
loc, err := file.spanLocation(from) |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
p := protocol.RenameParams{ |
| 64 |
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc), |
| 65 |
NewName: args[1], |
| 66 |
} |
| 67 |
edit, err := cli.server.Rename(ctx, &p) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
return cli.applyWorkspaceEdit(edit) |
| 72 |
} |