1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:54:43 -07:00

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 <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2019-12-09 10:41:28 -08:00 committed by Rebecca Stambler
parent 56eb7d2c19
commit 825cb06263
2 changed files with 13 additions and 3 deletions

View File

@ -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)
}
}
}

View File

@ -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.