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-01-23 12:16:43 -07:00
|
|
|
// IdentifierInfo holds information about an identifier in Go source.
|
|
|
|
type IdentifierInfo struct {
|
|
|
|
Name string
|
|
|
|
Range Range
|
|
|
|
File File
|
|
|
|
Type struct {
|
|
|
|
Range Range
|
|
|
|
Object types.Object
|
|
|
|
}
|
|
|
|
Declaration struct {
|
|
|
|
Range Range
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IdentifierInfo) Hover(q types.Qualifier) (string, error) {
|
|
|
|
if q == nil {
|
|
|
|
fAST, err := i.File.GetAST()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pkg, err := i.File.GetPackage()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
q = qualifier(fAST, pkg.Types, pkg.TypesInfo)
|
|
|
|
}
|
|
|
|
return types.ObjectString(i.Declaration.Object, q), nil
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
fAST, err := f.GetAST()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pkg, err := f.GetPackage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.Name = result.ident.Name
|
|
|
|
result.Range = Range{Start: result.ident.Pos(), End: result.ident.End()}
|
|
|
|
result.Declaration.Object = pkg.TypesInfo.ObjectOf(result.ident)
|
|
|
|
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 {
|
|
|
|
if n, ok := v.Type().(*types.Named); ok {
|
|
|
|
result.Declaration.Object = n.Obj()
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
if result.Declaration.Range, err = objToRange(ctx, v, result.Declaration.Object); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
typ := pkg.TypesInfo.TypeOf(result.ident)
|
|
|
|
if typ == nil {
|
|
|
|
return nil, fmt.Errorf("no type for %s", result.Name)
|
|
|
|
}
|
|
|
|
result.Type.Object = typeToObject(typ)
|
|
|
|
if result.Type.Object != nil {
|
|
|
|
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-01-11 15:03:01 -07:00
|
|
|
func objToRange(ctx context.Context, v View, obj types.Object) (Range, error) {
|
2018-11-17 17:01:23 -07:00
|
|
|
p := obj.Pos()
|
2019-01-11 15:03:01 -07:00
|
|
|
if !p.IsValid() {
|
|
|
|
return Range{}, fmt.Errorf("invalid position for %v", obj.Name())
|
|
|
|
}
|
2018-11-17 17:01:23 -07:00
|
|
|
return Range{
|
|
|
|
Start: p,
|
2018-12-18 13:46:14 -07:00
|
|
|
End: p + token.Pos(identifierLen(obj.Name())),
|
2019-01-11 15:03:01 -07:00
|
|
|
}, nil
|
2018-11-17 17:01:23 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// TODO: This needs to be fixed to address golang.org/issue/29149.
|
|
|
|
func identifierLen(ident string) int {
|
|
|
|
return len([]byte(ident))
|
|
|
|
}
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
// this functionality was borrowed from the analysisutil package
|
|
|
|
func lineStart(f *token.File, line int) token.Pos {
|
|
|
|
// Use binary search to find the start offset of this line.
|
|
|
|
//
|
2019-01-17 09:59:05 -07:00
|
|
|
// TODO(rstambler): eventually replace this function with the
|
2018-12-05 15:00:36 -07:00
|
|
|
// simpler and more efficient (*go/token.File).LineStart, added
|
|
|
|
// in go1.12.
|
|
|
|
|
|
|
|
min := 0 // inclusive
|
|
|
|
max := f.Size() // exclusive
|
|
|
|
for {
|
|
|
|
offset := (min + max) / 2
|
|
|
|
pos := f.Pos(offset)
|
|
|
|
posn := f.Position(pos)
|
|
|
|
if posn.Line == line {
|
|
|
|
return pos - (token.Pos(posn.Column) - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if min+1 >= max {
|
|
|
|
return token.NoPos
|
|
|
|
}
|
|
|
|
|
|
|
|
if posn.Line < line {
|
|
|
|
min = offset
|
|
|
|
} else {
|
|
|
|
max = offset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|