From a222fb47e2f16cc9a03ca7c9f0bb993368260571 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Thu, 2 Jan 2020 13:26:21 -0800 Subject: [PATCH] internal/lsp/source: don't downrank builtin constant completions We downrank untyped constant candidates so that we prefer candidates whose type matches exactly. However, this was causing builtin constants like "true" to be outranked by candidates that fuzzily match "true". Fix by not downranking builtin constants. Fixes golang/go#36363. Change-Id: I14801688c96efdbb7ff9fee69f66028530df984c Reviewed-on: https://go-review.googlesource.com/c/tools/+/213137 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/source/completion.go | 8 ++++---- internal/lsp/testdata/builtins/{iota.go => constants.go} | 6 ++++++ internal/lsp/testdata/summary.txt.golden | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) rename internal/lsp/testdata/builtins/{iota.go => constants.go} (59%) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 43f5d797f48..4aff81bf0b4 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -1665,10 +1665,10 @@ func (c *completer) matchingCandidate(cand *candidate) bool { // This doesn't take into account the constant value, so there will be some // false positives due to integer sign and overflow. 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, expType) { + // Lower candidate score if the types are not identical. This avoids + // ranking untyped constants above candidates with an exact type + // match. Don't lower score of builtin constants (e.g. "true"). + if !types.Identical(candType, expType) && cand.obj.Parent() != types.Universe { cand.score /= 2 } return true diff --git a/internal/lsp/testdata/builtins/iota.go b/internal/lsp/testdata/builtins/constants.go similarity index 59% rename from internal/lsp/testdata/builtins/iota.go rename to internal/lsp/testdata/builtins/constants.go index 44427245965..7ad07bd1f3a 100644 --- a/internal/lsp/testdata/builtins/iota.go +++ b/internal/lsp/testdata/builtins/constants.go @@ -11,3 +11,9 @@ func _() { iota //@complete(" //", iotaVar) } + +func _() { + var twoRedUpEnd bool //@item(TRUEVar, "twoRedUpEnd", "bool", "var") + + var _ bool = true //@rank(" //", _true, TRUEVar) +} diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 33ac397edaf..25d58cebe2f 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -4,7 +4,7 @@ CompletionSnippetCount = 61 UnimportedCompletionsCount = 4 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 -RankedCompletionsCount = 32 +RankedCompletionsCount = 33 CaseSensitiveCompletionsCount = 4 DiagnosticsCount = 35 FoldingRangesCount = 2