From e96d959c4788a79af280868cc58158f544fa2fb2 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Tue, 22 Oct 2019 21:50:40 -0700 Subject: [PATCH] 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 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/source/completion.go | 10 +++++++++- internal/lsp/testdata/rank/convert_rank.go.in | 17 ++++++++++------- internal/lsp/testdata/summary.txt.golden | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index a51c6e512e..07076cd555 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -1346,7 +1346,15 @@ func (c *completer) matchingCandidate(cand *candidate) bool { // 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 // 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 } diff --git a/internal/lsp/testdata/rank/convert_rank.go.in b/internal/lsp/testdata/rank/convert_rank.go.in index fe6b78317a..ac17416260 100644 --- a/internal/lsp/testdata/rank/convert_rank.go.in +++ b/internal/lsp/testdata/rank/convert_rank.go.in @@ -12,28 +12,31 @@ func _() { } func _() { + + type myInt int + const ( convC = "hi" //@item(convertC, "convC", "string", "const") convD = 123 //@item(convertD, "convD", "int", "const") convE int = 123 //@item(convertE, "convE", "int", "const") convF string = "there" //@item(convertF, "convF", "string", "const") + convG myInt = 123 //@item(convertG, "convG", "myInt", "const") ) var foo int - foo = conv //@complete(" //", convertD, convertE, convertC, convertF) + foo = conv //@rank(" //", convertE, convertD) - type myInt int var mi myInt - mi = conv //@complete(" //", convertD, convertC, convertE, convertF) - mi + conv //@complete(" //", convertD, convertC, convertE, convertF) + mi = conv //@rank(" //", convertG, convertD, convertE) + mi + conv //@rank(" //", convertG, convertD, convertE) - 1 + conv //@complete(" //", convertD, convertE, convertC, convertF) + 1 + conv //@rank(" //", convertD, convertE, convertG) type myString string var ms myString - ms = conv //@complete(" //", convertC, convertD, convertE, convertF) + ms = conv //@rank(" //", convertC, convertF) type myUint uint32 var mu myUint - mu = conv //@complete(" //", convertD, convertC, convertE, convertF) + mu = conv //@rank(" //", convertD, convertE) } diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 9127199146..1e5c354bfe 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -1,10 +1,10 @@ -- summary -- -CompletionsCount = 219 +CompletionsCount = 213 CompletionSnippetCount = 39 UnimportedCompletionsCount = 1 DeepCompletionsCount = 5 FuzzyCompletionsCount = 7 -RankedCompletionsCount = 2 +RankedCompletionsCount = 8 CaseSensitiveCompletionsCount = 4 DiagnosticsCount = 22 FoldingRangesCount = 2