2014-09-23 08:23:04 -06:00
|
|
|
// The gorename command performs precise type-safe renaming of
|
|
|
|
// identifiers in Go source code. See the -help message or Usage
|
|
|
|
// constant for details.
|
2014-12-08 21:00:58 -07:00
|
|
|
package main // import "golang.org/x/tools/cmd/gorename"
|
2014-09-23 08:23:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/refactor/rename"
|
2014-09-23 08:23:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
offsetFlag = flag.String("offset", "", "file and byte offset of identifier to be renamed, e.g. 'file.go:#123'. For use by editors.")
|
|
|
|
fromFlag = flag.String("from", "", "identifier to be renamed; see -help for formats")
|
|
|
|
toFlag = flag.String("to", "", "new name for identifier")
|
|
|
|
helpFlag = flag.Bool("help", false, "show usage message")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&rename.Force, "force", false, "proceed, even if conflicts were reported")
|
|
|
|
flag.BoolVar(&rename.DryRun, "dryrun", false, "show the change, but do not apply it")
|
|
|
|
flag.BoolVar(&rename.Verbose, "v", false, "print verbose information")
|
|
|
|
|
|
|
|
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
|
|
|
|
// For small machines, use at least 4 threads.
|
|
|
|
if os.Getenv("GOMAXPROCS") == "" {
|
|
|
|
n := runtime.NumCPU()
|
|
|
|
if n < 4 {
|
|
|
|
n = 4
|
|
|
|
}
|
|
|
|
runtime.GOMAXPROCS(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Usage = `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.
|
|
|
|
` + rename.FromFlagUsage + `
|
|
|
|
|
|
|
|
-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.)
|
|
|
|
|
2014-09-23 13:17:49 -06:00
|
|
|
-dryrun causes the tool to report conflicts but not update any files.
|
|
|
|
|
2014-09-23 08:23:04 -06:00
|
|
|
-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).
|
|
|
|
|
cmd/gorename: support renaming of methods with consequences for other types, iff initiated at an abstract method.
Previously, gorename rejected all method renamings if it would
change the assignability relation.
Now, so long as the renaming was initiated at an abstract
method, the renaming proceeds, changing concrete methods (and
possibly other abstract methods) as needed. The user
intention is clear.
The intention of a renaming initiated at a concrete method is
less clear, so we still reject it if it would change the
assignability relation. The diagnostic advises the user to
rename the abstract method if that was the intention.
Additional safety checks are required: for each
satisfy.Constraint that couples a concrete type C and an
interface type I, we must treat it just like a set of implicit
selections C.f, one per abstract method f of I, and ensure the
selections' meanings are unchanged.
The satisfy package no longer canonicalizes types, since this
substitutes one interface for another (equivalent) one, which
is sound, but makes the type names random and the error
messages confusing.
Also, fixed a bug in 'satisfy' relating to map keys.
+ Lots more tests.
LGTM=sameer
R=sameer
CC=golang-codereviews
https://golang.org/cl/173430043
2014-12-04 07:37:50 -07:00
|
|
|
gorename rejects renamings of concrete methods that would change the
|
|
|
|
assignability relation between types and interfaces. If the interface
|
|
|
|
change was intentional, initiate the renaming at the interface method.
|
|
|
|
|
2014-09-23 08:23:04 -06:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
% gorename -offset file.go:#123 -to foo
|
|
|
|
|
2014-09-23 13:17:49 -06:00
|
|
|
Rename the object whose identifier is at byte offset 123 within file file.go.
|
2014-09-23 08:23:04 -06:00
|
|
|
|
2014-10-31 13:39:22 -06:00
|
|
|
% gorename -from '"bytes".Buffer.Len' -to Size
|
2014-09-23 08:23:04 -06:00
|
|
|
|
|
|
|
Rename the "Len" method of the *bytes.Buffer type to "Size".
|
|
|
|
|
|
|
|
---- TODO ----
|
|
|
|
|
|
|
|
Correctness:
|
|
|
|
- handle dot imports correctly
|
|
|
|
- document limitations (reflection, 'implements' guesswork).
|
|
|
|
- sketch a proof of exhaustiveness.
|
|
|
|
|
|
|
|
Features:
|
2014-10-31 13:39:22 -06:00
|
|
|
- support running on packages specified as *.go files on the command line
|
2014-09-23 08:23:04 -06:00
|
|
|
- support running on programs containing errors (loader.Config.AllowErrors)
|
|
|
|
- allow users to specify a scope other than "global" (to avoid being
|
|
|
|
stuck by neglected packages in $GOPATH that don't build).
|
|
|
|
- support renaming the package clause (no object)
|
|
|
|
- support renaming an import path (no ident or object)
|
|
|
|
(requires filesystem + SCM updates).
|
|
|
|
- detect and reject edits to autogenerated files (cgo, protobufs)
|
|
|
|
and optionally $GOROOT packages.
|
|
|
|
- report all conflicts, or at least all qualitatively distinct ones.
|
|
|
|
Sometimes we stop to avoid redundancy, but
|
|
|
|
it may give a disproportionate sense of safety in -force mode.
|
|
|
|
- support renaming all instances of a pattern, e.g.
|
|
|
|
all receiver vars of a given type,
|
|
|
|
all local variables of a given type,
|
|
|
|
all PkgNames for a given package.
|
|
|
|
- emit JSON output for other editors and tools.
|
|
|
|
`
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) > 0 {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintln(os.Stderr, "gorename: surplus arguments.")
|
2014-09-23 08:23:04 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *helpFlag || (*offsetFlag == "" && *fromFlag == "" && *toFlag == "") {
|
|
|
|
fmt.Println(Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rename.Main(&build.Default, *offsetFlag, *fromFlag, *toFlag); err != nil {
|
|
|
|
if err != rename.ConflictError {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "gorename: %s\n", err)
|
2014-09-23 08:23:04 -06:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|