Skip to content

Commit

Permalink
internal/lsp: convert refactor code actions to use codeAction/resolve
Browse files Browse the repository at this point in the history
Allow apply fix and change signature commands to return edits instead
of applying the edits.

Added a marker test for removing parameters using the new resolve logic.
We probably want most refactoring code actions to work both ways. This
also updates the fill struct test to make sure that the capabilities
of the editor are being correctly respected.

For golang/go#64510

Change-Id: If58f7bdff52ec8e1621c007d029c5b9b60bbdd3a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/548276
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
suzmue committed Jan 18, 2024
1 parent f2d3f78 commit 592d9e1
Show file tree
Hide file tree
Showing 23 changed files with 1,346 additions and 126 deletions.
82 changes: 82 additions & 0 deletions gopls/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ Args:
"character": uint32,
},
},
// Whether to resolve and return the edits.
"ResolveEdits": bool,
}
```

Result:

```
{
// Holds changes to existing resources.
"changes": map[golang.org/x/tools/gopls/internal/lsp/protocol.DocumentURI][]golang.org/x/tools/gopls/internal/lsp/protocol.TextEdit,
// Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes
// are either an array of `TextDocumentEdit`s to express changes to n different text documents
// where each text document edit addresses a specific version of a text document. Or it can contain
// above `TextDocumentEdit`s mixed with create, rename and delete file / folder operations.
//
// Whether a client supports versioned document edits is expressed via
// `workspace.workspaceEdit.documentChanges` client capability.
//
// If a client neither supports `documentChanges` nor `workspace.workspaceEdit.resourceOperations` then
// only plain `TextEdit`s using the `changes` property are supported.
"documentChanges": []{
"TextDocumentEdit": {
"textDocument": { ... },
"edits": { ... },
},
"RenameFile": {
"kind": string,
"oldUri": string,
"newUri": string,
"options": { ... },
"ResourceOperation": { ... },
},
},
// A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and
// delete file / folder operations.
//
// Whether clients honor this property depends on the client capability `workspace.changeAnnotationSupport`.
//
// @since 3.16.0
"changeAnnotations": map[string]golang.org/x/tools/gopls/internal/lsp/protocol.ChangeAnnotation,
}
```

Expand All @@ -101,6 +142,47 @@ Args:
"end": { ... },
},
},
// Whether to resolve and return the edits.
"ResolveEdits": bool,
}
```

Result:

```
{
// Holds changes to existing resources.
"changes": map[golang.org/x/tools/gopls/internal/lsp/protocol.DocumentURI][]golang.org/x/tools/gopls/internal/lsp/protocol.TextEdit,
// Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes
// are either an array of `TextDocumentEdit`s to express changes to n different text documents
// where each text document edit addresses a specific version of a text document. Or it can contain
// above `TextDocumentEdit`s mixed with create, rename and delete file / folder operations.
//
// Whether a client supports versioned document edits is expressed via
// `workspace.workspaceEdit.documentChanges` client capability.
//
// If a client neither supports `documentChanges` nor `workspace.workspaceEdit.resourceOperations` then
// only plain `TextEdit`s using the `changes` property are supported.
"documentChanges": []{
"TextDocumentEdit": {
"textDocument": { ... },
"edits": { ... },
},
"RenameFile": {
"kind": string,
"oldUri": string,
"newUri": string,
"options": { ... },
"ResourceOperation": { ... },
},
},
// A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and
// delete file / folder operations.
//
// Whether clients honor this property depends on the client capability `workspace.changeAnnotationSupport`.
//
// @since 3.16.0
"changeAnnotations": map[string]golang.org/x/tools/gopls/internal/lsp/protocol.ChangeAnnotation,
}
```

Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/lsp/command/command_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions gopls/internal/lsp/command/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Interface interface {
// ApplyFix: Apply a fix
//
// Applies a fix to a region of source code.
ApplyFix(context.Context, ApplyFixArgs) error
ApplyFix(context.Context, ApplyFixArgs) (*protocol.WorkspaceEdit, error)

// Test: Run test(s) (legacy)
//
Expand Down Expand Up @@ -216,7 +216,7 @@ type Interface interface {
//
// This command is experimental, currently only supporting parameter removal.
// Its signature will certainly change in the future (pun intended).
ChangeSignature(context.Context, ChangeSignatureArgs) error
ChangeSignature(context.Context, ChangeSignatureArgs) (*protocol.WorkspaceEdit, error)

// DiagnoseFiles: Cause server to publish diagnostics for the specified files.
//
Expand Down Expand Up @@ -257,6 +257,8 @@ type ApplyFixArgs struct {
URI protocol.DocumentURI
// The document range to scan for fixes.
Range protocol.Range
// Whether to resolve and return the edits.
ResolveEdits bool
}

type URIArg struct {
Expand Down Expand Up @@ -500,6 +502,8 @@ type AddTelemetryCountersArgs struct {
// ChangeSignatureArgs specifies a "change signature" refactoring to perform.
type ChangeSignatureArgs struct {
RemoveParameter protocol.Location
// Whether to resolve and return the edits.
ResolveEdits bool
}

// DiagnoseFilesArgs specifies a set of files for which diagnostics are wanted.
Expand Down
1 change: 1 addition & 0 deletions gopls/internal/lsp/protocol/generate/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var renameProp = map[prop]string{
{"CancelParams", "id"}: "interface{}",
{"Command", "arguments"}: "[]json.RawMessage",
{"CompletionItem", "textEdit"}: "TextEdit",
{"CodeAction", "data"}: "json.RawMessage", // delay unmarshalling commands
{"Diagnostic", "code"}: "interface{}",
{"Diagnostic", "data"}: "json.RawMessage", // delay unmarshalling quickfixes

Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/protocol/tsprotocol.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 592d9e1

Please sign in to comment.