Skip to content

Commit

Permalink
gopls/internal/golang: add resolve support for inline refactorings
Browse files Browse the repository at this point in the history
Resolve edits for inline refactorings when the client supports it.

For golang/go#64510

Change-Id: I67101ab19576054b1d03c46e3d318a4660088a63
Reviewed-on: https://go-review.googlesource.com/c/tools/+/562118
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
  • Loading branch information
suzmue committed Feb 9, 2024
1 parent 9619683 commit 95f04f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
17 changes: 7 additions & 10 deletions gopls/internal/golang/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
}

if want[protocol.RefactorInline] {
rewrites, err := getInlineCodeActions(pkg, pgf, rng)
rewrites, err := getInlineCodeActions(pkg, pgf, rng, snapshot.Options())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func canRemoveParameter(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Rang
}

// getInlineCodeActions returns refactor.inline actions available at the specified range.
func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Range) ([]protocol.CodeAction, error) {
func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Range, options *settings.Options) ([]protocol.CodeAction, error) {
start, end, err := pgf.RangePos(rng)
if err != nil {
return nil, err
Expand All @@ -391,9 +391,10 @@ func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Ra
var commands []protocol.Command
if _, fn, err := EnclosingStaticCall(pkg, pgf, start, end); err == nil {
cmd, err := command.NewApplyFixCommand(fmt.Sprintf("Inline call to %s", fn.Name()), command.ApplyFixArgs{
Fix: fixInlineCall,
URI: pgf.URI,
Range: rng,
Fix: fixInlineCall,
URI: pgf.URI,
Range: rng,
ResolveEdits: supportsResolveEdits(options),
})
if err != nil {
return nil, err
Expand All @@ -404,11 +405,7 @@ func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Ra
// Convert commands to actions.
var actions []protocol.CodeAction
for i := range commands {
actions = append(actions, protocol.CodeAction{
Title: commands[i].Title,
Kind: protocol.RefactorInline,
Command: &commands[i],
})
actions = append(actions, newCodeAction(commands[i].Title, protocol.RefactorInline, &commands[i], nil, options))
}
return actions, nil
}
Expand Down
3 changes: 2 additions & 1 deletion gopls/internal/test/marker/testdata/codeaction/inline.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
This is a minimal test of the refactor.inline code action.
This is a minimal test of the refactor.inline code action, without resolve support.
See inline_resolve.txt for same test with resolve support.

-- go.mod --
module example.com/codeaction
Expand Down
35 changes: 35 additions & 0 deletions gopls/internal/test/marker/testdata/codeaction/inline_resolve.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
This is a minimal test of the refactor.inline code actions, with resolve support.
See inline.txt for same test without resolve support.

-- capabilities.json --
{
"textDocument": {
"codeAction": {
"dataSupport": true,
"resolveSupport": {
"properties": ["edit"]
}
}
}
}
-- go.mod --
module example.com/codeaction
go 1.18

-- a/a.go --
package a

func _() {
println(add(1, 2)) //@codeaction("add", ")", "refactor.inline", inline)
}

func add(x, y int) int { return x + y }

-- @inline/a/a.go --
package a

func _() {
println(1 + 2) //@codeaction("add", ")", "refactor.inline", inline)
}

func add(x, y int) int { return x + y }

0 comments on commit 95f04f4

Please sign in to comment.