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

internal/lsp: omit "iota" completion outside const decls

Add a special check to skip builtin "iota" candidate outside of const
declarations.

Change-Id: I767c012585dfc51b4c07cf5847d3b4083a4a2a7b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/195044
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2019-09-12 13:21:36 -07:00 committed by Rebecca Stambler
parent 31e00f45c2
commit 1d8cfc4bd2
4 changed files with 31 additions and 2 deletions

View File

@ -603,6 +603,8 @@ func (c *completer) lexical() error {
}
scopes = append(scopes, c.pkg.GetTypes().Scope(), types.Universe)
builtinIota := types.Universe.Lookup("iota")
// Track seen variables to avoid showing completions for shadowed variables.
// This works since we look at scopes from innermost to outermost.
seen := make(map[string]struct{})
@ -635,6 +637,11 @@ func (c *completer) lexical() error {
}
}
// Don't suggest "iota" outside of const decls.
if obj == builtinIota && !c.inConstDecl() {
continue
}
// If we haven't already added a candidate for an object with this name.
if _, ok := seen[obj.Name()]; !ok {
seen[obj.Name()] = struct{}{}
@ -665,6 +672,15 @@ func (c *completer) lexical() error {
return nil
}
func (c *completer) inConstDecl() bool {
for _, n := range c.path {
if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.CONST {
return true
}
}
return false
}
// structLiteralFieldName finds completions for struct field names inside a struct literal.
func (c *completer) structLiteralFieldName() error {
clInfo := c.enclosingCompositeLiteral

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, iota, 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, _nil, panic, print, println, real, recover, rune, string, _true, uint, uint16, uint32, uint64, uint8, uintptr)
}
/* Create markers for builtin types. Only for use by this test.

13
internal/lsp/testdata/builtins/iota.go vendored Normal file
View File

@ -0,0 +1,13 @@
package builtins
func _() {
const (
foo = iota //@complete(" //", iota)
)
iota //@complete(" //")
var iota int //@item(iotaVar, "iota", "int", "var")
iota //@complete(" //", iotaVar)
}

View File

@ -29,7 +29,7 @@ import (
// We hardcode the expected number of test cases to ensure that all tests
// are being executed. If a test is added, this number must be changed.
const (
ExpectedCompletionsCount = 160
ExpectedCompletionsCount = 163
ExpectedCompletionSnippetCount = 16
ExpectedDiagnosticsCount = 21
ExpectedFormatCount = 6