rename - ActiveState ActiveGo 1.8
...

Package rename

import "golang.org/x/tools/refactor/rename"
Overview
Index

Overview ▾

Package rename contains the implementation of the 'gorename' command whose main function is in golang.org/x/tools/cmd/gorename. See the Usage constant for the command documentation.

Constants

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.
           A legal -from query has one of the following forms:

  "encoding/json".Decoder.Decode        method of package-level named type
  (*"encoding/json".Decoder).Decode     ditto, alternative syntax
  "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

           Double-quotes must be escaped when writing a shell command.
           Quotes may be omitted for single-segment import paths such as "fmt".

           For methods, the parens and '*' on the receiver type are both
           optional.

           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.)

-d         display diffs instead of rewriting 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 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.

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

  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".

---- TODO ----

Correctness:
- handle dot imports correctly
- document limitations (reflection, 'implements' algorithm).
- sketch a proof of exhaustiveness.

Features:
- support running on packages specified as *.go files on the command line
- 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.
`

Variables

var (
    // Force enables patching of the source files even if conflicts were reported.
    // The resulting program may be ill-formed.
    // It may even cause gorename to crash.  TODO(adonovan): fix that.
    Force bool

    // Diff causes the tool to display diffs instead of rewriting files.
    Diff bool

    // DiffCmd specifies the diff command used by the -d feature.
    // (The command must accept a -u flag and two filename arguments.)
    DiffCmd = "diff"

    // ConflictError is returned by Main when it aborts the renaming due to conflicts.
    // (It is distinguished because the interesting errors are the conflicts themselves.)
    ConflictError = errors.New("renaming aborted due to conflicts")

    // Verbose enables extra logging.
    Verbose bool
)

func Main

func Main(ctxt *build.Context, offsetFlag, fromFlag, to string) error

func Move

func Move(ctxt *build.Context, from, to, moveTmpl string) error

Move, given a package path and a destination package path, will try to move the given package to the new path. The Move function will first check for any conflicts preventing the move, such as a package already existing at the destination package path. If the move can proceed, it builds an import graph to find all imports of the packages whose paths need to be renamed. This includes uses of the subpackages of the package to be moved as those packages will also need to be moved. It then renames all imports to point to the new paths, and then moves the packages to their new paths.