justin8976
justin8976

Reputation: 67

gopls replace variable in the entire project

The question born from the fact that gorename does not support modules. There is a replacement tool called gopls, but the example is not exhaustive so I'm wondering if there is way to rename a variable in the entire project (otherwise I'm failing to understand the usefulness of the rename part)

gopls rename --help says:

Usage: rename [flags]

Example:

gopls rename helper/helper.go:8:6  
gopls rename helper/helper.go:#53

Is there a way to rename a variable in the entire project like gorename is able to do?

Upvotes: 0

Views: 863

Answers (1)

Muir
Muir

Reputation: 442

I recommend you use gopls as an LSP server to integrate with your editor. Your editor should provide an lsp-rename command to conveniently invoke the rename functionality.

As for using gopls rename on the command line, if you have this module:

--- go.mod ---
module foo

--- foo.go ---
package foo
var Foo = 123

Inside the module's directory, run gopls rename -d foo.go:3:5 Hello to preview the diff ("3:5" means line 3, column 5):

% gopls rename -d foo.go:3:5 Hello
--- /Users/muir/scratch/foo/foo.go.orig
+++ /Users/muir/scratch/foo/foo.go
@@ -1,3 +1,3 @@
 package foo

-var Foo = 123
+var Hello = 123

Then run gopls rename -w foo.go:3:5 Hello to write out the changes. Assuming you are using a recent version of gopls, this will rename within the entire module.

Upvotes: 2

Related Questions