Skip to content

Commit

Permalink
gopls/Suppress completion of filtered directory packages.
Browse files Browse the repository at this point in the history
Attempted fix for golang/go#52347.
  • Loading branch information
gonzojive committed Apr 19, 2022
1 parent fe932b4 commit 50125c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ func candidateImportName(pkg *pkg) string {
// GetAllCandidates calls wrapped for each package whose name starts with
// searchPrefix, and can be imported from filename with the package name filePkg.
func GetAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix, filename, filePkg string, env *ProcessEnv) error {
return GetFilteredCandidates(ctx, nil, wrapped, searchPrefix, filename, filePkg, env)
}

// GetFilteredCandidates calls wrapped for each package whose name starts with
// searchPrefix, and can be imported from filename with the package name filePkg.
func GetFilteredCandidates(ctx context.Context, shouldScanPackageDir func(pkgDir, importPathShort, packageName string) bool, wrapped func(ImportFix), searchPrefix, filename, filePkg string, env *ProcessEnv) error {
callback := &scanCallback{
rootFound: func(gopathwalk.Root) bool {
return true
Expand All @@ -708,8 +714,10 @@ func GetAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix
}
// 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), searchPrefix) ||
strings.HasPrefix(path.Base(pkg.importPathShort), searchPrefix)
if !strings.HasPrefix(ImportPathToAssumedName(pkg.importPathShort), searchPrefix) && !strings.HasPrefix(path.Base(pkg.importPathShort), searchPrefix) {
return false
}
return shouldScanPackageDir == nil || shouldScanPackageDir(pkg.dir, pkg.importPathShort, pkg.packageName)
},
packageNameLoaded: func(pkg *pkg) bool {
if !strings.HasPrefix(pkg.packageName, searchPrefix) {
Expand Down
20 changes: 19 additions & 1 deletion internal/lsp/source/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go/token"
"go/types"
"math"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1468,6 +1469,7 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
if !strings.HasPrefix(pkg.GetTypes().Name(), prefix) {
continue
}
// TODO: Check that package is not defined in an excluded dir?
paths = append(paths, path)
}

Expand Down Expand Up @@ -1548,9 +1550,25 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
})
count++
}

shouldIncludePackage := func(pkgDir, importPathShort, packageName string) bool {
// pkgDir is an absolute path. Convert it to a workspace-relative folder.
pkgDir = filepath.ToSlash(pkgDir)
if !c.snapshot.View().Folder().IsFile() {
return true
}
workspaceDir := filepath.ToSlash(c.snapshot.View().Folder().Filename())
workspaceRelativePkgDir := strings.TrimPrefix(pkgDir, workspaceDir)
if workspaceRelativePkgDir == pkgDir {
// Not in workspace, allow.
return true
}
disallowed := source.FiltersDisallow(workspaceRelativePkgDir, c.snapshot.View().Options().DirectoryFilters)
return !disallowed
}
c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
defer cancel()
return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
return imports.GetFilteredCandidates(ctx, shouldIncludePackage, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
})
return nil
}
Expand Down

0 comments on commit 50125c0

Please sign in to comment.