1
0
mirror of https://github.com/golang/go synced 2024-11-05 14:56:10 -07:00

internal/lsp: limit code action false positives for extract to variable

Instead of only checking whether the selection is an AST expression in
canExtractVariable, we now also check what kind of AST expression
it is. This limits the frequency of situations where the lightbulb
appears (canExtractVariable succeeds), but nothing can be extracted
(extractVariable fails).

Change-Id: I1e63c982e482bb72df48b414bdb4e8037140afdb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/247408
Run-TryBot: Josh Baum <joshbaum@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Josh Baum 2020-08-07 15:54:09 -04:00
parent c1903db4db
commit 72f2e0bf6d

View File

@ -94,7 +94,12 @@ func canExtractVariable(rng span.Range, file *ast.File) (ast.Expr, []ast.Node, b
if !ok { if !ok {
return nil, nil, false, fmt.Errorf("node is not an expression") return nil, nil, false, fmt.Errorf("node is not an expression")
} }
return expr, path, true, nil switch expr.(type) {
case *ast.BasicLit, *ast.CompositeLit, *ast.IndexExpr,
*ast.SliceExpr, *ast.UnaryExpr, *ast.BinaryExpr, *ast.SelectorExpr:
return expr, path, true, nil
}
return nil, nil, false, fmt.Errorf("cannot extract an %T to a variable", expr)
} }
// Calculate indentation for insertion. // Calculate indentation for insertion.