Skip to content

Commit

Permalink
replace_all
Browse files Browse the repository at this point in the history
  • Loading branch information
xzbdmw committed Nov 1, 2024
1 parent 386503d commit 2eeb2c4
Show file tree
Hide file tree
Showing 11 changed files with 810 additions and 33 deletions.
15 changes: 15 additions & 0 deletions gopls/internal/golang/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ var codeActionProducers = [...]codeActionProducer{
{kind: settings.RefactorExtractFunction, fn: refactorExtractFunction},
{kind: settings.RefactorExtractMethod, fn: refactorExtractMethod},
{kind: settings.RefactorExtractToNewFile, fn: refactorExtractToNewFile},
{kind: settings.RefactorExtractAllOccursOfExpr, fn: refactorExtractAllOccursOfExpr},
{kind: settings.RefactorExtractVariable, fn: refactorExtractVariable},
{kind: settings.RefactorInlineCall, fn: refactorInlineCall, needPkg: true},
{kind: settings.RefactorRewriteChangeQuote, fn: refactorRewriteChangeQuote},
Expand Down Expand Up @@ -458,6 +459,20 @@ func refactorExtractVariable(ctx context.Context, req *codeActionsRequest) error
return nil
}

// refactorExtractAllOccursOfExpr produces "Extract all occcurrances of expression" code action.
// See [extractAllOccursOfExpr] for command implementation.
func refactorExtractAllOccursOfExpr(ctx context.Context, req *codeActionsRequest) error {
// Don't suggest if only one expr is found,
// otherwise will duplicate with [refactorExtractVariable]
if exprs, ok, _ := canExtractExprs(req.start, req.end, req.pgf.File); ok && len(exprs) > 1 {
startOffset := req.pgf.Tok.Offset(exprs[0].Pos())
endOffset := req.pgf.Tok.Offset(exprs[0].End())
expr := req.pgf.Src[startOffset:endOffset]
req.addApplyFixAction(fmt.Sprintf("Extract %d occcurrances of %s", len(exprs), expr), fixExtractAllOccursOfExpr, req.loc)
}
return nil
}

// refactorExtractToNewFile produces "Extract declarations to new file" code actions.
// See [server.commandHandler.ExtractToNewFile] for command implementation.
func refactorExtractToNewFile(ctx context.Context, req *codeActionsRequest) error {
Expand Down
6 changes: 3 additions & 3 deletions gopls/internal/golang/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, file
// TODO: stricter rules for selectorExpr.
case *ast.BasicLit, *ast.CompositeLit, *ast.IndexExpr, *ast.SliceExpr,
*ast.UnaryExpr, *ast.BinaryExpr, *ast.SelectorExpr:
lhsName, _ := generateAvailableIdentifier(expr.Pos(), path, pkg, info, "x", 0)
lhsName, _ := generateAvailableIdentifier(expr.Pos(), path, pkg, info, "newVar", 0)
lhsNames = append(lhsNames, lhsName)
case *ast.CallExpr:
tup, ok := info.TypeOf(expr).(*types.Tuple)
if !ok {
// If the call expression only has one return value, we can treat it the
// same as our standard extract variable case.
lhsName, _ := generateAvailableIdentifier(expr.Pos(), path, pkg, info, "x", 0)
lhsName, _ := generateAvailableIdentifier(expr.Pos(), path, pkg, info, "newVar", 0)
lhsNames = append(lhsNames, lhsName)
break
}
idx := 0
for i := 0; i < tup.Len(); i++ {
// Generate a unique variable for each return value.
var lhsName string
lhsName, idx = generateAvailableIdentifier(expr.Pos(), path, pkg, info, "x", idx)
lhsName, idx = generateAvailableIdentifier(expr.Pos(), path, pkg, info, "newVar", idx)
lhsNames = append(lhsNames, lhsName)
}
default:
Expand Down
Loading

0 comments on commit 2eeb2c4

Please sign in to comment.