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 (
|
2018-11-17 17:01:23 -07:00
|
|
|
"bytes"
|
2018-11-06 12:04:07 -07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2018-11-14 18:47:28 -07:00
|
|
|
"go/types"
|
2018-11-17 17:01:23 -07:00
|
|
|
"io/ioutil"
|
2018-11-06 12:04:07 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Definition(ctx context.Context, f *File, pos token.Pos) (Range, error) {
|
|
|
|
fAST, err := f.GetAST()
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
pkg, err := f.GetPackage()
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
i, err := findIdentifier(fAST, pos)
|
2018-11-06 12:04:07 -07:00
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
if i.ident == nil {
|
2018-11-06 12:04:07 -07:00
|
|
|
return Range{}, fmt.Errorf("definition was not a valid identifier")
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
obj := pkg.TypesInfo.ObjectOf(i.ident)
|
2018-11-06 12:04:07 -07:00
|
|
|
if obj == nil {
|
|
|
|
return Range{}, fmt.Errorf("no object")
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 17:01:23 -07:00
|
|
|
return objToRange(f.view.Config.Fset, obj), nil
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 15:14:30 -07:00
|
|
|
func TypeDefinition(ctx context.Context, f *File, pos token.Pos) (Range, error) {
|
|
|
|
fAST, err := f.GetAST()
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
pkg, err := f.GetPackage()
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
i, err := findIdentifier(fAST, pos)
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
|
|
|
if i.ident == nil {
|
|
|
|
return Range{}, fmt.Errorf("not a valid identifier")
|
|
|
|
}
|
|
|
|
typ := pkg.TypesInfo.TypeOf(i.ident)
|
|
|
|
if typ == nil {
|
|
|
|
return Range{}, fmt.Errorf("no type for %s", i.ident.Name)
|
|
|
|
}
|
|
|
|
obj := typeToObject(typ)
|
|
|
|
if obj == nil {
|
|
|
|
return Range{}, fmt.Errorf("no object for type %s", typ.String())
|
|
|
|
}
|
|
|
|
return objToRange(f.view.Config.Fset, obj), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func typeToObject(typ types.Type) (obj types.Object) {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Named:
|
|
|
|
obj = typ.Obj()
|
|
|
|
case *types.Pointer:
|
|
|
|
obj = typeToObject(typ.Elem())
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:47:28 -07:00
|
|
|
// ident returns the ident plus any extra information needed
|
|
|
|
type ident struct {
|
|
|
|
ident *ast.Ident
|
|
|
|
wasEmbeddedField bool
|
|
|
|
}
|
|
|
|
|
2018-11-06 12:04:07 -07:00
|
|
|
// findIdentifier returns the ast.Ident for a position
|
|
|
|
// in a file, accounting for a potentially incomplete selector.
|
2018-11-14 18:47:28 -07:00
|
|
|
func findIdentifier(f *ast.File, pos token.Pos) (ident, error) {
|
|
|
|
m, err := checkIdentifier(f, pos)
|
|
|
|
if err != nil {
|
|
|
|
return ident{}, err
|
|
|
|
}
|
|
|
|
if m.ident != nil {
|
|
|
|
return m, nil
|
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.
|
2018-11-14 18:47:28 -07:00
|
|
|
return checkIdentifier(f, 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{}
|
2018-11-06 12:04:07 -07:00
|
|
|
if path == nil {
|
2018-11-14 18:47:28 -07:00
|
|
|
return result, 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
|
|
|
|
}
|
|
|
|
if result.ident != nil {
|
|
|
|
for _, n := range path[1:] {
|
|
|
|
if field, ok := n.(*ast.Field); ok {
|
|
|
|
result.wasEmbeddedField = len(field.Names) == 0
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
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
|
|
|
|
|
|
|
func objToRange(fSet *token.FileSet, obj types.Object) Range {
|
|
|
|
p := obj.Pos()
|
|
|
|
f := fSet.File(p)
|
|
|
|
pos := f.Position(p)
|
|
|
|
if pos.Column == 1 {
|
|
|
|
// Column is 1, so we probably do not have full position information
|
|
|
|
// Currently exportdata does not store the column.
|
|
|
|
// For now we attempt to read the original source and find the identifier
|
|
|
|
// within the line. If we find it we patch the column to match its offset.
|
|
|
|
// TODO: we have probably already added the full data for the file to the
|
|
|
|
// fileset, we ought to track it rather than adding it over and over again
|
|
|
|
// TODO: if we parse from source, we will never need this hack
|
|
|
|
if src, err := ioutil.ReadFile(pos.Filename); err == nil {
|
|
|
|
newF := fSet.AddFile(pos.Filename, -1, len(src))
|
|
|
|
newF.SetLinesForContent(src)
|
|
|
|
lineStart := lineStart(newF, pos.Line)
|
|
|
|
offset := newF.Offset(lineStart)
|
|
|
|
col := bytes.Index(src[offset:], []byte(obj.Name()))
|
|
|
|
p = newF.Pos(offset + col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Range{
|
|
|
|
Start: p,
|
|
|
|
End: p + token.Pos(len([]byte(obj.Name()))), // TODO: use real range of obj
|
|
|
|
}
|
|
|
|
}
|