Skip to content

Commit 95f04f4

Browse files
committed
gopls/internal/golang: add resolve support for inline refactorings
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]>
1 parent 9619683 commit 95f04f4

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

Diff for: gopls/internal/golang/codeaction.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
106106
}
107107

108108
if want[protocol.RefactorInline] {
109-
rewrites, err := getInlineCodeActions(pkg, pgf, rng)
109+
rewrites, err := getInlineCodeActions(pkg, pgf, rng, snapshot.Options())
110110
if err != nil {
111111
return nil, err
112112
}
@@ -381,7 +381,7 @@ func canRemoveParameter(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Rang
381381
}
382382

383383
// getInlineCodeActions returns refactor.inline actions available at the specified range.
384-
func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Range) ([]protocol.CodeAction, error) {
384+
func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Range, options *settings.Options) ([]protocol.CodeAction, error) {
385385
start, end, err := pgf.RangePos(rng)
386386
if err != nil {
387387
return nil, err
@@ -391,9 +391,10 @@ func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Ra
391391
var commands []protocol.Command
392392
if _, fn, err := EnclosingStaticCall(pkg, pgf, start, end); err == nil {
393393
cmd, err := command.NewApplyFixCommand(fmt.Sprintf("Inline call to %s", fn.Name()), command.ApplyFixArgs{
394-
Fix: fixInlineCall,
395-
URI: pgf.URI,
396-
Range: rng,
394+
Fix: fixInlineCall,
395+
URI: pgf.URI,
396+
Range: rng,
397+
ResolveEdits: supportsResolveEdits(options),
397398
})
398399
if err != nil {
399400
return nil, err
@@ -404,11 +405,7 @@ func getInlineCodeActions(pkg *cache.Package, pgf *ParsedGoFile, rng protocol.Ra
404405
// Convert commands to actions.
405406
var actions []protocol.CodeAction
406407
for i := range commands {
407-
actions = append(actions, protocol.CodeAction{
408-
Title: commands[i].Title,
409-
Kind: protocol.RefactorInline,
410-
Command: &commands[i],
411-
})
408+
actions = append(actions, newCodeAction(commands[i].Title, protocol.RefactorInline, &commands[i], nil, options))
412409
}
413410
return actions, nil
414411
}

Diff for: gopls/internal/test/marker/testdata/codeaction/inline.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
This is a minimal test of the refactor.inline code action.
1+
This is a minimal test of the refactor.inline code action, without resolve support.
2+
See inline_resolve.txt for same test with resolve support.
23

34
-- go.mod --
45
module example.com/codeaction
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
This is a minimal test of the refactor.inline code actions, with resolve support.
2+
See inline.txt for same test without resolve support.
3+
4+
-- capabilities.json --
5+
{
6+
"textDocument": {
7+
"codeAction": {
8+
"dataSupport": true,
9+
"resolveSupport": {
10+
"properties": ["edit"]
11+
}
12+
}
13+
}
14+
}
15+
-- go.mod --
16+
module example.com/codeaction
17+
go 1.18
18+
19+
-- a/a.go --
20+
package a
21+
22+
func _() {
23+
println(add(1, 2)) //@codeaction("add", ")", "refactor.inline", inline)
24+
}
25+
26+
func add(x, y int) int { return x + y }
27+
28+
-- @inline/a/a.go --
29+
package a
30+
31+
func _() {
32+
println(1 + 2) //@codeaction("add", ")", "refactor.inline", inline)
33+
}
34+
35+
func add(x, y int) int { return x + y }

0 commit comments

Comments
 (0)