gorename is a new tool for automatic and sound renaming of Go identifiers.
It reports all declaration and reference conflicts so that, given a well-typed Go program as input, it will either report an informative error, or transform the program to another well-typed and semantically equivalent* Go program.
The entity to rename may be specified by position (e.g. from within an editor) or by logical name. Adding editor support is straightforward, but only Emacs is integrated today; contributions welcome.
Run 'gorename -help' to see documentation, most of which is shown below. The complete message includes caveats/TODOs.
Any suggestions much appreciated.
cheers
alan
* The usual static analysis caveats apply; gorename doesn't understand reflection or comments.
% mv ./gorename $GOROOT/bin
% gorename -help
gorename: precise type-safe renaming of identifiers in Go source code.
Usage:
gorename (-from <spec> | -offset <file>:#<byte-offset>) -to <name> [-force]
You must specify the object (named entity) to rename using the -offset
or -from flag. Exactly one must be specified.
Flags:
-offset specifies the filename and byte offset of an identifier to rename.
This form is intended for use by text editors.
-from specifies the object to rename using a query notation;
This form is intended for interactive use at the command line.
A legal -from query has one of the following forms:
(encoding/json.Decoder).Decode method of package-level named type
(encoding/json.Decoder).buf field of package-level named struct type
encoding/json.HTMLEscape package member (const, func, var, type)
(encoding/json.Decoder).Decode::x local object x within a method
encoding/json.HTMLEscape::x local object x within a function
encoding/json::x object x anywhere within a package
json.go::x object x within file json.go
For methods attached to a pointer type, the '*' must not be specified.
[TODO(adonovan): fix that.]
It is an error if one of the ::x queries matches multiple objects.
-to the new name.
-force causes the renaming to proceed even if conflicts were reported.
The resulting program may be ill-formed, or experience a change
in behaviour.
WARNING: this flag may even cause the renaming tool to crash.
(In due course this bug will be fixed by moving certain
analyses into the type-checker.)
-dryrun causes the tool to report conflicts but not update any files.
-v enables verbose logging.
gorename automatically computes the set of packages that might be
affected. For a local renaming, this is just the package specified by
-from or -offset, but for a potentially exported name, gorename scans
the workspace ($GOROOT and $GOPATH).
gorename rejects any renaming that would create a conflict at the point
of declaration, or a reference conflict (ambiguity or shadowing), or
anything else that could cause the resulting program not to compile.
Currently, it also rejects any method renaming that would change the
assignability relation between types and interfaces.
Examples:
% gorename -offset file.go:#123 -to foo
Rename the object whose identifier is at byte offset 123 within file file.go.
% gorename -from '(bytes.Buffer).Len' -to Size
Rename the "Len" method of the *bytes.Buffer type to "Size".