2016-02-11 15:57:17 -07:00
|
|
|
// Copyright 2013 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
|
|
|
|
"golang.org/x/tools/cmd/guru/serial"
|
2016-02-11 20:57:18 -07:00
|
|
|
"golang.org/x/tools/go/loader"
|
2016-02-11 15:57:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// definition reports the location of the definition of an identifier.
|
|
|
|
func definition(q *Query) error {
|
2016-02-15 12:50:53 -07:00
|
|
|
// First try the simple resolution done by parser.
|
|
|
|
// It only works for intra-file references but it is very fast.
|
|
|
|
// (Extending this approach to all the files of the package,
|
|
|
|
// resolved using ast.NewPackage, was not worth the effort.)
|
|
|
|
{
|
2016-03-03 11:42:12 -07:00
|
|
|
qpos, err := fastQueryPos(q.Build, q.Pos)
|
2016-02-15 12:50:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := qpos.path[0].(*ast.Ident)
|
|
|
|
if id == nil {
|
|
|
|
return fmt.Errorf("no identifier here")
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj := id.Obj; obj != nil && obj.Pos().IsValid() {
|
2016-04-01 13:04:45 -06:00
|
|
|
q.Output(qpos.fset, &definitionResult{
|
2016-02-15 12:50:53 -07:00
|
|
|
pos: obj.Pos(),
|
|
|
|
descr: fmt.Sprintf("%s %s", obj.Kind, obj.Name),
|
2016-04-01 13:04:45 -06:00
|
|
|
})
|
2016-02-15 12:50:53 -07:00
|
|
|
return nil // success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the type checker.
|
2016-02-11 15:57:17 -07:00
|
|
|
lconf := loader.Config{Build: q.Build}
|
|
|
|
allowErrors(&lconf)
|
|
|
|
|
|
|
|
if _, err := importQueryPackage(q.Pos, &lconf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load/parse/type-check the program.
|
|
|
|
lprog, err := lconf.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
qpos, err := parseQueryPos(lprog, q.Pos, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := qpos.path[0].(*ast.Ident)
|
|
|
|
if id == nil {
|
|
|
|
return fmt.Errorf("no identifier here")
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := qpos.info.ObjectOf(id)
|
|
|
|
if obj == nil {
|
|
|
|
// Happens for y in "switch y := x.(type)",
|
|
|
|
// and the package declaration,
|
|
|
|
// but I think that's all.
|
|
|
|
return fmt.Errorf("no object for identifier")
|
|
|
|
}
|
|
|
|
|
2016-02-15 12:50:53 -07:00
|
|
|
if !obj.Pos().IsValid() {
|
|
|
|
return fmt.Errorf("%s is built in", obj.Name())
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
q.Output(lprog.Fset, &definitionResult{
|
2016-02-15 12:50:53 -07:00
|
|
|
pos: obj.Pos(),
|
|
|
|
descr: qpos.objectString(obj),
|
2016-04-01 13:04:45 -06:00
|
|
|
})
|
2016-02-11 15:57:17 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type definitionResult struct {
|
2016-02-15 12:50:53 -07:00
|
|
|
pos token.Pos // (nonzero) location of definition
|
|
|
|
descr string // description of object it denotes
|
2016-02-11 15:57:17 -07:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
func (r *definitionResult) PrintPlain(printf printfFunc) {
|
2016-02-15 12:50:53 -07:00
|
|
|
printf(r.pos, "defined here as %s", r.descr)
|
2016-02-11 15:57:17 -07:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
func (r *definitionResult) JSON(fset *token.FileSet) []byte {
|
|
|
|
return toJSON(&serial.Definition{
|
2016-02-15 12:50:53 -07:00
|
|
|
Desc: r.descr,
|
|
|
|
ObjPos: fset.Position(r.pos).String(),
|
2016-04-01 13:04:45 -06:00
|
|
|
})
|
2016-02-11 15:57:17 -07:00
|
|
|
}
|