| 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 |
|
| 34 |
|
| 35 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 36 |
pub dynamic_registration: Option<bool>, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 41 |
pub code_action_literal_support: Option<CodeActionLiteralSupport>, |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 47 |
pub is_preferred_support: Option<bool>, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 53 |
pub disabled_support: Option<bool>, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 61 |
pub data_support: Option<bool>, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 68 |
pub resolve_support: Option<CodeActionCapabilityResolveSupport>, |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 78 |
pub honors_change_annotations: Option<bool>, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)] |
| 86 |
#[serde(rename_all = "camelCase")] |
| 87 |
pub struct CodeActionCapabilityResolveSupport { |
| 88 |
|
| 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 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
pub value_set: Vec<String>, |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)] |
| 111 |
#[serde(rename_all = "camelCase")] |
| 112 |
pub struct CodeActionParams { |
| 113 |
|
| 114 |
pub text_document: TextDocumentIdentifier, |
| 115 |
|
| 116 |
|
| 117 |
pub range: Range, |
| 118 |
|
| 119 |
|
| 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 |
|
| 130 |
pub type CodeActionResponse = Vec<CodeActionOrCommand>; |
| 131 |
|
| 132 |
#[allow(clippy::large_enum_variant)] |
| 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 |
|
| 157 |
pub const EMPTY: CodeActionKind = CodeActionKind::new(""); |
| 158 |
|
| 159 |
|
| 160 |
pub const QUICKFIX: CodeActionKind = CodeActionKind::new("quickfix"); |
| 161 |
|
| 162 |
|
| 163 |
pub const REFACTOR: CodeActionKind = CodeActionKind::new("refactor"); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
pub const REFACTOR_EXTRACT: CodeActionKind = CodeActionKind::new("refactor.extract"); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pub const REFACTOR_INLINE: CodeActionKind = CodeActionKind::new("refactor.inline"); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
pub const REFACTOR_REWRITE: CodeActionKind = CodeActionKind::new("refactor.rewrite"); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
pub const SOURCE: CodeActionKind = CodeActionKind::new("source"); |
| 202 |
|
| 203 |
|
| 204 |
pub const SOURCE_ORGANIZE_IMPORTS: CodeActionKind = |
| 205 |
CodeActionKind::new("source.organizeImports"); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 241 |
pub title: String, |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 246 |
pub kind: Option<CodeActionKind>, |
| 247 |
|
| 248 |
|
| 249 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 250 |
pub diagnostics: Option<Vec<Diagnostic>>, |
| 251 |
|
| 252 |
|
| 253 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 254 |
pub edit: Option<WorkspaceEdit>, |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 260 |
pub command: Option<Command>, |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 269 |
pub is_preferred: Option<bool>, |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 288 |
pub disabled: Option<CodeActionDisabled>, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 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 |
|
| 302 |
|
| 303 |
|
| 304 |
pub reason: String, |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#[derive(Eq, PartialEq, Clone, Copy, Deserialize, Serialize)] |
| 311 |
#[serde(transparent)] |
| 312 |
pub struct CodeActionTriggerKind(i32); |
| 313 |
lsp_enum! { |
| 314 |
impl CodeActionTriggerKind { |
| 315 |
|
| 316 |
pub const INVOKED: CodeActionTriggerKind = CodeActionTriggerKind(1); |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
pub const AUTOMATIC: CodeActionTriggerKind = CodeActionTriggerKind(2); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)] |
| 329 |
#[serde(rename_all = "camelCase")] |
| 330 |
pub struct CodeActionContext { |
| 331 |
|
| 332 |
pub diagnostics: Vec<Diagnostic>, |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 339 |
pub only: Option<Vec<CodeActionKind>>, |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 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 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 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 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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 |
} |