internal/lsp: downrank untyped completion candidates

In cases like:

type myInt int
const (
  a       = 1
  b myInt = 2
)
var foo myInt = <>

We now prefer "b" over "a" since b's type matches the expected type
exactly.

Change-Id: I675934761cc17f6b303b63b4715b31dd1af7cea1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/202737
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Muir Manders 2019-10-22 21:50:40 -07:00 committed by Rebecca Stambler
parent 2b544e3f2d
commit e96d959c47
3 changed files with 21 additions and 10 deletions

View File

@ -1346,7 +1346,15 @@ func (c *completer) matchingCandidate(cand *candidate) bool {
// Check that their constant kind (bool|int|float|complex|string) matches. // Check that their constant kind (bool|int|float|complex|string) matches.
// This doesn't take into account the constant value, so there will be some // This doesn't take into account the constant value, so there will be some
// false positives due to integer sign and overflow. // false positives due to integer sign and overflow.
return candBasic.Info()&types.IsConstType == wantBasic.Info()&types.IsConstType if candBasic.Info()&types.IsConstType == wantBasic.Info()&types.IsConstType {
// Lower candidate score if the types are not identical.
// This avoids ranking untyped integer constants above
// candidates with an exact type match.
if !types.Identical(candType, c.expectedType.objType) {
cand.score /= 2
}
return true
}
} }
return false return false
} }

View File

@ -12,28 +12,31 @@ func _() {
} }
func _() { func _() {
type myInt int
const ( const (
convC = "hi" //@item(convertC, "convC", "string", "const") convC = "hi" //@item(convertC, "convC", "string", "const")
convD = 123 //@item(convertD, "convD", "int", "const") convD = 123 //@item(convertD, "convD", "int", "const")
convE int = 123 //@item(convertE, "convE", "int", "const") convE int = 123 //@item(convertE, "convE", "int", "const")
convF string = "there" //@item(convertF, "convF", "string", "const") convF string = "there" //@item(convertF, "convF", "string", "const")
convG myInt = 123 //@item(convertG, "convG", "myInt", "const")
) )
var foo int var foo int
foo = conv //@complete(" //", convertD, convertE, convertC, convertF) foo = conv //@rank(" //", convertE, convertD)
type myInt int
var mi myInt var mi myInt
mi = conv //@complete(" //", convertD, convertC, convertE, convertF) mi = conv //@rank(" //", convertG, convertD, convertE)
mi + conv //@complete(" //", convertD, convertC, convertE, convertF) mi + conv //@rank(" //", convertG, convertD, convertE)
1 + conv //@complete(" //", convertD, convertE, convertC, convertF) 1 + conv //@rank(" //", convertD, convertE, convertG)
type myString string type myString string
var ms myString var ms myString
ms = conv //@complete(" //", convertC, convertD, convertE, convertF) ms = conv //@rank(" //", convertC, convertF)
type myUint uint32 type myUint uint32
var mu myUint var mu myUint
mu = conv //@complete(" //", convertD, convertC, convertE, convertF) mu = conv //@rank(" //", convertD, convertE)
} }

View File

@ -1,10 +1,10 @@
-- summary -- -- summary --
CompletionsCount = 219 CompletionsCount = 213
CompletionSnippetCount = 39 CompletionSnippetCount = 39
UnimportedCompletionsCount = 1 UnimportedCompletionsCount = 1
DeepCompletionsCount = 5 DeepCompletionsCount = 5
FuzzyCompletionsCount = 7 FuzzyCompletionsCount = 7
RankedCompletionsCount = 2 RankedCompletionsCount = 8
CaseSensitiveCompletionsCount = 4 CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 22 DiagnosticsCount = 22
FoldingRangesCount = 2 FoldingRangesCount = 2