From 480da3ebd79c82ff135cb63ea1df5546584e25f4 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 6 May 2020 22:26:25 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- internal/lsp/source/completion.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index fff371aae5..fe3ba16e17 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -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 }