Skip to content

Commit 6a7cf61

Browse files
committed
internal/lsp/source: fix cached package name matching
I made a silly mistake and checked the prefix on the import path rather than the package name, which obviously breaks everything other than top-level stdlib packages. Fix that, then tweak the ranking a bit. We now get deep completions, which is nice, but filled up the results too fast. Now instead of 5 results of any kind, we give up after 5 packages searched. Change-Id: I15b293f68f17531077da9ffe791a38ccc0e129f4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/231617 Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 688b3c5 commit 6a7cf61

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

internal/lsp/source/completion.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
862862
if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
863863
imp.name = pkg.GetTypes().Name()
864864
}
865-
c.packageMembers(ctx, pkg.GetTypes(), stdScore+.1*float64(relevance), imp)
865+
c.packageMembers(ctx, pkg.GetTypes(), unimportedScore(relevance), imp)
866866
if len(c.items) >= unimportedMemberTarget {
867867
return nil
868868
}
@@ -882,7 +882,7 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
882882
// Continue with untyped proposals.
883883
pkg := types.NewPackage(pkgExport.Fix.StmtInfo.ImportPath, pkgExport.Fix.IdentName)
884884
for _, export := range pkgExport.Exports {
885-
score := stdScore + 0.1*float64(pkgExport.Fix.Relevance)
885+
score := unimportedScore(pkgExport.Fix.Relevance)
886886
c.found(ctx, candidate{
887887
obj: types.NewVar(0, pkg, export, nil),
888888
score: score,
@@ -901,6 +901,12 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
901901
})
902902
}
903903

904+
// unimportedScore returns a score for an unimported package that is generally
905+
// lower than other candidates.
906+
func unimportedScore(relevance int) float64 {
907+
return (stdScore + .1*float64(relevance)) / 2
908+
}
909+
904910
func (c *completer) packageMembers(ctx context.Context, pkg *types.Package, score float64, imp *importInfo) {
905911
scope := pkg.Scope()
906912
for _, name := range scope.Names() {
@@ -1107,15 +1113,15 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
11071113
if c.surrounding != nil {
11081114
prefix = c.surrounding.Prefix()
11091115
}
1110-
initialItemCount := len(c.items)
1116+
count := 0
11111117

11121118
known, err := c.snapshot.CachedImportPaths(ctx)
11131119
if err != nil {
11141120
return err
11151121
}
11161122
var paths []string
1117-
for path := range known {
1118-
if !strings.HasPrefix(path, prefix) {
1123+
for path, pkg := range known {
1124+
if !strings.HasPrefix(pkg.GetTypes().Name(), prefix) {
11191125
continue
11201126
}
11211127
paths = append(paths, path)
@@ -1131,22 +1137,25 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
11311137

11321138
for path, relevance := range relevances {
11331139
pkg := known[path]
1140+
if _, ok := seen[pkg.GetTypes().Name()]; ok {
1141+
continue
1142+
}
11341143
imp := &importInfo{
11351144
importPath: path,
11361145
pkg: pkg,
11371146
}
11381147
if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
11391148
imp.name = pkg.GetTypes().Name()
11401149
}
1141-
score := 0.01 * float64(relevance)
1150+
if count >= maxUnimportedPackageNames {
1151+
return nil
1152+
}
11421153
c.found(ctx, candidate{
11431154
obj: types.NewPkgName(0, nil, pkg.GetTypes().Name(), pkg.GetTypes()),
1144-
score: score,
1155+
score: unimportedScore(relevance),
11451156
imp: imp,
11461157
})
1147-
if len(c.items)-initialItemCount >= maxUnimportedPackageNames {
1148-
return nil
1149-
}
1158+
count++
11501159
}
11511160

11521161
ctx, cancel := context.WithCancel(ctx)
@@ -1159,26 +1168,28 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
11591168
if _, ok := seen[pkg.IdentName]; ok {
11601169
return
11611170
}
1171+
if _, ok := relevances[pkg.StmtInfo.ImportPath]; ok {
1172+
return
1173+
}
11621174

1163-
if len(c.items)-initialItemCount >= maxUnimportedPackageNames {
1175+
if count >= maxUnimportedPackageNames {
11641176
cancel()
11651177
return
11661178
}
1167-
// Rank unimported packages significantly lower than other results.
1168-
score := 0.01 * float64(pkg.Relevance)
11691179

11701180
// Do not add the unimported packages to seen, since we can have
11711181
// multiple packages of the same name as completion suggestions, since
11721182
// only one will be chosen.
11731183
obj := types.NewPkgName(0, nil, pkg.IdentName, types.NewPackage(pkg.StmtInfo.ImportPath, pkg.IdentName))
11741184
c.found(ctx, candidate{
11751185
obj: obj,
1176-
score: score,
1186+
score: unimportedScore(pkg.Relevance),
11771187
imp: &importInfo{
11781188
importPath: pkg.StmtInfo.ImportPath,
11791189
name: pkg.StmtInfo.Name,
11801190
},
11811191
})
1192+
count++
11821193
}
11831194
return c.snapshot.View().RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
11841195
return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts)

internal/lsp/testdata/lsp/primarymod/unimported/unimported.go.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package unimported
22

33
func _() {
4-
http //@unimported("p", nethttp, nethttptest)
4+
http //@unimported("p", nethttp)
55
pkg //@unimported("g", externalpackage)
66
// container/ring is extremely unlikely to be imported by anything, so shouldn't have type information.
77
ring.Ring //@unimported("Ring", ringring)
@@ -12,7 +12,7 @@ func _() {
1212

1313
// Create markers for unimported std lib packages. Only for use by this test.
1414
/* http */ //@item(nethttp, "http", "\"net/http\"", "package")
15-
/* httptest */ //@item(nethttptest, "httptest", "\"net/http/httptest\"", "package")
15+
1616
/* pkg */ //@item(externalpackage, "pkg", "\"example.com/extramodule/pkg\"", "package")
1717

1818
/* ring.Ring */ //@item(ringring, "Ring", "(from \"container/ring\")", "var")

0 commit comments

Comments
 (0)