-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgo_selector.go
114 lines (84 loc) · 1.85 KB
/
go_selector.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
113
114
package gocoder
import (
"context"
"go/ast"
"go/token"
)
type GoSelector struct {
rootExpr *GoExpr
goSelIdent *GoIdent
goXExpr *GoExpr
astExpr *ast.SelectorExpr
}
func newGoSelector(rootExpr *GoExpr, astSelector *ast.SelectorExpr) *GoSelector {
g := &GoSelector{
rootExpr: rootExpr,
astExpr: astSelector,
}
g.load()
return g
}
func (p *GoSelector) load() {
if p.astExpr.X != nil {
p.goXExpr = newGoExpr(p.rootExpr, p.astExpr.X)
}
if p.astExpr.Sel != nil {
p.goSelIdent = newGoIdent(p.rootExpr, p.astExpr.Sel)
}
}
func (p *GoSelector) X() *GoExpr {
return p.goXExpr
}
func (p *GoSelector) IsInOtherPackage() bool {
xIdent, ok := p.astExpr.X.(*ast.Ident)
if !ok {
return false
}
// if xIdent.Obj != nil {
// return false
// }
if len(xIdent.Name) == 0 {
return false
}
gofile := p.rootExpr.Options().GoFile
if gofile == nil {
return false
}
_, b := gofile.FindImportByName(xIdent.Name)
return b
}
func (p *GoSelector) UsingPackage() *GoPackage {
gofile := p.rootExpr.Options().GoFile
xIdent, ok := p.astExpr.X.(*ast.Ident)
if !ok {
return gofile.Package()
}
// if xIdent.Obj != nil {
// return gofile.Package()
// }
if len(xIdent.Name) == 0 {
return gofile.Package()
}
if gofile == nil {
return nil
}
pkg, exist := gofile.FindImportByName(xIdent.Name)
if exist {
return pkg
}
return gofile.Package()
}
func (p *GoSelector) GetSelName() string {
return p.astExpr.Sel.Name
}
func (p *GoSelector) Inspect(f InspectFunc, ctx context.Context) {
p.goXExpr.Inspect(f, ctx)
p.goSelIdent.Inspect(f, ctx)
}
func (p *GoSelector) Position() (token.Position, token.Position) {
return p.rootExpr.astFileSet.Position(p.astExpr.Pos()), p.rootExpr.astFileSet.Position(p.astExpr.End())
}
func (p *GoSelector) Print() error {
return ast.Print(p.rootExpr.astFileSet, p.astExpr)
}
func (p *GoSelector) goNode() {}