internal/lsp: make Definition handle embedded fields
This change allows it to jump to the type if you are directly on the embedded field when you trigger go to definition. Change-Id: I48825a5a683e69c0714978c76b1d188d40b38c5d Reviewed-on: https://go-review.googlesource.com/c/149615 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
5215be16cd
commit
fc2e60c3c3
|
@ -9,6 +9,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
|
|
||||||
"golang.org/x/tools/go/ast/astutil"
|
"golang.org/x/tools/go/ast/astutil"
|
||||||
)
|
)
|
||||||
|
@ -22,45 +23,73 @@ func Definition(ctx context.Context, f *File, pos token.Pos) (Range, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Range{}, err
|
return Range{}, err
|
||||||
}
|
}
|
||||||
ident, err := findIdentifier(fAST, pos)
|
i, err := findIdentifier(fAST, pos)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Range{}, err
|
return Range{}, err
|
||||||
}
|
}
|
||||||
if ident == nil {
|
if i.ident == nil {
|
||||||
return Range{}, fmt.Errorf("definition was not a valid identifier")
|
return Range{}, fmt.Errorf("definition was not a valid identifier")
|
||||||
}
|
}
|
||||||
obj := pkg.TypesInfo.ObjectOf(ident)
|
obj := pkg.TypesInfo.ObjectOf(i.ident)
|
||||||
if obj == nil {
|
if obj == nil {
|
||||||
return Range{}, fmt.Errorf("no object")
|
return Range{}, fmt.Errorf("no object")
|
||||||
}
|
}
|
||||||
|
if i.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 := obj.(*types.Var); ok {
|
||||||
|
if n, ok := v.Type().(*types.Named); ok {
|
||||||
|
obj = n.Obj()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return Range{
|
return Range{
|
||||||
Start: obj.Pos(),
|
Start: obj.Pos(),
|
||||||
End: obj.Pos() + token.Pos(len([]byte(obj.Name()))), // TODO: use real range of obj
|
End: obj.Pos() + token.Pos(len([]byte(obj.Name()))), // TODO: use real range of obj
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ident returns the ident plus any extra information needed
|
||||||
|
type ident struct {
|
||||||
|
ident *ast.Ident
|
||||||
|
wasEmbeddedField bool
|
||||||
|
}
|
||||||
|
|
||||||
// findIdentifier returns the ast.Ident for a position
|
// findIdentifier returns the ast.Ident for a position
|
||||||
// in a file, accounting for a potentially incomplete selector.
|
// in a file, accounting for a potentially incomplete selector.
|
||||||
func findIdentifier(f *ast.File, pos token.Pos) (*ast.Ident, error) {
|
func findIdentifier(f *ast.File, pos token.Pos) (ident, error) {
|
||||||
path, _ := astutil.PathEnclosingInterval(f, pos, pos)
|
m, err := checkIdentifier(f, pos)
|
||||||
if path == nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't find node enclosing position")
|
return ident{}, err
|
||||||
|
}
|
||||||
|
if m.ident != nil {
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
// If the position is not an identifier but immediately follows
|
// If the position is not an identifier but immediately follows
|
||||||
// an identifier or selector period (as is common when
|
// an identifier or selector period (as is common when
|
||||||
// requesting a completion), use the path to the preceding node.
|
// requesting a completion), use the path to the preceding node.
|
||||||
if ident, ok := path[0].(*ast.Ident); ok {
|
return checkIdentifier(f, pos-1)
|
||||||
return ident, nil
|
|
||||||
}
|
}
|
||||||
path, _ = astutil.PathEnclosingInterval(f, pos-1, pos-1)
|
|
||||||
|
// checkIdentifier checks a single position for a potential identifier.
|
||||||
|
func checkIdentifier(f *ast.File, pos token.Pos) (ident, error) {
|
||||||
|
path, _ := astutil.PathEnclosingInterval(f, pos, pos)
|
||||||
|
result := ident{}
|
||||||
if path == nil {
|
if path == nil {
|
||||||
return nil, nil
|
return result, fmt.Errorf("can't find node enclosing position")
|
||||||
}
|
}
|
||||||
switch prev := path[0].(type) {
|
switch node := path[0].(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
return prev, nil
|
result.ident = node
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
return prev.Sel, nil
|
result.ident = node.Sel
|
||||||
}
|
}
|
||||||
return nil, nil
|
if result.ident != nil {
|
||||||
|
for _, n := range path[1:] {
|
||||||
|
if field, ok := n.(*ast.Field); ok {
|
||||||
|
result.wasEmbeddedField = len(field.Names) == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue