mirror of
https://github.com/golang/go
synced 2024-11-05 14:46:11 -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:
parent
c1903db4db
commit
72f2e0bf6d
@ -94,7 +94,12 @@ func canExtractVariable(rng span.Range, file *ast.File) (ast.Expr, []ast.Node, b
|
||||
if !ok {
|
||||
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.
|
||||
|
Loading…
Reference in New Issue
Block a user