internal/lsp: return early in completion where possible

This a pure cut-and-paste.

Fixes golang/go#38868

Change-Id: I2ff07134a8b9f6186bab737caceab8a34a8e2f44
Reviewed-on: https://go-review.googlesource.com/c/tools/+/232677
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-05-06 22:26:25 -04:00
parent 6441d34c3f
commit 480da3ebd7
1 changed files with 15 additions and 13 deletions

View File

@ -455,6 +455,7 @@ func Completion(ctx context.Context, snapshot Snapshot, fh FileHandle, protoPos
pos := rng.Start
// Check if completion at this position is valid. If not, return early.
switch n := path[0].(type) {
case *ast.BasicLit:
// Skip completion inside any kind of literal.
@ -465,6 +466,20 @@ func Completion(ctx context.Context, snapshot Snapshot, fh FileHandle, protoPos
// example, don't offer completions at "<>" in "foo(bar...<>").
return nil, nil, nil
}
case *ast.Ident:
// reject defining identifiers
if obj, ok := pkg.GetTypesInfo().Defs[n]; ok {
if v, ok := obj.(*types.Var); ok && v.IsField() && v.Embedded() {
// An anonymous field is also a reference to a type.
} else {
objStr := ""
if obj != nil {
qual := types.RelativeTo(pkg.GetTypes())
objStr = types.ObjectString(obj, qual)
}
return nil, nil, ErrIsDefinition{objStr: objStr}
}
}
}
opts := snapshot.View().Options()
@ -557,19 +572,6 @@ func Completion(ctx context.Context, snapshot Snapshot, fh FileHandle, protoPos
}
return c.items, c.getSurrounding(), nil
}
// reject defining identifiers
if obj, ok := pkg.GetTypesInfo().Defs[n]; ok {
if v, ok := obj.(*types.Var); ok && v.IsField() && v.Embedded() {
// An anonymous field is also a reference to a type.
} else {
objStr := ""
if obj != nil {
qual := types.RelativeTo(pkg.GetTypes())
objStr = types.ObjectString(obj, qual)
}
return nil, nil, ErrIsDefinition{objStr: objStr}
}
}
if err := c.lexical(ctx); err != nil {
return nil, nil, err
}