diff --git a/refactor/rename/spec.go b/refactor/rename/spec.go index 264b3bd0..d8d320a6 100644 --- a/refactor/rename/spec.go +++ b/refactor/rename/spec.go @@ -115,12 +115,12 @@ func parseFromFlag(ctxt *build.Context, fromFlag string) (*spec, error) { if !buildutil.FileExists(ctxt, spec.filename) { return nil, fmt.Errorf("no such file: %s", spec.filename) } - // Guess the default package. - var err error - if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil { - return nil, fmt.Errorf("-from: couldn't guess package from filename: %s: %s", - spec.filename, err) + + bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename) + if err != nil { + return nil, err } + spec.pkg = bp.ImportPath } else if a, b := splitAtLastDot(main); b == "" { // importpath e.g. "encoding/json" @@ -194,12 +194,12 @@ func parseOffsetFlag(ctxt *build.Context, offsetFlag string) (*spec, error) { if !buildutil.FileExists(ctxt, spec.filename) { return nil, fmt.Errorf("no such file: %s", spec.filename) } - // Guess the default package. - var err error - if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil { - return nil, fmt.Errorf("couldn't guess package from filename: %s: %s", - spec.filename, err) + + bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename) + if err != nil { + return nil, err } + spec.pkg = bp.ImportPath for _, r := range parts[1] { if !isDigit(r) { diff --git a/refactor/rename/util.go b/refactor/rename/util.go index 07b3767c..aa13908d 100644 --- a/refactor/rename/util.go +++ b/refactor/rename/util.go @@ -5,8 +5,6 @@ package rename import ( - "fmt" - "go/build" "os" "path/filepath" "reflect" @@ -79,61 +77,6 @@ func isDigit(ch rune) bool { // -- Plundered from code.google.com/p/go.tools/oracle ----------------- -// guessImportPath finds the package containing filename, and returns -// its import path relative to it. -func guessImportPath(filename string, ctxt *build.Context) (importPath string, err error) { - // TODO(adonovan): move this to package "buildutil"; factor in common with oracle. - // bp, err := buildutil.ContainingPackage(ctxt, wd, filename) - // if err != nil { - // return - // } - // return bp.ImportPath, nil - - absFile, err := filepath.Abs(filename) - if err != nil { - err = fmt.Errorf("can't form absolute path of %s", filename) - return - } - absFileDir := segments(filepath.Dir(absFile)) - - // Find the innermost directory in $GOPATH that encloses filename. - minD := 1024 - for _, gopathDir := range ctxt.SrcDirs() { - // We can assume $GOPATH and $GOROOT dirs are absolute, - // thus gopathDir too, and that it exists. - d := prefixLen(segments(gopathDir), absFileDir) - // If there are multiple matches, - // prefer the innermost enclosing directory - // (smallest d). - if d >= 0 && d < minD { - minD = d - importPath = strings.Join(absFileDir[len(absFileDir)-minD:], string(os.PathSeparator)) - } - } - if importPath == "" { - err = fmt.Errorf("can't find package for file %s", filename) - } - return -} - -func segments(path string) []string { - return strings.Split(path, string(os.PathSeparator)) -} - -// prefixLen returns the length of the remainder of y if x is a prefix -// of y, a negative number otherwise. -func prefixLen(x, y []string) int { - d := len(y) - len(x) - if d >= 0 { - for i := range x { - if y[i] != x[i] { - return -1 // not a prefix - } - } - } - return d -} - // sameFile returns true if x and y have the same basename and denote // the same file. //