Skip to content

Commit

Permalink
gopls/internal/golang: don't offer to move variadic parameters
Browse files Browse the repository at this point in the history
While we can't handle moving variadic parameters, don't offer to move
them.

For golang/go#70599

Change-Id: I21e78b4e2ae8c061d3858f2f981d593a6b0cf5bc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/633096
Reviewed-by: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Robert Findley <[email protected]>
  • Loading branch information
findleyr authored and gopherbot committed Dec 3, 2024
1 parent 399ee16 commit 557f540
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
39 changes: 26 additions & 13 deletions gopls/internal/golang/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,12 @@ func refactorRewriteRemoveUnusedParam(ctx context.Context, req *codeActionsReque
}

func refactorRewriteMoveParamLeft(ctx context.Context, req *codeActionsRequest) error {
if info := findParam(req.pgf, req.loc.Range); info != nil && info.paramIndex > 0 {
if info := findParam(req.pgf, req.loc.Range); info != nil &&
info.paramIndex > 0 &&
!is[*ast.Ellipsis](info.field.Type) {

// ^^ we can't currently handle moving a variadic param.
// TODO(rfindley): implement.

transform := identityTransform(info.decl.Type.Params)
transform[info.paramIndex] = command.ChangeSignatureParam{OldIndex: info.paramIndex - 1}
Expand All @@ -566,19 +571,27 @@ func refactorRewriteMoveParamLeft(ctx context.Context, req *codeActionsRequest)
}

func refactorRewriteMoveParamRight(ctx context.Context, req *codeActionsRequest) error {
if info := findParam(req.pgf, req.loc.Range); info != nil && info.paramIndex >= 0 && // found a param
info.paramIndex < info.decl.Type.Params.NumFields()-1 { // not the last param
if info := findParam(req.pgf, req.loc.Range); info != nil && info.paramIndex >= 0 {
params := info.decl.Type.Params
nparams := params.NumFields()
if info.paramIndex < nparams-1 { // not the last param
if info.paramIndex == nparams-2 && is[*ast.Ellipsis](params.List[len(params.List)-1].Type) {
// We can't currently handle moving a variadic param.
// TODO(rfindley): implement.
return nil
}

transform := identityTransform(info.decl.Type.Params)
transform[info.paramIndex] = command.ChangeSignatureParam{OldIndex: info.paramIndex + 1}
transform[info.paramIndex+1] = command.ChangeSignatureParam{OldIndex: info.paramIndex}
cmd := command.NewChangeSignatureCommand("Move parameter right", command.ChangeSignatureArgs{
Location: req.loc,
NewParams: transform,
NewResults: identityTransform(info.decl.Type.Results),
ResolveEdits: req.resolveEdits(),
})
req.addCommandAction(cmd, true)
transform := identityTransform(info.decl.Type.Params)
transform[info.paramIndex] = command.ChangeSignatureParam{OldIndex: info.paramIndex + 1}
transform[info.paramIndex+1] = command.ChangeSignatureParam{OldIndex: info.paramIndex}
cmd := command.NewChangeSignatureCommand("Move parameter right", command.ChangeSignatureArgs{
Location: req.loc,
NewParams: transform,
NewResults: identityTransform(info.decl.Type.Results),
ResolveEdits: req.resolveEdits(),
})
req.addCommandAction(cmd, true)
}
}
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions gopls/internal/test/marker/marker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"go/token"
Expand Down Expand Up @@ -2250,10 +2251,12 @@ func codeActionChanges(env *integration.Env, uri protocol.DocumentURI, rng proto
}
}
if len(candidates) != 1 {
var msg bytes.Buffer
fmt.Fprintf(&msg, "found %d CodeActions of kind %s for this diagnostic, want 1", len(candidates), kind)
for _, act := range actions {
env.T.Logf("found CodeAction Kind=%s Title=%q", act.Kind, act.Title)
fmt.Fprintf(&msg, "\n\tfound %q (%s)", act.Title, act.Kind)
}
return nil, fmt.Errorf("found %d CodeActions of kind %s for this diagnostic, want 1", len(candidates), kind)
return nil, errors.New(msg.String())
}
action := candidates[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ package a
// We should not offer movement involving variadic parameters if it is not well
// supported.

func Variadic(x int, y ...string) { //@codeaction("x", "refactor.rewrite.moveParamRight", err="type checking rewritten package")
func Variadic(x int, y ...string) { //@codeaction("x", "refactor.rewrite.moveParamRight", err="0 CodeActions"), codeaction("y", "refactor.rewrite.moveParamLeft", err="0 CodeActions")
}

func _() {
Expand Down

0 comments on commit 557f540

Please sign in to comment.