Skip to content

Commit 2f2ae23

Browse files
committed
List import paths for packages vendored only for the current package
1 parent 31552c2 commit 2f2ae23

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

autocompletecontext.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,19 @@ func (c *auto_complete_context) get_candidates_from_decl(cc cursor_context, clas
257257
}
258258

259259
func (c *auto_complete_context) get_import_candidates(partial string, b *out_buffers) {
260-
pkgdirs := g_daemon.context.pkg_dirs()
260+
currentPackagePath, pkgdirs := g_daemon.context.pkg_dirs()
261+
fmt.Println(pkgdirs)
261262
resultSet := map[string]struct{}{}
262263
for _, pkgdir := range pkgdirs {
263264
// convert srcpath to pkgpath and get candidates
264-
get_import_candidates_dir(pkgdir, filepath.FromSlash(partial), b.ignorecase, resultSet)
265+
get_import_candidates_dir(pkgdir, filepath.FromSlash(partial), b.ignorecase, currentPackagePath, resultSet)
265266
}
266267
for k := range resultSet {
267268
b.candidates = append(b.candidates, candidate{Name: k, Class: decl_import})
268269
}
269270
}
270271

271-
func get_import_candidates_dir(root, partial string, ignorecase bool, r map[string]struct{}) {
272+
func get_import_candidates_dir(root, partial string, ignorecase bool, currentPackagePath string, r map[string]struct{}) {
272273
var fpath string
273274
var match bool
274275
if strings.HasSuffix(partial, "/") {
@@ -287,15 +288,17 @@ func get_import_candidates_dir(root, partial string, ignorecase bool, r map[stri
287288
if match && !has_prefix(rel, partial, ignorecase) {
288289
continue
289290
} else if fi[i].IsDir() {
290-
get_import_candidates_dir(root, rel+string(filepath.Separator), ignorecase, r)
291+
get_import_candidates_dir(root, rel+string(filepath.Separator), ignorecase, currentPackagePath, r)
291292
} else {
292293
ext := filepath.Ext(name)
293294
if ext != ".a" {
294295
continue
295296
} else {
296297
rel = rel[0 : len(rel)-2]
297298
}
298-
r[vendorlessImportPath(filepath.ToSlash(rel))] = struct{}{}
299+
if ipath, ok := vendorlessImportPath(filepath.ToSlash(rel), currentPackagePath); ok {
300+
r[ipath] = struct{}{}
301+
}
299302
}
300303
}
301304
}

declcache.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,10 @@ func (ctxt *package_lookup_context) gopath() []string {
453453
return all
454454
}
455455

456-
func (ctxt *package_lookup_context) pkg_dirs() []string {
456+
func (ctxt *package_lookup_context) pkg_dirs() (string, []string) {
457457
pkgdir := fmt.Sprintf("%s_%s", ctxt.GOOS, ctxt.GOARCH)
458458

459+
var currentPackagePath string
459460
var all []string
460461
if ctxt.GOROOT != "" {
461462
dir := filepath.Join(ctxt.GOROOT, "pkg", pkgdir)
@@ -466,6 +467,7 @@ func (ctxt *package_lookup_context) pkg_dirs() []string {
466467

467468
switch g_config.PackageLookupMode {
468469
case "go":
470+
currentPackagePath = ctxt.CurrentPackagePath
469471
for _, p := range ctxt.gopath() {
470472
dir := filepath.Join(p, "pkg", pkgdir)
471473
if is_dir(dir) {
@@ -483,7 +485,7 @@ func (ctxt *package_lookup_context) pkg_dirs() []string {
483485
case "bzl":
484486
// TODO: Support bazel mode
485487
}
486-
return all
488+
return currentPackagePath, all
487489
}
488490

489491
type decl_cache struct {

utils.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,24 @@ func find_gb_project_root(path string) (string, error) {
153153

154154
// vendorlessImportPath returns the devendorized version of the provided import path.
155155
// e.g. "foo/bar/vendor/a/b" => "a/b"
156-
func vendorlessImportPath(ipath string) string {
156+
func vendorlessImportPath(ipath string, currentPackagePath string) (string, bool) {
157+
split := strings.Split(ipath, "vendor/")
158+
// no vendor in path
159+
if len(split) == 1 {
160+
return ipath, true
161+
}
162+
// this import path does not belong to the current package
163+
if currentPackagePath != "" && !strings.Contains(currentPackagePath, split[0]) {
164+
return "", false
165+
}
157166
// Devendorize for use in import statement.
158167
if i := strings.LastIndex(ipath, "/vendor/"); i >= 0 {
159-
return ipath[i+len("/vendor/"):]
168+
return ipath[i+len("/vendor/"):], true
160169
}
161170
if strings.HasPrefix(ipath, "vendor/") {
162-
return ipath[len("vendor/"):]
171+
return ipath[len("vendor/"):], true
163172
}
164-
return ipath
173+
return ipath, true
165174
}
166175

167176
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)