Skip to content

Commit c47efd1

Browse files
committed
Attach package import path to scopes, pass it through to formatters
This makes JSON and CSV formatters emit the import path for the package defining the symbol being completed. This should help tools like company get documentation for symbols, as gocode expects `importpath.symbol` as input.
1 parent 65402ba commit c47efd1

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

autocompletecontext.go

+26-14
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222

2323
// fields must be exported for RPC
2424
type candidate struct {
25-
Name string
26-
Type string
27-
Class decl_class
25+
Name string
26+
Type string
27+
Class decl_class
28+
Package string
2829
}
2930

3031
type out_buffers struct {
@@ -65,7 +66,7 @@ func (b *out_buffers) Swap(i, j int) {
6566
b.candidates[i], b.candidates[j] = b.candidates[j], b.candidates[i]
6667
}
6768

68-
func (b *out_buffers) append_decl(p, name string, decl *decl, class decl_class) {
69+
func (b *out_buffers) append_decl(p, name, pkg string, decl *decl, class decl_class) {
6970
c1 := !g_config.ProposeBuiltins && decl.scope == g_universe_scope && decl.name != "Error"
7071
c2 := class != decl_invalid && decl.class != class
7172
c3 := class == decl_invalid && !has_prefix(name, p, b.ignorecase)
@@ -78,14 +79,15 @@ func (b *out_buffers) append_decl(p, name string, decl *decl, class decl_class)
7879

7980
decl.pretty_print_type(b.tmpbuf, b.canonical_aliases)
8081
b.candidates = append(b.candidates, candidate{
81-
Name: name,
82-
Type: b.tmpbuf.String(),
83-
Class: decl.class,
82+
Name: name,
83+
Type: b.tmpbuf.String(),
84+
Class: decl.class,
85+
Package: pkg,
8486
})
8587
b.tmpbuf.Reset()
8688
}
8789

88-
func (b *out_buffers) append_embedded(p string, decl *decl, class decl_class) {
90+
func (b *out_buffers) append_embedded(p string, decl *decl, pkg string, class decl_class) {
8991
if decl.embedded == nil {
9092
return
9193
}
@@ -119,10 +121,10 @@ func (b *out_buffers) append_embedded(p string, decl *decl, class decl_class) {
119121
if _, has := b.tmpns[c.name]; has {
120122
continue
121123
}
122-
b.append_decl(p, c.name, c, class)
124+
b.append_decl(p, c.name, pkg, c, class)
123125
b.tmpns[c.name] = true
124126
}
125-
b.append_embedded(p, typedecl, class)
127+
b.append_embedded(p, typedecl, pkg, class)
126128
}
127129

128130
if first_level {
@@ -208,7 +210,11 @@ func (c *auto_complete_context) get_candidates_from_set(set map[string]*decl, pa
208210
continue
209211
}
210212
value.infer_type()
211-
b.append_decl(partial, key, value, class)
213+
pkgname := ""
214+
if pkg, ok := c.pcache[value.name]; ok {
215+
pkgname = pkg.import_name
216+
}
217+
b.append_decl(partial, key, pkgname, value, class)
212218
}
213219
}
214220

@@ -246,19 +252,25 @@ func (c *auto_complete_context) get_candidates_from_decl(cc cursor_context, clas
246252
continue
247253
}
248254
}
249-
b.append_decl(cc.partial, decl.name, decl, class)
255+
pkgname := ""
256+
if pkg, ok := c.pcache[decl.scope.pkgname]; ok {
257+
pkgname = pkg.import_name
258+
}
259+
b.append_decl(cc.partial, decl.name, pkgname, decl, class)
250260
}
251261
// propose all children of an underlying struct/interface type
252262
adecl := advance_to_struct_or_interface(cc.decl)
253263
if adecl != nil && adecl != cc.decl {
254264
for _, decl := range adecl.children {
255265
if decl.class == decl_var {
256-
b.append_decl(cc.partial, decl.name, decl, class)
266+
pkgname := c.pcache[decl.name].import_name
267+
b.append_decl(cc.partial, decl.name, pkgname, decl, class)
257268
}
258269
}
259270
}
260271
// propose all children of its embedded types
261-
b.append_embedded(cc.partial, cc.decl, class)
272+
pkgname := c.pcache[cc.decl.name].import_name
273+
b.append_embedded(cc.partial, cc.decl, pkgname, class)
262274
}
263275

264276
func (c *auto_complete_context) get_import_candidates(partial string, b *out_buffers) {

cursorcontext.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func resolveKnownPackageIdent(ident string, filename string, context *package_lo
413413
return nil
414414
}
415415

416-
p := new_package_file_cache(path)
416+
p := new_package_file_cache(path, path)
417417
p.update_cache()
418418
return p.main
419419
}

formatters.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type csv_formatter struct{}
124124

125125
func (*csv_formatter) write_candidates(candidates []candidate, num int) {
126126
for _, c := range candidates {
127-
fmt.Printf("%s,,%s,,%s\n", c.Class, c.Name, c.Type)
127+
fmt.Printf("%s,,%s,,%s,,%s\n", c.Class, c.Name, c.Type, c.Package)
128128
}
129129
}
130130

@@ -145,8 +145,8 @@ func (*json_formatter) write_candidates(candidates []candidate, num int) {
145145
if i != 0 {
146146
fmt.Printf(", ")
147147
}
148-
fmt.Printf(`{"class": "%s", "name": "%s", "type": "%s"}`,
149-
c.Class, c.Name, c.Type)
148+
fmt.Printf(`{"class": "%s", "name": "%s", "type": "%s", "package": "%s"}`,
149+
c.Class, c.Name, c.Type, c.Package)
150150
}
151151
fmt.Print("]]")
152152
}

package.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ type package_parser interface {
2020
//-------------------------------------------------------------------------
2121

2222
type package_file_cache struct {
23-
name string // file name
24-
mtime int64
25-
defalias string
23+
name string // file name
24+
import_name string
25+
mtime int64
26+
defalias string
2627

2728
scope *scope
2829
main *decl // package declaration
2930
others map[string]*decl
3031
}
3132

32-
func new_package_file_cache(name string) *package_file_cache {
33+
func new_package_file_cache(absname, name string) *package_file_cache {
3334
m := new(package_file_cache)
34-
m.name = name
35+
m.name = absname
36+
m.import_name = name
3537
m.mtime = 0
3638
m.defalias = ""
3739
return m
@@ -92,7 +94,7 @@ func (m *package_file_cache) update_cache() {
9294
}
9395

9496
func (m *package_file_cache) process_package_data(data []byte) {
95-
m.scope = new_scope(g_universe_scope)
97+
m.scope = new_named_scope(g_universe_scope, m.name)
9698

9799
// find import section
98100
i := bytes.Index(data, []byte{'\n', '$', '$'})
@@ -226,7 +228,7 @@ func (c package_cache) append_packages(ps map[string]*package_file_cache, pkgs [
226228
if mod, ok := c[m.abspath]; ok {
227229
ps[m.abspath] = mod
228230
} else {
229-
mod = new_package_file_cache(m.abspath)
231+
mod = new_package_file_cache(m.abspath, m.path)
230232
ps[m.abspath] = mod
231233
c[m.abspath] = mod
232234
}

scope.go

+11
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@ package main
55
//-------------------------------------------------------------------------
66

77
type scope struct {
8+
// the package name that this scope resides in
9+
pkgname string
810
parent *scope // nil for universe scope
911
entities map[string]*decl
1012
}
1113

14+
func new_named_scope(outer *scope, name string) *scope {
15+
s := new_scope(outer)
16+
s.pkgname = name
17+
return s
18+
}
19+
1220
func new_scope(outer *scope) *scope {
1321
s := new(scope)
22+
if outer != nil {
23+
s.pkgname = outer.pkgname
24+
}
1425
s.parent = outer
1526
s.entities = make(map[string]*decl)
1627
return s

server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func server_auto_complete(file []byte, filename string, cursor int, context_pack
137137
if err := recover(); err != nil {
138138
print_backtrace(err)
139139
c = []candidate{
140-
{"PANIC", "PANIC", decl_invalid},
140+
{"PANIC", "PANIC", decl_invalid, "panic"},
141141
}
142142

143143
// drop cache

0 commit comments

Comments
 (0)