diff options
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop')
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 48 |
1 files changed, 39 insertions, 9 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 @@ | |||
1 | use gen_lsp_server::ErrorCode; | 1 | use gen_lsp_server::ErrorCode; |
2 | use lsp_types::{ | 2 | use 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 | }; |
10 | use ra_ide_api::{ | 10 | use 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 | }; |
13 | use ra_syntax::{AstNode, SyntaxKind, TextUnit}; | 14 | use ra_syntax::{AstNode, SyntaxKind, TextUnit}; |
14 | use rustc_hash::FxHashMap; | 15 | use 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 | ||
603 | pub fn handle_code_lens( | 633 | pub fn handle_code_lens( |