Skip to content

Commit 5163cbd

Browse files
committed
Fixes #389: Add CodeAction class
1 parent e7d5d77 commit 5163cbd

File tree

2 files changed

+160
-2
lines changed

2 files changed

+160
-2
lines changed

_data/specification-toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@
121121
- title: Change Log
122122
anchor: changeLog
123123
children:
124+
- title: 3.8.0
125+
anchor: version_3_8_0
124126
- title: 3.7.0
125127
anchor: version_3_7_0
126128
- title: 3.6.0

specification.md

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,28 @@ export interface TextDocumentClientCapabilities {
11241124
* Whether code action supports dynamic registration.
11251125
*/
11261126
dynamicRegistration?: boolean;
1127+
/**
1128+
* The client support code action literals as a valid
1129+
* response of the `textDocument/codeAction` request.
1130+
*
1131+
* Since 3.8.0
1132+
*/
1133+
codeActionLiteralSupport?: {
1134+
/**
1135+
* The code action kind is support with the following value
1136+
* set.
1137+
*/
1138+
codeActionKind: {
1139+
1140+
/**
1141+
* The code action kind values the client supports. When this
1142+
* property exists the client also guarantees that it will
1143+
* handle values outside its set gracefully and falls back
1144+
* to a default value when unknown.
1145+
*/
1146+
valueSet: CodeActionKind[];
1147+
};
1148+
};
11271149
};
11281150

11291151
/**
@@ -3115,6 +3137,13 @@ _Registration Options_: `TextDocumentRegistrationOptions`
31153137

31163138
The code action request is sent from the client to the server to compute commands for a given text document and range. These commands are typically code fixes to either fix problems or to beautify/refactor code. The result of a `textDocument/codeAction` request is an array of `Command` literals which are typically presented in the user interface. When the command is selected the server should be contacted again (via the `workspace/executeCommand`) request to execute the command.
31173139

3140+
> *Since version 3.8.0:* support for CodeAction litarals to enable the following scenarios:
3141+
3142+
- the ability to directly return a workspace edit from e code action request. This avoids having another server roundtrip to execute an actual code action. However server providers should be aware that if the code action is expensive to compute or the edits are huge it might still be beneficial if the result is imply a command and the actual edit is only computed when needed.
3143+
- the ability to group code actions using a kind. Clients are allowed to ignore that information. However it allows them to better group code action for example into corresponding menus (e.g. all refactor code actions into a refactor menu).
3144+
3145+
Clients need to announce there support code action literals and code action kinds via the corresponding client capability `textDocument.codeAction.codeActionLiteralSupport`.
3146+
31183147
_Request_:
31193148
* method: 'textDocument/codeAction'
31203149
* params: `CodeActionParams` defined as follows:
@@ -3140,6 +3169,82 @@ interface CodeActionParams {
31403169
context: CodeActionContext;
31413170
}
31423171

3172+
/**
3173+
* The kind of a code action.
3174+
*
3175+
* Kinds are a hierarchical list of identifiers separated by `.`, e.g. `"refactor.extract.function"`.
3176+
*
3177+
* The set of kinds is open and client needs to announce the kinds it supports to the server during
3178+
* initialization.
3179+
*/
3180+
export type CodeActionKind = string;
3181+
3182+
/**
3183+
* A set of predefined code action kinds
3184+
*/
3185+
export namespace CodeActionKind {
3186+
/**
3187+
* Base kind for quickfix actions: 'quickfix'
3188+
*/
3189+
export const QuickFix: CodeActionKind = 'quickfix';
3190+
3191+
/**
3192+
* Base kind for refactoring actions: 'refactor'
3193+
*/
3194+
export const Refactor: CodeActionKind = 'refactor';
3195+
3196+
/**
3197+
* Base kind for refactoring extraction actions: 'refactor.extract'
3198+
*
3199+
* Example extract actions:
3200+
*
3201+
* - Extract method
3202+
* - Extract function
3203+
* - Extract variable
3204+
* - Extract interface from class
3205+
* - ...
3206+
*/
3207+
export const RefactorExtract: CodeActionKind = 'refactor.extract';
3208+
3209+
/**
3210+
* Base kind for refactoring inline actions: 'refactor.inline'
3211+
*
3212+
* Example inline actions:
3213+
*
3214+
* - Inline function
3215+
* - Inline variable
3216+
* - Inline constant
3217+
* - ...
3218+
*/
3219+
export const RefactorInline: CodeActionKind = 'refactor.inline';
3220+
3221+
/**
3222+
* Base kind for refactoring rewrite actions: 'refactor.rewrite'
3223+
*
3224+
* Example rewrite actions:
3225+
*
3226+
* - Convert JavaScript function to class
3227+
* - Add or remove parameter
3228+
* - Encapsulate field
3229+
* - Make method static
3230+
* - Move method to base class
3231+
* - ...
3232+
*/
3233+
export const RefactorRewrite: CodeActionKind = 'refactor.rewrite';
3234+
3235+
/**
3236+
* Base kind for source actions: `source`
3237+
*
3238+
* Source code actions apply to the entire file.
3239+
*/
3240+
export const Source: CodeActionKind = 'source';
3241+
3242+
/**
3243+
* Base kind for an organize imports source action: `source.organizeImports`
3244+
*/
3245+
export const SourceOrganizeImports: CodeActionKind = 'source.organizeImports';
3246+
}
3247+
31433248
/**
31443249
* Contains additional diagnostic information about the context in which
31453250
* a code action is run.
@@ -3149,11 +3254,60 @@ interface CodeActionContext {
31493254
* An array of diagnostics.
31503255
*/
31513256
diagnostics: Diagnostic[];
3257+
3258+
/**
3259+
* Requested kind of actions to return.
3260+
*
3261+
* Actions not of this kind are filtered out by the client before being shown. So servers
3262+
* can omit computing them.
3263+
*/
3264+
only?: CodeActionKind[];
31523265
}
31533266
```
31543267

31553268
_Response_:
3156-
* result: [`Command[]`](#command) \| `null` defined as follows:
3269+
* result: `(Command | CodeAction)[]` \| `null` where `CodeAction` is defined as follows:
3270+
3271+
```typescript
3272+
/**
3273+
* A code action represents a change that can be performed in code, e.g. to fix a problem or
3274+
* to refactor code.
3275+
*
3276+
* A CodeAction must set either `edit` and/or a `command`. If both are supplied, the `edit` is applied first, then the `command` is executed.
3277+
*/
3278+
export interface CodeAction {
3279+
3280+
/**
3281+
* A short, human-readable, title for this code action.
3282+
*/
3283+
title: string;
3284+
3285+
/**
3286+
* The kind of the code action.
3287+
*
3288+
* Used to filter code actions.
3289+
*/
3290+
kind?: CodeActionKind;
3291+
3292+
/**
3293+
* The diagnostics that this code action resolves.
3294+
*/
3295+
diagnostics?: Diagnostic[];
3296+
3297+
/**
3298+
* The workspace edit this code action performs.
3299+
*/
3300+
edit?: WorkspaceEdit;
3301+
3302+
/**
3303+
* A command this code action executes. If a code action
3304+
* provides an edit and a command, first the edit is
3305+
* executed and then the command.
3306+
*/
3307+
command?: Command;
3308+
}
3309+
```
3310+
31573311
* error: code and message set in case an exception happens during the code action request.
31583312

31593313
_Registration Options_: `TextDocumentRegistrationOptions`
@@ -3591,7 +3745,9 @@ _Registration Options_: `TextDocumentRegistrationOptions`
35913745

35923746
### <a name="changeLog" class="anchor"></a>Change Log
35933747

3594-
Below a change log of the last shipped version.
3748+
#### <a name="version_3_8_0" class="anchor"></a>3.8.0 (6/11/2018)
3749+
3750+
* Added support for CodeAction literals to the `textDocument/codeAction` request.
35953751
* ColorServerCapabilities.colorProvider can also be a boolean
35963752
* Corrected ColorPresentationParams.colorInfo to color (as in the d.ts and in implementations)
35973753

0 commit comments

Comments
 (0)