From 825cb0626375eeecd4d401e7f1653c1ec405d2c8 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Mon, 9 Dec 2019 10:41:28 -0800 Subject: [PATCH] internal/lsp: downrank "nil" completion candidate Having nil ranked normally causes it to show up as the top candidate in cases like: context.WithCancel(<>) // "nil" shows up before "context.Background()" "context.Background()" gets a slight score penalty since it is a deep completion, so "nil" is ranked highest. Sometimes you do want "nil", but it's such a short identifier you probably aren't leaning too heavily on autocompletion. I think it makes sense to optimize for the case when you want something non-nil. Change-Id: I537927db2b573535e751380c4cba5c9873dfe524 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210539 Run-TryBot: Muir Manders TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/source/completion.go | 14 ++++++++++++-- internal/lsp/testdata/builtins/builtins.go | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 788a528ebd9..342d095c64c 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -696,7 +696,10 @@ func (c *completer) lexical() error { } scopes = append(scopes, c.pkg.GetTypes().Scope(), types.Universe) - builtinIota := types.Universe.Lookup("iota") + var ( + builtinIota = types.Universe.Lookup("iota") + builtinNil = types.Universe.Lookup("nil") + ) // Track seen variables to avoid showing completions for shadowed variables. // This works since we look at scopes from innermost to outermost. @@ -736,10 +739,17 @@ func (c *completer) lexical() error { continue } + score := stdScore + + // Dowrank "nil" a bit so it is ranked below more interesting candidates. + if obj == builtinNil { + score /= 2 + } + // If we haven't already added a candidate for an object with this name. if _, ok := seen[obj.Name()]; !ok { seen[obj.Name()] = struct{}{} - c.found(obj, stdScore, nil) + c.found(obj, score, nil) } } } diff --git a/internal/lsp/testdata/builtins/builtins.go b/internal/lsp/testdata/builtins/builtins.go index d9d384313b9..25c29f21e6c 100644 --- a/internal/lsp/testdata/builtins/builtins.go +++ b/internal/lsp/testdata/builtins/builtins.go @@ -1,7 +1,7 @@ package builtins func _() { - //@complete("", append, bool, byte, cap, close, complex, complex128, complex64, copy, delete, error, _false, float32, float64, imag, int, int16, int32, int64, int8, len, make, new, _nil, panic, print, println, real, recover, rune, string, _true, uint, uint16, uint32, uint64, uint8, uintptr) + //@complete("", append, bool, byte, cap, close, complex, complex128, complex64, copy, delete, error, _false, float32, float64, imag, int, int16, int32, int64, int8, len, make, new, panic, print, println, real, recover, rune, string, _true, uint, uint16, uint32, uint64, uint8, uintptr, _nil) } /* Create markers for builtin types. Only for use by this test.