Skip to content

Commit 26fbd88

Browse files
committed
replace_all
zz
1 parent 386503d commit 26fbd88

15 files changed

+857
-62
lines changed
243 KB
Loading
Loading
237 KB
Loading
-26.8 KB
Binary file not shown.

gopls/doc/features/transformation.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Gopls supports the following code actions:
7272
- [`refactor.extract.method`](#extract)
7373
- [`refactor.extract.toNewFile`](#extract.toNewFile)
7474
- [`refactor.extract.variable`](#extract)
75+
- [`refactor.extract.variable.all`](#extract)
7576
- [`refactor.inline.call`](#refactor.inline.call)
7677
- [`refactor.rewrite.changeQuote`](#refactor.rewrite.changeQuote)
7778
- [`refactor.rewrite.fillStruct`](#refactor.rewrite.fillStruct)
@@ -313,11 +314,18 @@ newly created declaration that contains the selected code:
313314
will be a method of the same receiver type.
314315

315316
- **`refactor.extract.variable`** replaces an expression by a reference to a new
316-
local variable named `x` initialized by the expression:
317+
local variable named `newVar` initialized by the expression:
317318

318-
![Before extracting a var](../assets/extract-var-before.png)
319+
![Before extracting a var](../assets/extract-expressions-before.png)
319320
![After extracting a var](../assets/extract-var-after.png)
320321

322+
- **`refactor.extract.variable.all`** replaces all occurrences of the selected expression
323+
within the function with a reference to a new local variable named `newVar`.
324+
This extracts the expression once and reuses it wherever it appears in the function.
325+
326+
![Before extracting all expressions](../assets/extract-expressions-before.png)
327+
![After extracting all expressions](../assets/extract-expressions-after.png)
328+
321329
If the default name for the new declaration is already in use, gopls
322330
generates a fresh name.
323331

gopls/doc/release/v0.17.0.md

+5
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ Gopls now offers a new code action, “Declare missing method of T.f”,
7373
where T is the concrete type and f is the undefined method.
7474
The stub method's signature is inferred
7575
from the context of the call.
76+
77+
## Extract all occurrences of the same expression under selection
78+
When you have multiple instances of the same expression in a function,
79+
you can use this code action to extract it into a variable.
80+
All occurrences of the expression will be replaced with a reference to the new variable.

gopls/internal/golang/codeaction.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"golang.org/x/tools/gopls/internal/protocol"
2828
"golang.org/x/tools/gopls/internal/protocol/command"
2929
"golang.org/x/tools/gopls/internal/settings"
30+
"golang.org/x/tools/gopls/internal/util/safetoken"
3031
"golang.org/x/tools/gopls/internal/util/typesutil"
3132
"golang.org/x/tools/internal/event"
3233
"golang.org/x/tools/internal/imports"
@@ -236,6 +237,7 @@ var codeActionProducers = [...]codeActionProducer{
236237
{kind: settings.RefactorExtractFunction, fn: refactorExtractFunction},
237238
{kind: settings.RefactorExtractMethod, fn: refactorExtractMethod},
238239
{kind: settings.RefactorExtractToNewFile, fn: refactorExtractToNewFile},
240+
{kind: settings.RefactorExtractAllOccursOfExpr, fn: refactorExtractAllOccursOfExpr},
239241
{kind: settings.RefactorExtractVariable, fn: refactorExtractVariable},
240242
{kind: settings.RefactorInlineCall, fn: refactorInlineCall, needPkg: true},
241243
{kind: settings.RefactorRewriteChangeQuote, fn: refactorRewriteChangeQuote},
@@ -449,11 +451,40 @@ func refactorExtractMethod(ctx context.Context, req *codeActionsRequest) error {
449451
return nil
450452
}
451453

452-
// refactorExtractVariable produces "Extract variable" code actions.
454+
// refactorExtractVariable produces "Extract expression" code actions.
453455
// See [extractVariable] for command implementation.
454456
func refactorExtractVariable(ctx context.Context, req *codeActionsRequest) error {
455-
if _, _, ok, _ := canExtractVariable(req.start, req.end, req.pgf.File); ok {
456-
req.addApplyFixAction("Extract variable", fixExtractVariable, req.loc)
457+
if expr, _, ok, _ := canExtractVariable(req.start, req.end, req.pgf.File); ok {
458+
startOffset, err := safetoken.Offset(req.pgf.Tok, expr.Pos())
459+
if err != nil {
460+
return nil
461+
}
462+
endOffset, err := safetoken.Offset(req.pgf.Tok, expr.End())
463+
if err != nil {
464+
return nil
465+
}
466+
expr := req.pgf.Src[startOffset:endOffset]
467+
req.addApplyFixAction(fmt.Sprintf("Extract %s", expr), fixExtractVariable, req.loc)
468+
}
469+
return nil
470+
}
471+
472+
// refactorExtractAllOccursOfExpr produces "Extract n occurrences of expression" code action.
473+
// See [extractAllOccursOfExpr] for command implementation.
474+
func refactorExtractAllOccursOfExpr(ctx context.Context, req *codeActionsRequest) error {
475+
// Don't suggest if only one expr is found,
476+
// otherwise it will duplicate with [refactorExtractVariable]
477+
if _, exprs, ok, _ := canExtractVariable(req.start, req.end, req.pgf.File); ok && len(exprs) > 1 {
478+
startOffset, err := safetoken.Offset(req.pgf.Tok, exprs[0].Pos())
479+
if err != nil {
480+
return nil
481+
}
482+
endOffset, err := safetoken.Offset(req.pgf.Tok, exprs[0].End())
483+
if err != nil {
484+
return nil
485+
}
486+
expr := req.pgf.Src[startOffset:endOffset]
487+
req.addApplyFixAction(fmt.Sprintf("Extract %d occcurrances of %s", len(exprs), expr), fixExtractAllOccursOfExpr, req.loc)
457488
}
458489
return nil
459490
}

0 commit comments

Comments
 (0)