2018-11-06 12:04:07 -07:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2018-11-14 18:47:28 -07:00
|
|
|
"go/types"
|
2019-05-10 08:32:25 -06:00
|
|
|
"strconv"
|
2018-11-06 12:04:07 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-11-06 12:04:07 -07:00
|
|
|
)
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
// IdentifierInfo holds information about an identifier in Go source.
|
|
|
|
type IdentifierInfo struct {
|
|
|
|
Name string
|
2019-02-19 19:11:15 -07:00
|
|
|
Range span.Range
|
2019-05-03 22:04:18 -06:00
|
|
|
File GoFile
|
2019-01-23 12:16:43 -07:00
|
|
|
Type struct {
|
2019-02-19 19:11:15 -07:00
|
|
|
Range span.Range
|
2019-01-23 12:16:43 -07:00
|
|
|
Object types.Object
|
|
|
|
}
|
2019-06-04 23:16:23 -06:00
|
|
|
decl declaration
|
2018-12-03 15:14:30 -07:00
|
|
|
|
2018-11-14 18:47:28 -07:00
|
|
|
ident *ast.Ident
|
|
|
|
wasEmbeddedField bool
|
2019-06-04 23:16:23 -06:00
|
|
|
qf types.Qualifier
|
|
|
|
}
|
|
|
|
|
|
|
|
type declaration struct {
|
|
|
|
rng span.Range
|
|
|
|
node ast.Node
|
|
|
|
obj types.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IdentifierInfo) DeclarationRange() span.Range {
|
|
|
|
return i.decl.rng
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
// Identifier returns identifier information for a position
|
2018-11-06 12:04:07 -07:00
|
|
|
// in a file, accounting for a potentially incomplete selector.
|
2019-05-03 22:04:18 -06:00
|
|
|
func Identifier(ctx context.Context, v View, f GoFile, pos token.Pos) (*IdentifierInfo, error) {
|
2019-01-23 12:16:43 -07:00
|
|
|
if result, err := identifier(ctx, v, f, pos); err != nil || result != nil {
|
|
|
|
return result, err
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
|
|
|
// If the position is not an identifier but immediately follows
|
|
|
|
// an identifier or selector period (as is common when
|
|
|
|
// requesting a completion), use the path to the preceding node.
|
2019-01-31 15:29:12 -07:00
|
|
|
result, err := identifier(ctx, v, f, pos-1)
|
|
|
|
if result == nil && err == nil {
|
|
|
|
err = fmt.Errorf("no identifier found")
|
|
|
|
}
|
|
|
|
return result, err
|
2019-01-23 12:16:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// identifier checks a single position for a potential identifier.
|
2019-05-03 22:04:18 -06:00
|
|
|
func identifier(ctx context.Context, v View, f GoFile, pos token.Pos) (*IdentifierInfo, error) {
|
2019-05-20 13:23:02 -06:00
|
|
|
file := f.GetAST(ctx)
|
|
|
|
if file == nil {
|
|
|
|
return nil, fmt.Errorf("no AST for %s", f.URI())
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
pkg := f.GetPackage(ctx)
|
2019-05-10 08:32:25 -06:00
|
|
|
if pkg == nil || pkg.IsIllTyped() {
|
2019-03-11 15:14:55 -06:00
|
|
|
return nil, fmt.Errorf("package for %s is ill typed", f.URI())
|
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2019-05-20 13:23:02 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
2018-11-06 12:04:07 -07:00
|
|
|
if path == nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, fmt.Errorf("can't find node enclosing position")
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2019-05-15 15:58:16 -06:00
|
|
|
// Handle import specs separately, as there is no formal position for a package declaration.
|
2019-05-20 13:23:02 -06:00
|
|
|
if result, err := importSpec(f, file, pkg, pos); result != nil || err != nil {
|
2019-05-10 08:32:25 -06:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:16:23 -06:00
|
|
|
result := &IdentifierInfo{
|
|
|
|
File: f,
|
|
|
|
qf: qualifier(file, pkg.GetTypes(), pkg.GetTypesInfo()),
|
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2018-11-14 18:47:28 -07:00
|
|
|
switch node := path[0].(type) {
|
2018-11-06 12:04:07 -07:00
|
|
|
case *ast.Ident:
|
2018-11-14 18:47:28 -07:00
|
|
|
result.ident = node
|
2018-11-06 12:04:07 -07:00
|
|
|
case *ast.SelectorExpr:
|
2018-11-14 18:47:28 -07:00
|
|
|
result.ident = node.Sel
|
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.ident == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
for _, n := range path[1:] {
|
|
|
|
if field, ok := n.(*ast.Field); ok {
|
|
|
|
result.wasEmbeddedField = len(field.Names) == 0
|
2019-04-19 09:10:38 -06:00
|
|
|
break
|
2019-01-23 12:16:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.Name = result.ident.Name
|
2019-05-17 08:51:19 -06:00
|
|
|
result.Range = span.NewRange(f.FileSet(), result.ident.Pos(), result.ident.End())
|
2019-06-04 23:16:23 -06:00
|
|
|
result.decl.obj = pkg.GetTypesInfo().ObjectOf(result.ident)
|
|
|
|
if result.decl.obj == nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, fmt.Errorf("no object for ident %v", result.Name)
|
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Handle builtins separately.
|
2019-06-04 23:16:23 -06:00
|
|
|
if result.decl.obj.Parent() == types.Universe {
|
2019-05-15 15:58:16 -06:00
|
|
|
decl, ok := lookupBuiltinDecl(f.View(), result.Name).(ast.Node)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no declaration for %s", result.Name)
|
|
|
|
}
|
2019-06-04 23:16:23 -06:00
|
|
|
result.decl.node = decl
|
|
|
|
if result.decl.rng, err = posToRange(ctx, f.FileSet(), result.Name, decl.Pos()); err != nil {
|
2019-05-15 15:58:16 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.wasEmbeddedField {
|
|
|
|
// The original position was on the embedded field declaration, so we
|
|
|
|
// try to dig out the type and jump to that instead.
|
2019-06-04 23:16:23 -06:00
|
|
|
if v, ok := result.decl.obj.(*types.Var); ok {
|
2019-04-19 09:10:38 -06:00
|
|
|
if typObj := typeToObject(v.Type()); typObj != nil {
|
2019-06-04 23:16:23 -06:00
|
|
|
result.decl.obj = typObj
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
2019-06-04 23:16:23 -06:00
|
|
|
if result.decl.rng, err = objToRange(ctx, f.FileSet(), result.decl.obj); err != nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-05 15:44:09 -06:00
|
|
|
if result.decl.node, err = objToNode(ctx, v, pkg.GetTypes(), result.decl.obj, result.decl.rng); err != nil {
|
2019-04-17 16:21:47 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 14:33:47 -07:00
|
|
|
typ := pkg.GetTypesInfo().TypeOf(result.ident)
|
2019-01-23 12:16:43 -07:00
|
|
|
if typ == nil {
|
|
|
|
return nil, fmt.Errorf("no type for %s", result.Name)
|
|
|
|
}
|
|
|
|
result.Type.Object = typeToObject(typ)
|
|
|
|
if result.Type.Object != nil {
|
2019-04-15 14:15:49 -06:00
|
|
|
// Identifiers with the type "error" are a special case with no position.
|
2019-05-15 15:58:16 -06:00
|
|
|
if hasErrorType(result.Type.Object) {
|
2019-04-15 14:15:49 -06:00
|
|
|
return result, nil
|
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
if result.Type.Range, err = objToRange(ctx, f.FileSet(), result.Type.Object); err != nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
return result, nil
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2018-11-17 17:01:23 -07:00
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
func typeToObject(typ types.Type) types.Object {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Named:
|
|
|
|
return typ.Obj()
|
|
|
|
case *types.Pointer:
|
|
|
|
return typeToObject(typ.Elem())
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:58:16 -06:00
|
|
|
func hasErrorType(obj types.Object) bool {
|
|
|
|
return types.IsInterface(obj.Type()) && obj.Pkg() == nil && obj.Name() == "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
func objToRange(ctx context.Context, fset *token.FileSet, obj types.Object) (span.Range, error) {
|
|
|
|
return posToRange(ctx, fset, obj.Name(), obj.Pos())
|
|
|
|
}
|
|
|
|
|
|
|
|
func posToRange(ctx context.Context, fset *token.FileSet, name string, pos token.Pos) (span.Range, error) {
|
|
|
|
if !pos.IsValid() {
|
|
|
|
return span.Range{}, fmt.Errorf("invalid position for %v", name)
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
return span.NewRange(fset, pos, pos+token.Pos(len(name))), nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-04-17 16:21:47 -06:00
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
func objToNode(ctx context.Context, v View, originPkg *types.Package, obj types.Object, rng span.Range) (ast.Decl, error) {
|
2019-04-17 16:21:47 -06:00
|
|
|
s, err := rng.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
f, err := v.GetFile(ctx, s.URI())
|
2019-04-17 16:21:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
declFile, ok := f.(GoFile)
|
|
|
|
if !ok {
|
2019-06-05 15:44:09 -06:00
|
|
|
return nil, fmt.Errorf("%s is not a Go file", s.URI())
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2019-06-05 15:44:09 -06:00
|
|
|
// If the object is exported from a different package,
|
|
|
|
// we don't need its full AST to find the definition.
|
2019-05-23 11:51:56 -06:00
|
|
|
var declAST *ast.File
|
2019-06-05 15:44:09 -06:00
|
|
|
if obj.Exported() && obj.Pkg() != originPkg {
|
|
|
|
declAST = declFile.GetAnyAST(ctx)
|
2019-05-23 11:51:56 -06:00
|
|
|
} else {
|
|
|
|
declAST = declFile.GetAST(ctx)
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
2019-04-17 16:21:47 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(declAST, rng.Start, rng.End)
|
|
|
|
if path == nil {
|
|
|
|
return nil, fmt.Errorf("no path for range %v", rng)
|
|
|
|
}
|
2019-04-19 11:09:33 -06:00
|
|
|
for _, node := range path {
|
|
|
|
switch node := node.(type) {
|
|
|
|
case *ast.GenDecl:
|
|
|
|
// Type names, fields, and methods.
|
|
|
|
switch obj.(type) {
|
|
|
|
case *types.TypeName, *types.Var, *types.Const, *types.Func:
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
// Function signatures.
|
|
|
|
if _, ok := obj.(*types.Func); ok {
|
2019-04-17 16:21:47 -06:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 11:09:33 -06:00
|
|
|
return nil, nil // didn't find a node, but don't fail
|
2019-04-17 16:21:47 -06:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
|
|
|
// importSpec handles positions inside of an *ast.ImportSpec.
|
|
|
|
func importSpec(f GoFile, fAST *ast.File, pkg Package, pos token.Pos) (*IdentifierInfo, error) {
|
|
|
|
for _, imp := range fAST.Imports {
|
|
|
|
if !(imp.Pos() <= pos && pos < imp.End()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
importPath, err := strconv.Unquote(imp.Path.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("import path not quoted: %s (%v)", imp.Path.Value, err)
|
|
|
|
}
|
|
|
|
result := &IdentifierInfo{
|
|
|
|
File: f,
|
|
|
|
Name: importPath,
|
2019-05-17 08:51:19 -06:00
|
|
|
Range: span.NewRange(f.FileSet(), imp.Pos(), imp.End()),
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
|
|
|
// Consider the "declaration" of an import spec to be the imported package.
|
|
|
|
importedPkg := pkg.GetImport(importPath)
|
|
|
|
if importedPkg == nil {
|
|
|
|
return nil, fmt.Errorf("no import for %q", importPath)
|
|
|
|
}
|
|
|
|
if importedPkg.GetSyntax() == nil {
|
|
|
|
return nil, fmt.Errorf("no syntax for for %q", importPath)
|
|
|
|
}
|
|
|
|
// Heuristic: Jump to the longest (most "interesting") file of the package.
|
|
|
|
var dest *ast.File
|
|
|
|
for _, f := range importedPkg.GetSyntax() {
|
|
|
|
if dest == nil || f.End()-f.Pos() > dest.End()-dest.Pos() {
|
|
|
|
dest = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dest == nil {
|
|
|
|
return nil, fmt.Errorf("package %q has no files", importPath)
|
|
|
|
}
|
2019-06-04 23:16:23 -06:00
|
|
|
result.decl.rng = span.NewRange(f.FileSet(), dest.Name.Pos(), dest.Name.End())
|
2019-05-15 15:58:16 -06:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|