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"
|
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-01-23 12:16:43 -07:00
|
|
|
File File
|
|
|
|
Type struct {
|
2019-02-19 19:11:15 -07:00
|
|
|
Range span.Range
|
2019-01-23 12:16:43 -07:00
|
|
|
Object types.Object
|
|
|
|
}
|
|
|
|
Declaration struct {
|
2019-02-19 19:11:15 -07:00
|
|
|
Range span.Range
|
2019-04-17 16:21:47 -06:00
|
|
|
Node ast.Decl
|
2019-01-23 12:16:43 -07:00
|
|
|
Object types.Object
|
2018-12-03 15:14:30 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:47:28 -07:00
|
|
|
ident *ast.Ident
|
|
|
|
wasEmbeddedField bool
|
|
|
|
}
|
|
|
|
|
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-01-23 12:16:43 -07:00
|
|
|
func Identifier(ctx context.Context, v View, f File, pos token.Pos) (*IdentifierInfo, error) {
|
|
|
|
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.
|
|
|
|
func identifier(ctx context.Context, v View, f File, pos token.Pos) (*IdentifierInfo, error) {
|
2019-03-05 15:30:44 -07:00
|
|
|
fAST := f.GetAST(ctx)
|
|
|
|
pkg := f.GetPackage(ctx)
|
2019-04-22 10:49:04 -06:00
|
|
|
if pkg == nil {
|
|
|
|
return nil, fmt.Errorf("no package for %s", f.URI())
|
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
if pkg.IsIllTyped() {
|
|
|
|
return nil, fmt.Errorf("package for %s is ill typed", f.URI())
|
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
path, _ := astutil.PathEnclosingInterval(fAST, pos, pos)
|
|
|
|
result := &IdentifierInfo{
|
|
|
|
File: f,
|
|
|
|
}
|
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
|
|
|
}
|
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-02-19 19:11:15 -07:00
|
|
|
result.Range = span.NewRange(v.FileSet(), result.ident.Pos(), result.ident.End())
|
2019-03-06 14:33:47 -07:00
|
|
|
result.Declaration.Object = pkg.GetTypesInfo().ObjectOf(result.ident)
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.Declaration.Object == nil {
|
|
|
|
return nil, fmt.Errorf("no object for ident %v", result.Name)
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
if v, ok := result.Declaration.Object.(*types.Var); ok {
|
2019-04-19 09:10:38 -06:00
|
|
|
if typObj := typeToObject(v.Type()); typObj != nil {
|
|
|
|
result.Declaration.Object = typObj
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
var err error
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.Declaration.Range, err = objToRange(ctx, v, result.Declaration.Object); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 16:21:47 -06:00
|
|
|
if result.Declaration.Node, err = objToNode(ctx, v, result.Declaration.Object, result.Declaration.Range); err != nil {
|
|
|
|
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.
|
|
|
|
if types.IsInterface(result.Type.Object.Type()) && result.Type.Object.Pkg() == nil && result.Type.Object.Name() == "error" {
|
|
|
|
return result, nil
|
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.Type.Range, err = objToRange(ctx, v, result.Type.Object); err != nil {
|
|
|
|
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-02-19 19:11:15 -07:00
|
|
|
func objToRange(ctx context.Context, v View, obj types.Object) (span.Range, error) {
|
2018-11-17 17:01:23 -07:00
|
|
|
p := obj.Pos()
|
2019-01-11 15:03:01 -07:00
|
|
|
if !p.IsValid() {
|
2019-02-19 19:11:15 -07:00
|
|
|
return span.Range{}, fmt.Errorf("invalid position for %v", obj.Name())
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
return span.NewRange(v.FileSet(), p, p+token.Pos(len(obj.Name()))), nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-04-17 16:21:47 -06:00
|
|
|
|
|
|
|
func objToNode(ctx context.Context, v View, obj types.Object, rng span.Range) (ast.Decl, error) {
|
|
|
|
s, err := rng.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
declFile, err := v.GetFile(ctx, s.URI())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
declAST := declFile.GetAST(ctx)
|
|
|
|
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
|
|
|
}
|