Skip to content

Commit 3d03fbd

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/lsp: use matcher score in ranking unimported candidates
In the refactoring of CL 472183, we inadvertently dropped the fuzzy matching factor from the unimported completion item score. Fixes golang/go#62560 Change-Id: I35294c55d4229a885b781d3cdd0c6433e82f280b Reviewed-on: https://go-review.googlesource.com/c/tools/+/531418 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 7577387 commit 3d03fbd

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

gopls/internal/lsp/regtest/marker.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ var update = flag.Bool("update", false, "if set, update test data during marker
232232
// request at the given location, and verifies that each expected
233233
// completion item occurs in the results, in the expected order. Other
234234
// unexpected completion items may occur in the results.
235-
// TODO(rfindley): this should accept a slice of labels, rather than
236-
// completion items.
235+
// TODO(rfindley): this exists for compatibility with the old marker tests.
236+
// Replace this with rankl, and rename.
237+
//
238+
// - rankl(location, ...label): like rank, but only cares about completion
239+
// item labels.
237240
//
238241
// - refs(location, want ...location): executes a textDocument/references
239242
// request at the first location and asserts that the result is the set of
@@ -712,6 +715,7 @@ var actionMarkerFuncs = map[string]func(marker){
712715
"hover": actionMarkerFunc(hoverMarker),
713716
"implementation": actionMarkerFunc(implementationMarker),
714717
"rank": actionMarkerFunc(rankMarker),
718+
"rankl": actionMarkerFunc(ranklMarker),
715719
"refs": actionMarkerFunc(refsMarker),
716720
"rename": actionMarkerFunc(renameMarker),
717721
"renameerr": actionMarkerFunc(renameErrMarker),
@@ -1450,6 +1454,23 @@ func rankMarker(mark marker, src protocol.Location, items ...completionItem) {
14501454
}
14511455
}
14521456

1457+
func ranklMarker(mark marker, src protocol.Location, labels ...string) {
1458+
list := mark.run.env.Completion(src)
1459+
var got []string
1460+
// Collect results that are present in items, preserving their order.
1461+
for _, g := range list.Items {
1462+
for _, label := range labels {
1463+
if g.Label == label {
1464+
got = append(got, g.Label)
1465+
break
1466+
}
1467+
}
1468+
}
1469+
if diff := cmp.Diff(labels, got); diff != "" {
1470+
mark.errorf("completion rankings do not match (-want +got):\n%s", diff)
1471+
}
1472+
}
1473+
14531474
func snippetMarker(mark marker, src protocol.Location, item completionItem, want string) {
14541475
list := mark.run.env.Completion(src)
14551476
var (

gopls/internal/lsp/source/completion/completion.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
12961296
Label: id.Name,
12971297
Detail: fmt.Sprintf("%s (from %q)", strings.ToLower(tok.String()), m.PkgPath),
12981298
InsertText: id.Name,
1299-
Score: unimportedScore(relevances[path]),
1299+
Score: float64(score) * unimportedScore(relevances[path]),
13001300
}
13011301
switch tok {
13021302
case token.FUNC:
@@ -1397,6 +1397,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
13971397
if ignoreUnimportedCompletion(pkgExport.Fix) {
13981398
return
13991399
}
1400+
14001401
mu.Lock()
14011402
defer mu.Unlock()
14021403
// TODO(adonovan): what if the actual package has a vendor/ prefix?
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
This test verifies that completion of package members in unimported packages
2+
reflects their fuzzy score, even when those members are present in the
3+
transitive import graph of the main module. (For technical reasons, this was
4+
the nature of the regression in golang/go#62560.)
5+
6+
-- go.mod --
7+
module mod.test
8+
9+
-- foo/foo.go --
10+
package foo
11+
12+
func _() {
13+
json.U //@rankl(re"U()", "Unmarshal", "InvalidUTF8Error"), diag("json", re"(undefined|undeclared)")
14+
}
15+
16+
-- bar/bar.go --
17+
package bar
18+
19+
import _ "encoding/json"

0 commit comments

Comments
 (0)