-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgo_ident.go
112 lines (95 loc) · 2.02 KB
/
go_ident.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gocoder
import (
"context"
"go/ast"
"go/token"
)
type GoIdent struct {
rootExpr *GoExpr
goChildren []GoNode
astExpr *ast.Ident
objKind string
}
func newGoIdent(rootExpr *GoExpr, ident *ast.Ident) *GoIdent {
g := &GoIdent{
rootExpr: rootExpr,
astExpr: ident,
}
g.load()
return g
}
func (p *GoIdent) HasObject() bool {
return p.astExpr.Obj != nil
}
func (p *GoIdent) ObjectKind() string {
return p.objKind
}
func (p *GoIdent) load() {
if p.astExpr.Obj == nil {
return
}
switch p.astExpr.Obj.Kind {
case ast.Var:
{
p.objKind = "var"
switch expr := p.astExpr.Obj.Decl.(type) {
case *ast.AssignStmt:
{
for i := 0; i < len(expr.Rhs); i++ {
p.goChildren = append(p.goChildren, newGoExpr(p.rootExpr, expr.Rhs[i]))
}
}
case *ast.ValueSpec:
{
if expr.Type != nil {
p.goChildren = append(p.goChildren, newGoExpr(p.rootExpr, expr.Type))
}
}
case *ast.Field:
{
if expr.Type != nil {
p.goChildren = append(p.goChildren, newGoExpr(p.rootExpr, expr.Type))
}
}
}
}
case ast.Typ:
{
p.objKind = "type"
switch expr := p.astExpr.Obj.Decl.(type) {
case *ast.TypeSpec:
{
p.goChildren = append(p.goChildren, newGoExpr(p.rootExpr, expr))
}
}
}
case ast.Fun:
{
p.objKind = "func"
switch expr := p.astExpr.Obj.Decl.(type) {
case *ast.FuncDecl:
{
p.goChildren = append(p.goChildren, newGoExpr(p.rootExpr, expr))
}
}
}
}
}
func (p *GoIdent) Inspect(f InspectFunc, ctx context.Context) {
for i := 0; i < len(p.goChildren); i++ {
child, inspectable := p.goChildren[i].(GoNodeInspectable)
if inspectable {
child.Inspect(f, ctx)
}
}
}
func (p *GoIdent) Name() string {
return p.astExpr.Name
}
func (p *GoIdent) Position() (token.Position, token.Position) {
return p.rootExpr.astFileSet.Position(p.astExpr.Pos()), p.rootExpr.astFileSet.Position(p.astExpr.End())
}
func (p *GoIdent) Print() error {
return ast.Print(p.rootExpr.astFileSet, p.astExpr)
}
func (p *GoIdent) goNode() {}