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 5package cmd 6 7import ( 8 "context" 9 "flag" 10 "fmt" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "sort" 15 16 "golang.org/x/tools/internal/lsp/diff" 17 "golang.org/x/tools/internal/lsp/protocol" 18 "golang.org/x/tools/internal/lsp/source" 19 "golang.org/x/tools/internal/span" 20 "golang.org/x/tools/internal/tool" 21 errors "golang.org/x/xerrors" 22) 23 24// rename implements the rename verb for gopls. 25type rename struct { 26 Diff bool `flag:"d" help:"display diffs instead of rewriting files"` 27 Write bool `flag:"w" help:"write result to (source) file instead of stdout"` 28 29 app *Application 30} 31 32func (r *rename) Name() string { return "rename" } 33func (r *rename) Usage() string { return "<position>" } 34func (r *rename) ShortHelp() string { return "rename selected identifier" } 35func (r *rename) DetailedHelp(f *flag.FlagSet) { 36 fmt.Fprint(f.Output(), ` 37Example: 38 39 $ # 1-based location (:line:column or :#position) of the thing to change 40 $ gopls rename helper/helper.go:8:6 41 $ gopls rename helper/helper.go:#53 42 43 gopls rename flags are: 44`) 45 f.PrintDefaults() 46} 47 48// Run renames the specified identifier and either; 49// - if -w is specified, updates the file(s) in place; 50// - if -d is specified, prints out unified diffs of the changes; or 51// - otherwise, prints the new versions to stdout. 52func (r *rename) Run(ctx context.Context, args ...string) error { 53 if len(args) != 2 { 54 return tool.CommandLineErrorf("definition expects 2 arguments (position, new name)") 55 } 56 conn, err := r.app.connect(ctx) 57 if err != nil { 58 return err 59 } 60 defer conn.terminate(ctx) 61 62 from := span.Parse(args[0]) 63 file := conn.AddFile(ctx, from.URI()) 64 if file.err != nil { 65 return file.err 66 } 67 loc, err := file.mapper.Location(from) 68 if err != nil { 69 return err 70 } 71 p := protocol.RenameParams{ 72 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI}, 73 Position: loc.Range.Start, 74 NewName: args[1], 75 } 76 edit, err := conn.Rename(ctx, &p) 77 if err != nil { 78 return err 79 } 80 var orderedURIs []string 81 edits := map[span.URI][]protocol.TextEdit{} 82 for _, c := range edit.DocumentChanges { 83 uri := span.URI(c.TextDocument.URI) 84 edits[uri] = append(edits[uri], c.Edits...) 85 orderedURIs = append(orderedURIs, c.TextDocument.URI) 86 } 87 sort.Strings(orderedURIs) 88 changeCount := len(orderedURIs) 89 90 for _, u := range orderedURIs { 91 uri := span.URI(u) 92 cmdFile := conn.AddFile(ctx, uri) 93 filename := cmdFile.uri.Filename() 94 95 // convert LSP-style edits to []diff.TextEdit cuz Spans are handy 96 renameEdits, err := source.FromProtocolEdits(cmdFile.mapper, edits[uri]) 97 if err != nil { 98 return errors.Errorf("%v: %v", edits, err) 99 } 100 newContent := diff.ApplyEdits(string(cmdFile.mapper.Content), renameEdits) 101 102 switch { 103 case r.Write: 104 fmt.Fprintln(os.Stderr, filename) 105 err := os.Rename(filename, filename+".orig") 106 if err != nil { 107 return errors.Errorf("%v: %v", edits, err) 108 } 109 ioutil.WriteFile(filename, []byte(newContent), 0644) 110 case r.Diff: 111 diffs := diff.ToUnified(filename+".orig", filename, string(cmdFile.mapper.Content), renameEdits) 112 fmt.Print(diffs) 113 default: 114 if len(orderedURIs) > 1 { 115 fmt.Printf("%s:\n", filepath.Base(filename)) 116 } 117 fmt.Print(string(newContent)) 118 if changeCount > 1 { // if this wasn't last change, print newline 119 fmt.Println() 120 } 121 changeCount -= 1 122 } 123 } 124 return nil 125} 126