Skip to content

Commit

Permalink
lsp: Add source.fixAll to LSP.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Oct 15, 2024
1 parent 89642c9 commit 803a79f
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions autocorrect-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ impl LanguageServer for Backend {
document_formatting_provider: Some(OneOf::Left(true)),
code_action_provider: Some(CodeActionProviderCapability::Options(
CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),
code_action_kinds: Some(vec![
CodeActionKind::QUICKFIX,
CodeActionKind::SOURCE_FIX_ALL,
]),
..Default::default()
},
)),
Expand Down Expand Up @@ -330,11 +333,46 @@ impl LanguageServer for Backend {
self.client
.log_message(
MessageType::INFO,
format!("code_action {}\n", text_document.uri),
format!(
"code_action {:?} {}\n",
context.trigger_kind, text_document.uri
),
)
.await;

let mut response = CodeActionResponse::new();

let fix_all_action = CodeAction {
title: "AutoCorrect (Fix all)".into(),
kind: Some(CodeActionKind::SOURCE_FIX_ALL),
diagnostics: None,
edit: Some(WorkspaceEdit {
changes: Some(
context
.diagnostics
.iter()
.map(|diagnostic| {
let input = diagnostic.message.as_str();
let result = autocorrect::format_for(input, text_document.uri.path());
(
text_document.uri.clone(),
vec![TextEdit {
range: diagnostic.range,
new_text: result.out,
}],
)
})
.collect(),
),
document_changes: None,
change_annotations: None,
}),
command: None,
is_preferred: Some(false),
disabled: None,
data: None,
};

for diagnostic in context.diagnostics.iter() {
let action = CodeAction {
title: diagnostic.source.clone().unwrap_or("AutoCorrect".into()),
Expand All @@ -361,6 +399,7 @@ impl LanguageServer for Backend {
data: None,
};
response.push(CodeActionOrCommand::CodeAction(action));
response.push(CodeActionOrCommand::CodeAction(fix_all_action.clone()))
}
return Ok(Some(response));
}
Expand Down

0 comments on commit 803a79f

Please sign in to comment.