forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gocode.go
187 lines (168 loc) · 4.26 KB
/
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gocode
import (
"go/build"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/charlievieth/buildutil"
)
const g_debug = false
func init() {
if g_debug {
log.SetOutput(os.Stderr)
log.SetFlags(log.Lshortfile | log.LstdFlags)
log.SetPrefix("[gocode] ")
log.Println("debug enabled")
}
}
type Candidate struct {
Name string `json:"name"`
Type string `json:"type"`
Class string `json:"class"`
}
func (c Candidate) String() string {
b := make([]byte, len(c.Name)+len(c.Type)+len(c.Class)+2)
n := copy(b, c.Class)
n += copy(b[n:], []byte{' '})
n += copy(b[n:], c.Name)
switch c.Class {
case "func":
if strings.HasPrefix(c.Type, "func") {
n += copy(b[n:], c.Type[len("func"):])
}
return string(b[:n])
case "package":
return string(b[:n])
default:
if c.Type != "" {
n += copy(b[n:], []byte{' '})
n += copy(b[n:], c.Type)
}
return string(b[:n])
}
}
type Config struct {
GOROOT string
GOPATH string
InstallSuffix string
AutoBuild bool
Builtins bool // propose builtin functions
}
func (c *Config) Complete(file []byte, name string, cursor int) []Candidate {
if gocodeDaemon == nil {
gocodeDaemon = newDaemon()
}
return gocodeDaemon.complete(file, name, cursor, c)
}
var gocodeDaemon = newDaemon()
type daemon struct {
autocomplete *auto_complete_context
declcache *decl_cache
pkgcache package_cache
context package_lookup_context
mu sync.Mutex
}
func newDaemon() *daemon {
ctxt := build.Default
ctxt.GOPATH = os.Getenv("GOPATH")
ctxt.GOROOT = runtime.GOROOT()
ctxt.IsDir = is_dir
d := daemon{
context: package_lookup_context{Context: ctxt},
pkgcache: new_package_cache(),
}
d.declcache = new_decl_cache(&d.context)
d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
return &d
}
var NoCandidates = []Candidate{}
func (d *daemon) complete(file []byte, name string, cursor int, conf *Config) (res []Candidate) {
defer func() {
if e := recover(); e != nil {
if g_debug {
log.Printf("gocode: panic (%+v)\n", e)
}
if len(res) == 0 {
res = NoCandidates
}
}
}()
d.mu.Lock()
defer d.mu.Unlock()
d.update(conf)
d.context.CurrentPackagePath = ""
importPath, err := buildutil.ImportPath(&d.context.Context, filepath.Dir(name))
if err == nil {
d.context.CurrentPackagePath = importPath
}
list, _ := d.autocomplete.apropos(file, name, cursor)
if list == nil || len(list) == 0 {
return NoCandidates
}
res = make([]Candidate, len(list))
for i, c := range list {
res[i] = Candidate{
Name: c.Name,
Type: c.Type,
Class: c.Class.String(),
}
}
return res
}
func (d *daemon) update(conf *Config) {
g_config.SetProposeBuiltins(conf.Builtins)
g_config.SetAutoBuild(conf.AutoBuild)
if !d.same(conf) {
d.context.GOPATH = conf.GOPATH
d.context.GOROOT = conf.GOROOT
d.context.InstallSuffix = conf.InstallSuffix
d.pkgcache = new_package_cache()
d.declcache = new_decl_cache(&d.context)
d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
g_config.libPath = d.libPath() // global config
g_config.proposeBuiltins = conf.Builtins
}
}
func (d *daemon) same(conf *Config) bool {
return d.context.GOPATH == conf.GOPATH &&
d.context.GOROOT == conf.GOROOT &&
d.context.InstallSuffix == conf.InstallSuffix
}
// libPath, returns the OS and Arch specific pkg paths for the current GOROOT
// and GOPATH.
func (d *daemon) libPath() string {
var all []string
pkg := d.pkgDir()
if d.context.GOROOT != "" {
all = append(all, filepath.Join(d.context.GOROOT, pkg))
}
if d.context.GOPATH != "" {
all = append(all, d.pkgpaths(pkg)...)
}
return strings.Join(all, string(filepath.ListSeparator))
}
// pkgpaths, returns all GOPATH pkg paths for Arch arch.
func (d *daemon) pkgpaths(arch string) []string {
paths := filepath.SplitList(d.context.GOPATH)
n := 0
for _, p := range paths {
if p != d.context.GOROOT && p != "" && p[0] != '~' {
paths[n] = filepath.Join(p, arch)
n++
}
}
return paths[:n]
}
// osArch returns the os and arch specific package directory
func (d *daemon) pkgDir() string {
var s string
if d.context.InstallSuffix == "" {
s = d.context.GOOS + "_" + d.context.GOARCH
} else {
s = d.context.GOOS + "_" + d.context.GOARCH + "_" + d.context.InstallSuffix
}
return filepath.Join("pkg", s)
}