mirror of https://github.com/golang/go.git
internal/imports: filter candidates on directory name
When finding completion candidates, we can use the same tricks goimports uses to ignore directories that look irrelevant. Change-Id: I114a3d4e487aed7f59fc48b2f86d42129baf5183 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212859 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
872f4f411e
commit
2f3125dfbf
|
|
@ -589,9 +589,10 @@ func getFixes(fset *token.FileSet, f *ast.File, filename string, env *ProcessEnv
|
||||||
// match pre-existing gopls code.
|
// match pre-existing gopls code.
|
||||||
const MaxRelevance = 7
|
const MaxRelevance = 7
|
||||||
|
|
||||||
// getCandidatePkgs returns the list of pkgs that are accessible from filename,
|
// getCandidatePkgs works with the passed callback to find all acceptable packages.
|
||||||
// filtered to those that match pkgnameFilter.
|
// It deduplicates by import path, and uses a cached stdlib rather than reading
|
||||||
func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filename string, env *ProcessEnv) error {
|
// from disk.
|
||||||
|
func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, env *ProcessEnv) error {
|
||||||
// TODO(heschi): filter out current package. (Don't forget x_test can import x.)
|
// TODO(heschi): filter out current package. (Don't forget x_test can import x.)
|
||||||
|
|
||||||
// Start off with the standard library.
|
// Start off with the standard library.
|
||||||
|
|
@ -616,9 +617,7 @@ func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filena
|
||||||
// and generally redundant with the in-memory version.
|
// and generally redundant with the in-memory version.
|
||||||
return root.Type != gopathwalk.RootGOROOT && wrappedCallback.rootFound(root)
|
return root.Type != gopathwalk.RootGOROOT && wrappedCallback.rootFound(root)
|
||||||
},
|
},
|
||||||
dirFound: func(pkg *pkg) bool {
|
dirFound: wrappedCallback.dirFound,
|
||||||
return canUse(filename, pkg.dir) && wrappedCallback.dirFound(pkg)
|
|
||||||
},
|
|
||||||
packageNameLoaded: func(pkg *pkg) bool {
|
packageNameLoaded: func(pkg *pkg) bool {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
@ -649,8 +648,13 @@ func getAllCandidates(ctx context.Context, wrapped func(ImportFix), prefix strin
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
dirFound: func(pkg *pkg) bool {
|
dirFound: func(pkg *pkg) bool {
|
||||||
// TODO(heschi): apply dir match heuristics like pkgIsCandidate
|
if !canUse(filename, pkg.dir) {
|
||||||
return true
|
return false
|
||||||
|
}
|
||||||
|
// Try the assumed package name first, then a simpler path match
|
||||||
|
// in case of packages named vN, which are not uncommon.
|
||||||
|
return strings.HasPrefix(ImportPathToAssumedName(pkg.importPathShort), prefix) ||
|
||||||
|
strings.HasPrefix(path.Base(pkg.importPathShort), prefix)
|
||||||
},
|
},
|
||||||
packageNameLoaded: func(pkg *pkg) bool {
|
packageNameLoaded: func(pkg *pkg) bool {
|
||||||
if strings.HasPrefix(pkg.packageName, prefix) {
|
if strings.HasPrefix(pkg.packageName, prefix) {
|
||||||
|
|
@ -667,7 +671,7 @@ func getAllCandidates(ctx context.Context, wrapped func(ImportFix), prefix strin
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return getCandidatePkgs(ctx, callback, filename, env)
|
return getCandidatePkgs(ctx, callback, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A PackageExport is a package and its exports.
|
// A PackageExport is a package and its exports.
|
||||||
|
|
@ -682,8 +686,7 @@ func getPackageExports(ctx context.Context, wrapped func(PackageExport), complet
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
dirFound: func(pkg *pkg) bool {
|
dirFound: func(pkg *pkg) bool {
|
||||||
// TODO(heschi): apply dir match heuristics like pkgIsCandidate
|
return pkgIsCandidate(filename, references{completePackage: nil}, pkg)
|
||||||
return true
|
|
||||||
},
|
},
|
||||||
packageNameLoaded: func(pkg *pkg) bool {
|
packageNameLoaded: func(pkg *pkg) bool {
|
||||||
return pkg.packageName == completePackage
|
return pkg.packageName == completePackage
|
||||||
|
|
@ -704,7 +707,7 @@ func getPackageExports(ctx context.Context, wrapped func(PackageExport), complet
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return getCandidatePkgs(ctx, callback, filename, env)
|
return getCandidatePkgs(ctx, callback, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessEnv contains environment variables and settings that affect the use of
|
// ProcessEnv contains environment variables and settings that affect the use of
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue