/
m_gocode.go
162 lines (137 loc) · 3.18 KB
/
m_gocode.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package margo_pkg
import (
"go/ast"
"go/parser"
"go/token"
"gosublime/something-borrowed/gocode"
"io/ioutil"
"path/filepath"
"strings"
)
type mGocode struct {
Autoinst bool
InstallSuffix string
Env map[string]string
Home string
Dir string
Builtins bool
Fn string
Src string
Pos int
calltip bool
}
type calltipVisitor struct {
offset int
fset *token.FileSet
x *ast.CallExpr
}
func (v *calltipVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node != nil {
if x, ok := node.(*ast.CallExpr); ok {
a := v.fset.Position(node.Pos())
b := v.fset.Position(node.End())
if (a.IsValid() && v.offset >= a.Offset) && (!b.IsValid() || v.offset <= b.Offset) {
v.x = x
}
}
}
return v
}
func (m *mGocode) Call() (interface{}, string) {
if m.Src == "" {
// this is here for testing, the client should always send the src
s, _ := ioutil.ReadFile(m.Fn)
m.Src = string(s)
}
if m.Src == "" {
return nil, "No source"
}
pos := bytePos(m.Src, m.Pos)
if m.Pos < 0 {
return nil, "Invalid offset"
}
src := []byte(m.Src)
fn := m.Fn
if !filepath.IsAbs(fn) {
fn = filepath.Join(orString(m.Dir, m.Home), orString(fn, "_.go"))
}
res := struct {
Candidates []gocode.MargoCandidate
}{}
if m.calltip {
res.Candidates = m.calltips(src, fn, pos)
} else {
res.Candidates = m.completions(src, fn, pos)
}
if m.Autoinst && len(res.Candidates) == 0 {
autoInstall(AutoInstOptions{
Src: m.Src,
Env: m.Env,
InstallSuffix: m.InstallSuffix,
})
}
return res, ""
}
func (g *mGocode) completions(src []byte, fn string, pos int) []gocode.MargoCandidate {
c := gocode.MargoConfig{}
c.InstallSuffix = g.InstallSuffix
c.Builtins = g.Builtins
c.GOROOT, c.GOPATHS = envRootList(g.Env)
return gocode.Margo.Complete(c, src, fn, pos)
}
func (m *mGocode) calltips(src []byte, fn string, offset int) []gocode.MargoCandidate {
id, fset, af := identAtOffset(src, offset)
if id != nil {
cp := fset.Position(id.End())
if cp.IsValid() {
line := offsetLine(fset, af, offset)
cr := cp.Offset
cl := m.completions(src, fn, cr)
if (cp.Line == line || line == 0) && len(cl) > 0 {
for i, c := range cl {
if strings.EqualFold(id.Name, c.Name) {
return cl[i : i+1]
}
}
}
}
}
return []gocode.MargoCandidate{}
}
func identAtOffset(src []byte, offset int) (id *ast.Ident, fset *token.FileSet, af *ast.File) {
fset = token.NewFileSet()
af, _ = parser.ParseFile(fset, "<stdin>", src, 0)
if af == nil {
return
}
vis := &calltipVisitor{
offset: offset,
fset: fset,
}
ast.Walk(vis, af)
if vis.x != nil && vis.x.Fun != nil {
switch v := vis.x.Fun.(type) {
case *ast.Ident:
id = v
case *ast.SelectorExpr:
id = v.Sel
}
}
return
}
func offsetLine(fset *token.FileSet, af *ast.File, offset int) (line int) {
defer func() {
if err := recover(); err != nil {
line = 0
}
}()
return fset.File(af.Pos()).Position(token.Pos(offset)).Line
}
func init() {
registry.Register("gocode_complete", func(b *Broker) Caller {
return &mGocode{}
})
registry.Register("gocode_calltip", func(b *Broker) Caller {
return &mGocode{calltip: true}
})
}