aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs48
-rw-r--r--crates/ra_lsp_server/tests/heavy_tests/main.rs11
2 files changed, 46 insertions, 13 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 9abd4054e..5da731801 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -1,6 +1,6 @@
1use gen_lsp_server::ErrorCode; 1use gen_lsp_server::ErrorCode;
2use lsp_types::{ 2use lsp_types::{
3 CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity, 3 CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity, CodeAction,
4 DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, 4 DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange,
5 FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, 5 FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent,
6 MarkupKind, ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range, 6 MarkupKind, ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range,
@@ -9,6 +9,7 @@ use lsp_types::{
9}; 9};
10use ra_ide_api::{ 10use ra_ide_api::{
11 FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, RunnableKind, Severity, Cancelable, 11 FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, RunnableKind, Severity, Cancelable,
12 AssistId,
12}; 13};
13use ra_syntax::{AstNode, SyntaxKind, TextUnit}; 14use ra_syntax::{AstNode, SyntaxKind, TextUnit};
14use rustc_hash::FxHashMap; 15use rustc_hash::FxHashMap;
@@ -576,28 +577,57 @@ pub fn handle_code_action(
576 let range = params.range.conv_with(&line_index); 577 let range = params.range.conv_with(&line_index);
577 578
578 let assists = world.analysis().assists(FileRange { file_id, range })?.into_iter(); 579 let assists = world.analysis().assists(FileRange { file_id, range })?.into_iter();
579 let fixes = world 580 let diagnostics = world.analysis().diagnostics(file_id)?;
580 .analysis() 581 let mut res: Vec<CodeAction> = Vec::new();
581 .diagnostics(file_id)? 582
583 let fixes_from_diagnostics = diagnostics
582 .into_iter() 584 .into_iter()
583 .filter_map(|d| Some((d.range, d.fix?))) 585 .filter_map(|d| Some((d.range, d.fix?)))
584 .filter(|(diag_range, _fix)| diag_range.intersection(&range).is_some()) 586 .filter(|(diag_range, _fix)| diag_range.intersection(&range).is_some())
585 .map(|(_range, fix)| fix); 587 .map(|(_range, fix)| fix);
586 588
587 let mut res = Vec::new(); 589 for source_edit in fixes_from_diagnostics {
588 for source_edit in assists.chain(fixes) {
589 let title = source_edit.label.clone(); 590 let title = source_edit.label.clone();
590 let edit = source_edit.try_conv_with(&world)?; 591 let edit = source_edit.try_conv_with(&world)?;
591 592
592 let cmd = Command { 593 let command = Command {
594 title,
595 command: "rust-analyzer.applySourceChange".to_string(),
596 arguments: Some(vec![to_value(edit).unwrap()]),
597 };
598 let action = CodeAction {
599 title: command.title.clone(),
600 kind: None,
601 diagnostics: None,
602 edit: None,
603 command: Some(command),
604 };
605 res.push(action);
606 }
607
608 for assist in assists {
609 let title = assist.change.label.clone();
610 let edit = assist.change.try_conv_with(&world)?;
611
612 let command = Command {
593 title, 613 title,
594 command: "rust-analyzer.applySourceChange".to_string(), 614 command: "rust-analyzer.applySourceChange".to_string(),
595 arguments: Some(vec![to_value(edit).unwrap()]), 615 arguments: Some(vec![to_value(edit).unwrap()]),
596 }; 616 };
597 res.push(cmd); 617 let action = CodeAction {
618 title: command.title.clone(),
619 kind: match assist.id {
620 AssistId("introduce_variable") => Some("refactor.extract.variable".to_string()),
621 _ => None,
622 },
623 diagnostics: None,
624 edit: None,
625 command: Some(command),
626 };
627 res.push(action);
598 } 628 }
599 629
600 Ok(Some(CodeActionResponse::Commands(res))) 630 Ok(Some(CodeActionResponse::Actions(res)))
601} 631}
602 632
603pub fn handle_code_lens( 633pub fn handle_code_lens(
diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs
index e49c87169..996bf8e01 100644
--- a/crates/ra_lsp_server/tests/heavy_tests/main.rs
+++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs
@@ -225,10 +225,12 @@ fn main() {}
225 context: empty_context(), 225 context: empty_context(),
226 }, 226 },
227 json!([ 227 json!([
228 { 228 {
229 "command": {
229 "arguments": [ 230 "arguments": [
230 { 231 {
231 "cursorPosition": null, 232 "cursorPosition": null,
233 "label": "create module",
232 "workspaceEdit": { 234 "workspaceEdit": {
233 "documentChanges": [ 235 "documentChanges": [
234 { 236 {
@@ -236,13 +238,14 @@ fn main() {}
236 "uri": "file:///[..]/src/bar.rs" 238 "uri": "file:///[..]/src/bar.rs"
237 } 239 }
238 ] 240 ]
239 }, 241 }
240 "label": "create module"
241 } 242 }
242 ], 243 ],
243 "command": "rust-analyzer.applySourceChange", 244 "command": "rust-analyzer.applySourceChange",
244 "title": "create module" 245 "title": "create module"
245 } 246 },
247 "title": "create module"
248 }
246 ]), 249 ]),
247 ); 250 );
248 251