github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/refactor/rename/util14.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !go1.5
     6  
     7  package rename
     8  
     9  import (
    10  	"go/ast"
    11  	"os"
    12  	"path/filepath"
    13  	"reflect"
    14  	"runtime"
    15  	"strings"
    16  	"unicode"
    17  
    18  	"golang.org/x/tools/go/ast/astutil"
    19  	"golang.org/x/tools/go/types"
    20  )
    21  
    22  func objectKind(obj types.Object) string {
    23  	switch obj := obj.(type) {
    24  	case *types.PkgName:
    25  		return "imported package name"
    26  	case *types.TypeName:
    27  		return "type"
    28  	case *types.Var:
    29  		if obj.IsField() {
    30  			return "field"
    31  		}
    32  	case *types.Func:
    33  		if obj.Type().(*types.Signature).Recv() != nil {
    34  			return "method"
    35  		}
    36  	}
    37  	// label, func, var, const
    38  	return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types."))
    39  }
    40  
    41  func typeKind(T types.Type) string {
    42  	return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(T.Underlying()).String(), "*types."))
    43  }
    44  
    45  // NB: for renamings, blank is not considered valid.
    46  func isValidIdentifier(id string) bool {
    47  	if id == "" || id == "_" {
    48  		return false
    49  	}
    50  	for i, r := range id {
    51  		if !isLetter(r) && (i == 0 || !isDigit(r)) {
    52  			return false
    53  		}
    54  	}
    55  	return true
    56  }
    57  
    58  // isLocal reports whether obj is local to some function.
    59  // Precondition: not a struct field or interface method.
    60  func isLocal(obj types.Object) bool {
    61  	// [... 5=stmt 4=func 3=file 2=pkg 1=universe]
    62  	var depth int
    63  	for scope := obj.Parent(); scope != nil; scope = scope.Parent() {
    64  		depth++
    65  	}
    66  	return depth >= 4
    67  }
    68  
    69  func isPackageLevel(obj types.Object) bool {
    70  	return obj.Pkg().Scope().Lookup(obj.Name()) == obj
    71  }
    72  
    73  // -- Plundered from go/scanner: ---------------------------------------
    74  
    75  func isLetter(ch rune) bool {
    76  	return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
    77  }
    78  
    79  func isDigit(ch rune) bool {
    80  	return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch)
    81  }
    82  
    83  // -- Plundered from golang.org/x/tools/oracle -----------------
    84  
    85  // sameFile returns true if x and y have the same basename and denote
    86  // the same file.
    87  //
    88  func sameFile(x, y string) bool {
    89  	if runtime.GOOS == "windows" {
    90  		x = filepath.ToSlash(x)
    91  		y = filepath.ToSlash(y)
    92  	}
    93  	if x == y {
    94  		return true
    95  	}
    96  	if filepath.Base(x) == filepath.Base(y) { // (optimisation)
    97  		if xi, err := os.Stat(x); err == nil {
    98  			if yi, err := os.Stat(y); err == nil {
    99  				return os.SameFile(xi, yi)
   100  			}
   101  		}
   102  	}
   103  	return false
   104  }
   105  
   106  func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }