Debian Codegraph @

helix-lsp-types/src/code_action.rs

raw symbols SCIP
1 use crate::{
2 Command, Diagnostic, PartialResultParams, Range, TextDocumentIdentifier,
3 WorkDoneProgressOptions, WorkDoneProgressParams, WorkspaceEdit,
4 };
5 use serde::{Deserialize, Serialize};
6
7 use serde_json::Value;
8
9 use std::borrow::Cow;
10 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
11 #[serde(untagged)]
12 pub enum CodeActionProviderCapability {
13 Simple(bool),
14 Options(CodeActionOptions),
15 }
16
17 impl From<CodeActionOptions> for CodeActionProviderCapability {
18 fn from(from: CodeActionOptions) -> Self {
19 Self::Options(from)
20 }
21 }
22
23 impl From<bool> for CodeActionProviderCapability {
24 fn from(from: bool) -> Self {
25 Self::Simple(from)
26 }
27 }
28
29 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
30 #[serde(rename_all = "camelCase")]
31 pub struct CodeActionClientCapabilities {
32 ///
33 /// This capability supports dynamic registration.
34 ///
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub dynamic_registration: Option<bool>,
37
38 /// The client support code action literals as a valid
39 /// response of the `textDocument/codeAction` request.
40 #[serde(skip_serializing_if = "Option::is_none")]
41 pub code_action_literal_support: Option<CodeActionLiteralSupport>,
42
43 /// Whether code action supports the `isPreferred` property.
44 ///
45 /// @since 3.15.0
46 #[serde(skip_serializing_if = "Option::is_none")]
47 pub is_preferred_support: Option<bool>,
48
49 /// Whether code action supports the `disabled` property.
50 ///
51 /// @since 3.16.0
52 #[serde(skip_serializing_if = "Option::is_none")]
53 pub disabled_support: Option<bool>,
54
55 /// Whether code action supports the `data` property which is
56 /// preserved between a `textDocument/codeAction` and a
57 /// `codeAction/resolve` request.
58 ///
59 /// @since 3.16.0
60 #[serde(skip_serializing_if = "Option::is_none")]
61 pub data_support: Option<bool>,
62
63 /// Whether the client supports resolving additional code action
64 /// properties via a separate `codeAction/resolve` request.
65 ///
66 /// @since 3.16.0
67 #[serde(skip_serializing_if = "Option::is_none")]
68 pub resolve_support: Option<CodeActionCapabilityResolveSupport>,
69
70 /// Whether the client honors the change annotations in
71 /// text edits and resource operations returned via the
72 /// `CodeAction#edit` property by for example presenting
73 /// the workspace edit in the user interface and asking
74 /// for confirmation.
75 ///
76 /// @since 3.16.0
77 #[serde(skip_serializing_if = "Option::is_none")]
78 pub honors_change_annotations: Option<bool>,
79 }
80
81 /// Whether the client supports resolving additional code action
82 /// properties via a separate `codeAction/resolve` request.
83 ///
84 /// @since 3.16.0
85 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
86 #[serde(rename_all = "camelCase")]
87 pub struct CodeActionCapabilityResolveSupport {
88 /// The properties that a client can resolve lazily.
89 pub properties: Vec<String>,
90 }
91
92 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
93 #[serde(rename_all = "camelCase")]
94 pub struct CodeActionLiteralSupport {
95 /// The code action kind is support with the following value set.
96 pub code_action_kind: CodeActionKindLiteralSupport,
97 }
98
99 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
100 #[serde(rename_all = "camelCase")]
101 pub struct CodeActionKindLiteralSupport {
102 /// The code action kind values the client supports. When this
103 /// property exists the client also guarantees that it will
104 /// handle values outside its set gracefully and falls back
105 /// to a default value when unknown.
106 pub value_set: Vec<String>,
107 }
108
109 /// Params for the CodeActionRequest
110 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
111 #[serde(rename_all = "camelCase")]
112 pub struct CodeActionParams {
113 /// The document in which the command was invoked.
114 pub text_document: TextDocumentIdentifier,
115
116 /// The range for which the command was invoked.
117 pub range: Range,
118
119 /// Context carrying additional information.
120 pub context: CodeActionContext,
121
122 #[serde(flatten)]
123 pub work_done_progress_params: WorkDoneProgressParams,
124
125 #[serde(flatten)]
126 pub partial_result_params: PartialResultParams,
127 }
128
129 /// response for CodeActionRequest
130 pub type CodeActionResponse = Vec<CodeActionOrCommand>;
131
132 #[allow(clippy::large_enum_variant)] // TODO: In a separate PR attempt the `Box<CodeAction>` pattern.
133 #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
134 #[serde(untagged)]
135 pub enum CodeActionOrCommand {
136 Command(Command),
137 CodeAction(CodeAction),
138 }
139
140 impl From<Command> for CodeActionOrCommand {
141 fn from(command: Command) -> Self {
142 CodeActionOrCommand::Command(command)
143 }
144 }
145
146 impl From<CodeAction> for CodeActionOrCommand {
147 fn from(action: CodeAction) -> Self {
148 CodeActionOrCommand::CodeAction(action)
149 }
150 }
151
152 #[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Clone, Deserialize, Serialize)]
153 pub struct CodeActionKind(Cow<'static, str>);
154
155 impl CodeActionKind {
156 /// Empty kind.
157 pub const EMPTY: CodeActionKind = CodeActionKind::new("");
158
159 /// Base kind for quickfix actions: 'quickfix'
160 pub const QUICKFIX: CodeActionKind = CodeActionKind::new("quickfix");
161
162 /// Base kind for refactoring actions: 'refactor'
163 pub const REFACTOR: CodeActionKind = CodeActionKind::new("refactor");
164
165 /// Base kind for refactoring extraction actions: 'refactor.extract'
166 ///
167 /// Example extract actions:
168 ///
169 /// - Extract method
170 /// - Extract function
171 /// - Extract variable
172 /// - Extract interface from class
173 /// - ...
174 pub const REFACTOR_EXTRACT: CodeActionKind = CodeActionKind::new("refactor.extract");
175
176 /// Base kind for refactoring inline actions: 'refactor.inline'
177 ///
178 /// Example inline actions:
179 ///
180 /// - Inline function
181 /// - Inline variable
182 /// - Inline constant
183 /// - ...
184 pub const REFACTOR_INLINE: CodeActionKind = CodeActionKind::new("refactor.inline");
185
186 /// Base kind for refactoring rewrite actions: 'refactor.rewrite'
187 ///
188 /// Example rewrite actions:
189 ///
190 /// - Convert JavaScript function to class
191 /// - Add or remove parameter
192 /// - Encapsulate field
193 /// - Make method static
194 /// - Move method to base class
195 /// - ...
196 pub const REFACTOR_REWRITE: CodeActionKind = CodeActionKind::new("refactor.rewrite");
197
198 /// Base kind for source actions: `source`
199 ///
200 /// Source code actions apply to the entire file.
201 pub const SOURCE: CodeActionKind = CodeActionKind::new("source");
202
203 /// Base kind for an organize imports source action: `source.organizeImports`
204 pub const SOURCE_ORGANIZE_IMPORTS: CodeActionKind =
205 CodeActionKind::new("source.organizeImports");
206
207 /// Base kind for a 'fix all' source action: `source.fixAll`.
208 ///
209 /// 'Fix all' actions automatically fix errors that have a clear fix that
210 /// do not require user input. They should not suppress errors or perform
211 /// unsafe fixes such as generating new types or classes.
212 ///
213 /// @since 3.17.0
214 pub const SOURCE_FIX_ALL: CodeActionKind = CodeActionKind::new("source.fixAll");
215
216 pub const fn new(tag: &'static str) -> Self {
217 CodeActionKind(Cow::Borrowed(tag))
218 }
219
220 pub fn as_str(&self) -> &str {
221 &self.0
222 }
223 }
224
225 impl From<String> for CodeActionKind {
226 fn from(from: String) -> Self {
227 CodeActionKind(Cow::from(from))
228 }
229 }
230
231 impl From<&'static str> for CodeActionKind {
232 fn from(from: &'static str) -> Self {
233 CodeActionKind::new(from)
234 }
235 }
236
237 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
238 #[serde(rename_all = "camelCase")]
239 pub struct CodeAction {
240 /// A short, human-readable, title for this code action.
241 pub title: String,
242
243 /// The kind of the code action.
244 /// Used to filter code actions.
245 #[serde(skip_serializing_if = "Option::is_none")]
246 pub kind: Option<CodeActionKind>,
247
248 /// The diagnostics that this code action resolves.
249 #[serde(skip_serializing_if = "Option::is_none")]
250 pub diagnostics: Option<Vec<Diagnostic>>,
251
252 /// The workspace edit this code action performs.
253 #[serde(skip_serializing_if = "Option::is_none")]
254 pub edit: Option<WorkspaceEdit>,
255
256 /// A command this code action executes. If a code action
257 /// provides an edit and a command, first the edit is
258 /// executed and then the command.
259 #[serde(skip_serializing_if = "Option::is_none")]
260 pub command: Option<Command>,
261
262 /// Marks this as a preferred action. Preferred actions are used by the `auto fix` command and can be targeted
263 /// by keybindings.
264 /// A quick fix should be marked preferred if it properly addresses the underlying error.
265 /// A refactoring should be marked preferred if it is the most reasonable choice of actions to take.
266 ///
267 /// @since 3.15.0
268 #[serde(skip_serializing_if = "Option::is_none")]
269 pub is_preferred: Option<bool>,
270
271 /// Marks that the code action cannot currently be applied.
272 ///
273 /// Clients should follow the following guidelines regarding disabled code actions:
274 ///
275 /// - Disabled code actions are not shown in automatic
276 /// [lightbulb](https://code.visualstudio.com/docs/editor/editingevolved#_code-action)
277 /// code action menu.
278 ///
279 /// - Disabled actions are shown as faded out in the code action menu when the user request
280 /// a more specific type of code action, such as refactorings.
281 ///
282 /// - If the user has a keybinding that auto applies a code action and only a disabled code
283 /// actions are returned, the client should show the user an error message with `reason`
284 /// in the editor.
285 ///
286 /// @since 3.16.0
287 #[serde(skip_serializing_if = "Option::is_none")]
288 pub disabled: Option<CodeActionDisabled>,
289
290 /// A data entry field that is preserved on a code action between
291 /// a `textDocument/codeAction` and a `codeAction/resolve` request.
292 ///
293 /// @since 3.16.0
294 #[serde(skip_serializing_if = "Option::is_none")]
295 pub data: Option<Value>,
296 }
297
298 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
299 #[serde(rename_all = "camelCase")]
300 pub struct CodeActionDisabled {
301 /// Human readable description of why the code action is currently disabled.
302 ///
303 /// This is displayed in the code actions UI.
304 pub reason: String,
305 }
306
307 /// The reason why code actions were requested.
308 ///
309 /// @since 3.17.0
310 #[derive(Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
311 #[serde(transparent)]
312 pub struct CodeActionTriggerKind(i32);
313 lsp_enum! {
314 impl CodeActionTriggerKind {
315 /// Code actions were explicitly requested by the user or by an extension.
316 pub const INVOKED: CodeActionTriggerKind = CodeActionTriggerKind(1);
317
318 /// Code actions were requested automatically.
319 ///
320 /// This typically happens when current selection in a file changes, but can
321 /// also be triggered when file content changes.
322 pub const AUTOMATIC: CodeActionTriggerKind = CodeActionTriggerKind(2);
323 }
324 }
325
326 /// Contains additional diagnostic information about the context in which
327 /// a code action is run.
328 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
329 #[serde(rename_all = "camelCase")]
330 pub struct CodeActionContext {
331 /// An array of diagnostics.
332 pub diagnostics: Vec<Diagnostic>,
333
334 /// Requested kind of actions to return.
335 ///
336 /// Actions not of this kind are filtered out by the client before being shown. So servers
337 /// can omit computing them.
338 #[serde(skip_serializing_if = "Option::is_none")]
339 pub only: Option<Vec<CodeActionKind>>,
340
341 /// The reason why code actions were requested.
342 ///
343 /// @since 3.17.0
344 #[serde(skip_serializing_if = "Option::is_none")]
345 pub trigger_kind: Option<CodeActionTriggerKind>,
346 }
347
348 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize, Default)]
349 #[serde(rename_all = "camelCase")]
350 pub struct CodeActionOptions {
351 /// CodeActionKinds that this server may return.
352 ///
353 /// The list of kinds may be generic, such as `CodeActionKind.Refactor`, or the server
354 /// may list out every specific kind they provide.
355 #[serde(skip_serializing_if = "Option::is_none")]
356 pub code_action_kinds: Option<Vec<CodeActionKind>>,
357
358 #[serde(flatten)]
359 pub work_done_progress_options: WorkDoneProgressOptions,
360
361 /// The server provides support to resolve additional
362 /// information for a code action.
363 ///
364 /// @since 3.16.0
365 #[serde(skip_serializing_if = "Option::is_none")]
366 pub resolve_provider: Option<bool>,
367 }
368
369 #[cfg(test)]
370 mod tests {
371 use super::*;
372 use crate::tests::test_serialization;
373
374 #[test]
375 fn test_code_action_response() {
376 test_serialization(
377 &vec![
378 CodeActionOrCommand::Command(Command {
379 title: "title".to_string(),
380 command: "command".to_string(),
381 arguments: None,
382 }),
383 CodeActionOrCommand::CodeAction(CodeAction {
384 title: "title".to_string(),
385 kind: Some(CodeActionKind::QUICKFIX),
386 command: None,
387 diagnostics: None,
388 edit: None,
389 is_preferred: None,
390 ..CodeAction::default()
391 }),
392 ],
393 r#"[{"title":"title","command":"command"},{"title":"title","kind":"quickfix"}]"#,
394 )
395 }
396 }