Code Action
Code Action 请求
代码操作请求从客户端发送到服务器,以计算给定文本文档和范围的命令。这些命令通常是代码修复,用于修复问题或美化/重构代码。textDocument/codeAction
请求的结果是命令文本数组,这些文本通常显示在用户界面中。若要确保服务器在许多客户端中有用,代码操作中指定的命令应由服务器处理,而不是由客户端处理(请参阅 workspace/executeCommand
和 ServerCapabilities.executeCommandProvider
)。如果客户端支持通过代码操作提供编辑,则应使用该模式。
从版本 3.16.0 开始:客户端可以提供在 textDocument/codeAction
请求期间服务器延迟 Code Action
计算的属性:
这对于计算属性(例如编辑属性)的值成本高昂的情况非常有用。客户端通过 codeAction.resolveSupport
功能发出此信号,该功能列出了客户端可以延迟解析的所有属性。服务器功能 codeActionProvider.resolveProvider
表示服务器将提供 codeAction/resolve
路由。为了帮助服务器在解析请求中唯一标识代码操作,代码操作文本可以选择携带数据属性。这也由其他客户端功能 codeAction.dataSupport
守护。通常,如果客户端提供解析支持,则应提供数据支持。还应注意,服务器不应更改 codeAction/resolve
请求中代码操作的现有属性。
从版本 3.8.0 开始:支持 CodeAction
以启用以下方案:
-
能够直接从代码操作请求返回工作区编辑。这样可以避免另一个服务器往返来执行实际的代码操作。但是,服务器提供商应该意识到,如果代码操作的计算成本很高,或者编辑量很大,那么如果结果只是一个命令,并且仅在需要时计算实际编辑,则仍然可能是有益的。
-
使用
kind
对代码操作进行分组的能力。允许客户端忽略该信息。但是,它允许他们更好地将代码操作分组到相应的菜单中(例如,将所有重构代码操作都分组到重构菜单中)。
客户端需要通过相应的客户端功能 codeAction.codeActionLiteralSupport
声明其对代码操作文本(例如 CodeAction
类型)和 codeActionKind
的支持。
客户端能力(Client capability):
- 属性路径:
textDocument.codeAction
- 属性类型:
CodeActionClientCapabilities
, 定义如下:
export interface CodeActionClientCapabilities {
/**
* Whether code action supports dynamic registration.
*/
dynamicRegistration?: boolean;
/**
* The client supports code action literals as a valid
* response of the `textDocument/codeAction` request.
*
* @since 3.8.0
*/
codeActionLiteralSupport?: {
/**
* The code action kind is supported with the following value
* set.
*/
codeActionKind: {
/**
* The code action kind values the client supports. When this
* property exists the client also guarantees that it will
* handle values outside its set gracefully and falls back
* to a default value when unknown.
*/
valueSet: CodeActionKind[];
};
};
/**
* Whether code action supports the `isPreferred` property.
*
* @since 3.15.0
*/
isPreferredSupport?: boolean;
/**
* Whether code action supports the `disabled` property.
*
* @since 3.16.0
*/
disabledSupport?: boolean;
/**
* Whether code action supports the `data` property which is
* preserved between a `textDocument/codeAction` and a
* `codeAction/resolve` request.
*
* @since 3.16.0
*/
dataSupport?: boolean;
/**
* Whether the client supports resolving additional code action
* properties via a separate `codeAction/resolve` request.
*
* @since 3.16.0
*/
resolveSupport?: {
/**
* The properties that a client can resolve lazily.
*/
properties: string[];
};
/**
* Whether the client honors the change annotations in
* text edits and resource operations returned via the
* `CodeAction#edit` property by for example presenting
* the workspace edit in the user interface and asking
* for confirmation.
*
* @since 3.16.0
*/
honorsChangeAnnotations?: boolean;
}
服务端能力(Server capability):
- 属性路径:
codeActionProvider
- 属性类型:
boolean | CodeActionOptions
, 定义如下:
export interface CodeActionOptions extends WorkDoneProgressOptions {
/**
* CodeActionKinds that this server may return.
*
* The list of kinds may be generic, such as `CodeActionKind.Refactor`,
* or the server may list out every specific kind they provide.
*/
codeActionKinds?: CodeActionKind[];
/**
* The server provides support to resolve additional
* information for a code action.
*
* @since 3.16.0
*/
resolveProvider?: boolean;
}
注册选项(Registration Options): CodeActionRegistrationOptions
, 定义如下:
export interface CodeActionRegistrationOptions extends
TextDocumentRegistrationOptions, CodeActionOptions {
}
请求(Request):
- method: "textDocument/codeAction"
- params:
CodeActionParams
, 定义如下:
/**
* Params for the CodeActionRequest
*/
export interface CodeActionParams extends WorkDoneProgressParams,
PartialResultParams {
/**
* The document in which the command was invoked.
*/
textDocument: TextDocumentIdentifier;
/**
* The range for which the command was invoked.
*/
range: Range;
/**
* Context carrying additional information.
*/
context: CodeActionContext;
}
/**
* The kind of a code action.
*
* Kinds are a hierarchical list of identifiers separated by `.`,
* e.g. `"refactor.extract.function"`.
*
* The set of kinds is open and client needs to announce the kinds it supports
* to the server during initialization.
*/
export type CodeActionKind = string;
/**
* A set of predefined code action kinds.
*/
export namespace CodeActionKind {
/**
* Empty kind.
*/
export const Empty: CodeActionKind = '';
/**
* Base kind for quickfix actions: 'quickfix'.
*/
export const QuickFix: CodeActionKind = 'quickfix';
/**
* Base kind for refactoring actions: 'refactor'.
*/
export const Refactor: CodeActionKind = 'refactor';
/**
* Base kind for refactoring extraction actions: 'refactor.extract'.
*
* Example extract actions:
*
* - Extract method
* - Extract function
* - Extract variable
* - Extract interface from class
* - ...
*/
export const RefactorExtract: CodeActionKind = 'refactor.extract';
/**
* Base kind for refactoring inline actions: 'refactor.inline'.
*
* Example inline actions:
*
* - Inline function
* - Inline variable
* - Inline constant
* - ...
*/
export const RefactorInline: CodeActionKind = 'refactor.inline';
/**
* Base kind for refactoring rewrite actions: 'refactor.rewrite'.
*
* Example rewrite actions:
*
* - Convert JavaScript function to class
* - Add or remove parameter
* - Encapsulate field
* - Make method static
* - Move method to base class
* - ...
*/
export const RefactorRewrite: CodeActionKind = 'refactor.rewrite';
/**
* Base kind for source actions: `source`.
*
* Source code actions apply to the entire file.
*/
export const Source: CodeActionKind = 'source';
/**
* Base kind for an organize imports source action:
* `source.organizeImports`.
*/
export const SourceOrganizeImports: CodeActionKind =
'source.organizeImports';
/**
* Base kind for a 'fix all' source action: `source.fixAll`.
*
* 'Fix all' actions automatically fix errors that have a clear fix that
* do not require user input. They should not suppress errors or perform
* unsafe fixes such as generating new types or classes.
*
* @since 3.17.0
*/
export const SourceFixAll: CodeActionKind = 'source.fixAll';
}
/**
* Contains additional diagnostic information about the context in which
* a code action is run.
*/
export interface CodeActionContext {
/**
* An array of diagnostics known on the client side overlapping the range
* provided to the `textDocument/codeAction` request. They are provided so
* that the server knows which errors are currently presented to the user
* for the given range. There is no guarantee that these accurately reflect
* the error state of the resource. The primary parameter
* to compute code actions is the provided range.
*/
diagnostics: Diagnostic[];
/**
* Requested kind of actions to return.
*
* Actions not of this kind are filtered out by the client before being
* shown. So servers can omit computing them.
*/
only?: CodeActionKind[];
/**
* The reason why code actions were requested.
*
* @since 3.17.0
*/
triggerKind?: CodeActionTriggerKind;
}
/**
* The reason why code actions were requested.
*
* @since 3.17.0
*/
export namespace CodeActionTriggerKind {
/**
* Code actions were explicitly requested by the user or by an extension.
*/
export const Invoked: 1 = 1;
/**
* Code actions were requested automatically.
*
* This typically happens when current selection in a file changes, but can
* also be triggered when file content changes.
*/
export const Automatic: 2 = 2;
}
export type CodeActionTriggerKind = 1 | 2;
响应(Response):
- result:
(Command | CodeAction)[] | null
, 定义如下:
/**
* A code action represents a change that can be performed in code, e.g. to fix
* a problem or to refactor code.
*
* A CodeAction must set either `edit` and/or a `command`. If both are supplied,
* the `edit` is applied first, then the `command` is executed.
*/
export interface CodeAction {
/**
* A short, human-readable, title for this code action.
*/
title: string;
/**
* The kind of the code action.
*
* Used to filter code actions.
*/
kind?: CodeActionKind;
/**
* The diagnostics that this code action resolves.
*/
diagnostics?: Diagnostic[];
/**
* Marks this as a preferred action. Preferred actions are used by the
* `auto fix` command and can be targeted by keybindings.
*
* A quick fix should be marked preferred if it properly addresses the
* underlying error. A refactoring should be marked preferred if it is the
* most reasonable choice of actions to take.
*
* @since 3.15.0
*/
isPreferred?: boolean;
/**
* Marks that the code action cannot currently be applied.
*
* Clients should follow the following guidelines regarding disabled code
* actions:
*
* - Disabled code actions are not shown in automatic lightbulbs code
* action menus.
*
* - Disabled actions are shown as faded out in the code action menu when
* the user request a more specific type of code action, such as
* refactorings.
*
* - If the user has a keybinding that auto applies a code action and only
* a disabled code actions are returned, the client should show the user
* an error message with `reason` in the editor.
*
* @since 3.16.0
*/
disabled?: {
/**
* Human readable description of why the code action is currently
* disabled.
*
* This is displayed in the code actions UI.
*/
reason: string;
};
/**
* The workspace edit this code action performs.
*/
edit?: WorkspaceEdit;
/**
* A command this code action executes. If a code action
* provides an edit and a command, first the edit is
* executed and then the command.
*/
command?: Command;
/**
* A data entry field that is preserved on a code action between
* a `textDocument/codeAction` and a `codeAction/resolve` request.
*
* @since 3.16.0
*/
data?: LSPAny;
}
- partial result:
(Command | CodeAction)[]
- error:
code
和message
,以防在请求期间发生异常。
Code Action Resolve 请求
请求从客户端发送到服务器,以解析给定代码操作的其他信息。这通常用于计算代码操作的编辑属性,以避免在 textDocument/codeAction
请求期间进行不必要的计算。
假设客户端将 edit 属性宣布为可以使用客户端功能延迟解析的属性
textDocument.codeAction.resolveSupport = { properties: ['edit'] };
一个 CodeAction 是:
{
"title": "Do Foo"
}
需要先使用 codeAction/resolve
请求进行解析,然后才能应用它。
客户端能力(Client capability):
- 属性路径:
textDocument.codeAction.resolveSupport
- 属性类型:
{ properties: string[]; }
请求(Request):
- method: "codeAction/resolve"
- params:
CodeAction
响应(Response):
- result:
CodeAction
- error:
code
和message
,以防在请求期间发生异常。