internal/lsp/fuzzy: fix return value on no match
The fuzzy matcher doc string says that it returns 0 on no match, but
this is in fact not true: it returns -1 on no match.
0 makes more sense, so fix the implementation rather than the docstring.
Change-Id: I997a6b5dcb1d7c25cc73b2c236d24647f9326c80
Reviewed-on: https://go-review.googlesource.com/c/tools/+/248417
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go
index 54a6992..01f6e82 100644
--- a/internal/analysisinternal/analysis.go
+++ b/internal/analysisinternal/analysis.go
@@ -402,14 +402,14 @@
func FindBestMatch(pattern string, idents []*ast.Ident) ast.Expr {
fuzz := fuzzy.NewMatcher(pattern)
var bestFuzz ast.Expr
- highScore := float32(-1) // minimum score is -1 (no match)
+ highScore := float32(0) // minimum score is 0 (no match)
for _, ident := range idents {
// TODO: Improve scoring algorithm.
score := fuzz.Score(ident.Name)
if score > highScore {
highScore = score
bestFuzz = ident
- } else if score == -1 {
+ } else if score == 0 {
// Order matters in the fuzzy matching algorithm. If we find no match
// when matching the target to the identifier, try matching the identifier
// to the target.
diff --git a/internal/lsp/fuzzy/matcher.go b/internal/lsp/fuzzy/matcher.go
index f39f7ef..16a6430 100644
--- a/internal/lsp/fuzzy/matcher.go
+++ b/internal/lsp/fuzzy/matcher.go
@@ -135,7 +135,7 @@
}
m.lastCandidateMatched = false
- return -1
+ return 0
}
const minScore = -10000
diff --git a/internal/lsp/fuzzy/matcher_test.go b/internal/lsp/fuzzy/matcher_test.go
index 4c2eac5..bac81c0 100644
--- a/internal/lsp/fuzzy/matcher_test.go
+++ b/internal/lsp/fuzzy/matcher_test.go
@@ -71,8 +71,8 @@
{
pattern: "abc",
tests: []scoreTest{
- {"def", eq, -1},
- {"abd", eq, -1},
+ {"def", eq, 0},
+ {"abd", eq, 0},
{"abc", ge, 0},
{"Abc", ge, 0},
{"Ab stuff c", ge, 0},
@@ -81,8 +81,8 @@
{
pattern: "Abc",
tests: []scoreTest{
- {"def", eq, -1},
- {"abd", eq, -1},
+ {"def", eq, 0},
+ {"abd", eq, 0},
{"abc", ge, 0},
{"Abc", ge, 0},
{"Ab stuff c", ge, 0},
@@ -103,7 +103,7 @@
for _, sct := range tc.tests {
score := m.Score(sct.candidate)
if !sct.comparator.eval(score, sct.ref) {
- t.Errorf("not true that m.Score(%s)[=%v] %s %v", sct.candidate, score, sct.comparator, sct.ref)
+ t.Errorf("m.Score(%q) = %.2g, want %s %v", sct.candidate, score, sct.comparator, sct.ref)
}
}
}
@@ -196,7 +196,7 @@
matcher := fuzzy.NewMatcher(tc.p)
score := matcher.Score(tc.str)
if tc.want == "" {
- if score >= 0 {
+ if score > 0 {
t.Errorf("Score(%s, %s) = %v; want: <= 0", tc.p, tc.str, score)
}
continue
diff --git a/internal/lsp/source/completion_literal.go b/internal/lsp/source/completion_literal.go
index a115e95..342306d 100644
--- a/internal/lsp/source/completion_literal.go
+++ b/internal/lsp/source/completion_literal.go
@@ -104,7 +104,7 @@
}
// If prefix matches the type name, client may want a composite literal.
- if score := c.matcher.Score(matchName); score >= 0 {
+ if score := c.matcher.Score(matchName); score > 0 {
if cand.takeAddress {
if sel != nil {
// If we are in a selector we must place the "&" before the selector.
@@ -146,7 +146,7 @@
// If prefix matches "make", client may want a "make()"
// invocation. We also include the type name to allow for more
// flexible fuzzy matching.
- if score := c.matcher.Score("make." + matchName); !cand.takeAddress && score >= 0 {
+ if score := c.matcher.Score("make." + matchName); !cand.takeAddress && score > 0 {
switch literalType.Underlying().(type) {
case *types.Slice:
// The second argument to "make()" for slices is required, so default to "0".
@@ -159,7 +159,7 @@
}
// If prefix matches "func", client may want a function literal.
- if score := c.matcher.Score("func"); !cand.takeAddress && score >= 0 && !isInterface(expType) {
+ if score := c.matcher.Score("func"); !cand.takeAddress && score > 0 && !isInterface(expType) {
switch t := literalType.Underlying().(type) {
case *types.Signature:
c.functionLiteral(ctx, t, float64(score))