forked from rogpeppe/godef
-
Notifications
You must be signed in to change notification settings - Fork 1
/
objpos.go
68 lines (65 loc) · 1.39 KB
/
objpos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package types
import (
"github.com/tw4452852/godef/go/ast"
"github.com/tw4452852/godef/go/token"
)
func declPos(name string, decl ast.Node) token.Pos {
switch d := decl.(type) {
case nil:
return token.NoPos
case *ast.AssignStmt:
for _, n := range d.Lhs {
if n, ok := n.(*ast.Ident); ok && n.Name == name {
return n.Pos()
}
}
case *ast.Field:
for _, n := range d.Names {
if n.Name == name {
return n.Pos()
}
}
case *ast.ValueSpec:
for _, n := range d.Names {
if n.Name == name {
return n.Pos()
}
}
case *ast.TypeSpec:
if d.Name.Name == name {
return d.Name.Pos()
}
case *ast.FuncDecl:
if d.Name.Name == name {
return d.Name.Pos()
}
case *ast.LabeledStmt:
if d.Label.Name == name {
return d.Label.Pos()
}
case *ast.GenDecl:
for _, spec := range d.Specs {
if pos := declPos(name, spec); pos.IsValid() {
return pos
}
}
case *ast.TypeSwitchStmt:
return declPos(name, d.Assign)
}
return token.NoPos
}
// DeclPos computes the source position of the declaration of an object name.
// The result may be an invalid position if it cannot be computed
// (obj.Decl may be nil or not correct).
// This should be called ast.Object.Pos.
func DeclPos(obj *ast.Object) token.Pos {
decl, _ := obj.Decl.(ast.Node)
if decl == nil {
return token.NoPos
}
pos := declPos(obj.Name, decl)
if !pos.IsValid() {
pos = decl.Pos()
}
return pos
}