diff options
Diffstat (limited to 'crates')
9 files changed, 150 insertions, 47 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 12d5716e8..34c2d75fe 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs | |||
@@ -77,7 +77,7 @@ pub use crate::{ | |||
77 | }; | 77 | }; |
78 | 78 | ||
79 | pub use hir::Documentation; | 79 | pub use hir::Documentation; |
80 | pub use ra_assists::{AssistConfig, AssistId}; | 80 | pub use ra_assists::{Assist, AssistConfig, AssistId, ResolvedAssist}; |
81 | pub use ra_db::{ | 81 | pub use ra_db::{ |
82 | Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, | 82 | Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId, |
83 | }; | 83 | }; |
@@ -142,14 +142,6 @@ pub struct AnalysisHost { | |||
142 | db: RootDatabase, | 142 | db: RootDatabase, |
143 | } | 143 | } |
144 | 144 | ||
145 | #[derive(Debug)] | ||
146 | pub struct Assist { | ||
147 | pub id: AssistId, | ||
148 | pub label: String, | ||
149 | pub group_label: Option<String>, | ||
150 | pub source_change: SourceChange, | ||
151 | } | ||
152 | |||
153 | impl AnalysisHost { | 145 | impl AnalysisHost { |
154 | pub fn new(lru_capacity: Option<usize>) -> AnalysisHost { | 146 | pub fn new(lru_capacity: Option<usize>) -> AnalysisHost { |
155 | AnalysisHost { db: RootDatabase::new(lru_capacity) } | 147 | AnalysisHost { db: RootDatabase::new(lru_capacity) } |
@@ -470,20 +462,23 @@ impl Analysis { | |||
470 | self.with_db(|db| completion::completions(db, config, position).map(Into::into)) | 462 | self.with_db(|db| completion::completions(db, config, position).map(Into::into)) |
471 | } | 463 | } |
472 | 464 | ||
473 | /// Computes assists (aka code actions aka intentions) for the given | 465 | /// Computes resolved assists with source changes for the given position. |
466 | pub fn resolved_assists( | ||
467 | &self, | ||
468 | config: &AssistConfig, | ||
469 | frange: FileRange, | ||
470 | ) -> Cancelable<Vec<ResolvedAssist>> { | ||
471 | self.with_db(|db| ra_assists::Assist::resolved(db, config, frange)) | ||
472 | } | ||
473 | |||
474 | /// Computes unresolved assists (aka code actions aka intentions) for the given | ||
474 | /// position. | 475 | /// position. |
475 | pub fn assists(&self, config: &AssistConfig, frange: FileRange) -> Cancelable<Vec<Assist>> { | 476 | pub fn unresolved_assists( |
476 | self.with_db(|db| { | 477 | &self, |
477 | ra_assists::Assist::resolved(db, config, frange) | 478 | config: &AssistConfig, |
478 | .into_iter() | 479 | frange: FileRange, |
479 | .map(|assist| Assist { | 480 | ) -> Cancelable<Vec<Assist>> { |
480 | id: assist.assist.id, | 481 | self.with_db(|db| Assist::unresolved(db, config, frange)) |
481 | label: assist.assist.label, | ||
482 | group_label: assist.assist.group.map(|it| it.0), | ||
483 | source_change: assist.source_change, | ||
484 | }) | ||
485 | .collect() | ||
486 | }) | ||
487 | } | 482 | } |
488 | 483 | ||
489 | /// Computes the set of diagnostics for the given file. | 484 | /// Computes the set of diagnostics for the given file. |
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 9c6e369d2..3337078ac 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs | |||
@@ -103,6 +103,7 @@ pub struct ClientCapsConfig { | |||
103 | pub code_action_literals: bool, | 103 | pub code_action_literals: bool, |
104 | pub work_done_progress: bool, | 104 | pub work_done_progress: bool, |
105 | pub code_action_group: bool, | 105 | pub code_action_group: bool, |
106 | pub resolve_code_action: bool, | ||
106 | } | 107 | } |
107 | 108 | ||
108 | impl Default for Config { | 109 | impl Default for Config { |
@@ -299,7 +300,11 @@ impl Config { | |||
299 | 300 | ||
300 | let code_action_group = | 301 | let code_action_group = |
301 | experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true); | 302 | experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true); |
302 | self.client_caps.code_action_group = code_action_group | 303 | self.client_caps.code_action_group = code_action_group; |
304 | |||
305 | let resolve_code_action = | ||
306 | experimental.get("resolveCodeAction").and_then(|it| it.as_bool()) == Some(true); | ||
307 | self.client_caps.resolve_code_action = resolve_code_action; | ||
303 | } | 308 | } |
304 | } | 309 | } |
305 | } | 310 | } |
diff --git a/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap b/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap index c40cfdcdc..272057b47 100644 --- a/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap +++ b/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap | |||
@@ -65,6 +65,7 @@ expression: diag | |||
65 | fixes: [ | 65 | fixes: [ |
66 | CodeAction { | 66 | CodeAction { |
67 | title: "return the expression directly", | 67 | title: "return the expression directly", |
68 | id: None, | ||
68 | group: None, | 69 | group: None, |
69 | kind: Some( | 70 | kind: Some( |
70 | "quickfix", | 71 | "quickfix", |
diff --git a/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap b/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap index 6dd3fcb2e..9a7972ff5 100644 --- a/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap +++ b/crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap | |||
@@ -50,6 +50,7 @@ expression: diag | |||
50 | fixes: [ | 50 | fixes: [ |
51 | CodeAction { | 51 | CodeAction { |
52 | title: "consider prefixing with an underscore", | 52 | title: "consider prefixing with an underscore", |
53 | id: None, | ||
53 | group: None, | 54 | group: None, |
54 | kind: Some( | 55 | kind: Some( |
55 | "quickfix", | 56 | "quickfix", |
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index a500d670a..257910e09 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs | |||
@@ -145,6 +145,7 @@ fn map_rust_child_diagnostic( | |||
145 | } else { | 145 | } else { |
146 | MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction { | 146 | MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction { |
147 | title: rd.message.clone(), | 147 | title: rd.message.clone(), |
148 | id: None, | ||
148 | group: None, | 149 | group: None, |
149 | kind: Some("quickfix".to_string()), | 150 | kind: Some("quickfix".to_string()), |
150 | edit: Some(lsp_ext::SnippetWorkspaceEdit { | 151 | edit: Some(lsp_ext::SnippetWorkspaceEdit { |
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index ec24ce5e0..4b436c301 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -97,6 +97,23 @@ pub struct JoinLinesParams { | |||
97 | pub ranges: Vec<Range>, | 97 | pub ranges: Vec<Range>, |
98 | } | 98 | } |
99 | 99 | ||
100 | pub enum ResolveCodeActionRequest {} | ||
101 | |||
102 | impl Request for ResolveCodeActionRequest { | ||
103 | type Params = ResolveCodeActionParams; | ||
104 | type Result = Option<SnippetWorkspaceEdit>; | ||
105 | const METHOD: &'static str = "experimental/resolveCodeAction"; | ||
106 | } | ||
107 | |||
108 | /// Params for the ResolveCodeActionRequest | ||
109 | #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)] | ||
110 | #[serde(rename_all = "camelCase")] | ||
111 | pub struct ResolveCodeActionParams { | ||
112 | pub code_action_params: lsp_types::CodeActionParams, | ||
113 | pub id: String, | ||
114 | pub label: String, | ||
115 | } | ||
116 | |||
100 | pub enum OnEnter {} | 117 | pub enum OnEnter {} |
101 | 118 | ||
102 | impl Request for OnEnter { | 119 | impl Request for OnEnter { |
@@ -202,6 +219,8 @@ impl Request for CodeActionRequest { | |||
202 | pub struct CodeAction { | 219 | pub struct CodeAction { |
203 | pub title: String, | 220 | pub title: String, |
204 | #[serde(skip_serializing_if = "Option::is_none")] | 221 | #[serde(skip_serializing_if = "Option::is_none")] |
222 | pub id: Option<String>, | ||
223 | #[serde(skip_serializing_if = "Option::is_none")] | ||
205 | pub group: Option<String>, | 224 | pub group: Option<String>, |
206 | #[serde(skip_serializing_if = "Option::is_none")] | 225 | #[serde(skip_serializing_if = "Option::is_none")] |
207 | pub kind: Option<String>, | 226 | pub kind: Option<String>, |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index f1287d52c..ad9dd4c59 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -517,6 +517,7 @@ fn on_request( | |||
517 | .on::<lsp_ext::Runnables>(handlers::handle_runnables)? | 517 | .on::<lsp_ext::Runnables>(handlers::handle_runnables)? |
518 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)? | 518 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)? |
519 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? | 519 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? |
520 | .on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)? | ||
520 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? | 521 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? |
521 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? | 522 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? |
522 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)? | 523 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)? |
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 7fd691764..3c4064441 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs | |||
@@ -696,40 +696,35 @@ pub fn handle_formatting( | |||
696 | }])) | 696 | }])) |
697 | } | 697 | } |
698 | 698 | ||
699 | pub fn handle_code_action( | 699 | fn handle_fixes( |
700 | world: WorldSnapshot, | 700 | world: &WorldSnapshot, |
701 | params: lsp_types::CodeActionParams, | 701 | params: &lsp_types::CodeActionParams, |
702 | ) -> Result<Option<Vec<lsp_ext::CodeAction>>> { | 702 | res: &mut Vec<lsp_ext::CodeAction>, |
703 | let _p = profile("handle_code_action"); | 703 | ) -> Result<()> { |
704 | // We intentionally don't support command-based actions, as those either | ||
705 | // requires custom client-code anyway, or requires server-initiated edits. | ||
706 | // Server initiated edits break causality, so we avoid those as well. | ||
707 | if !world.config.client_caps.code_action_literals { | ||
708 | return Ok(None); | ||
709 | } | ||
710 | |||
711 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; | 704 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; |
712 | let line_index = world.analysis().file_line_index(file_id)?; | 705 | let line_index = world.analysis().file_line_index(file_id)?; |
713 | let range = from_proto::text_range(&line_index, params.range); | 706 | let range = from_proto::text_range(&line_index, params.range); |
714 | let frange = FileRange { file_id, range }; | ||
715 | 707 | ||
716 | let diagnostics = world.analysis().diagnostics(file_id)?; | 708 | let diagnostics = world.analysis().diagnostics(file_id)?; |
717 | let mut res: Vec<lsp_ext::CodeAction> = Vec::new(); | ||
718 | 709 | ||
719 | let fixes_from_diagnostics = diagnostics | 710 | let fixes_from_diagnostics = diagnostics |
720 | .into_iter() | 711 | .into_iter() |
721 | .filter_map(|d| Some((d.range, d.fix?))) | 712 | .filter_map(|d| Some((d.range, d.fix?))) |
722 | .filter(|(diag_range, _fix)| diag_range.intersect(range).is_some()) | 713 | .filter(|(diag_range, _fix)| diag_range.intersect(range).is_some()) |
723 | .map(|(_range, fix)| fix); | 714 | .map(|(_range, fix)| fix); |
724 | |||
725 | for fix in fixes_from_diagnostics { | 715 | for fix in fixes_from_diagnostics { |
726 | let title = fix.label; | 716 | let title = fix.label; |
727 | let edit = to_proto::snippet_workspace_edit(&world, fix.source_change)?; | 717 | let edit = to_proto::snippet_workspace_edit(&world, fix.source_change)?; |
728 | let action = | 718 | let action = lsp_ext::CodeAction { |
729 | lsp_ext::CodeAction { title, group: None, kind: None, edit: Some(edit), command: None }; | 719 | title, |
720 | id: None, | ||
721 | group: None, | ||
722 | kind: None, | ||
723 | edit: Some(edit), | ||
724 | command: None, | ||
725 | }; | ||
730 | res.push(action); | 726 | res.push(action); |
731 | } | 727 | } |
732 | |||
733 | for fix in world.check_fixes.get(&file_id).into_iter().flatten() { | 728 | for fix in world.check_fixes.get(&file_id).into_iter().flatten() { |
734 | let fix_range = from_proto::text_range(&line_index, fix.range); | 729 | let fix_range = from_proto::text_range(&line_index, fix.range); |
735 | if fix_range.intersect(range).is_none() { | 730 | if fix_range.intersect(range).is_none() { |
@@ -737,13 +732,67 @@ pub fn handle_code_action( | |||
737 | } | 732 | } |
738 | res.push(fix.action.clone()); | 733 | res.push(fix.action.clone()); |
739 | } | 734 | } |
735 | Ok(()) | ||
736 | } | ||
740 | 737 | ||
741 | for assist in world.analysis().assists(&world.config.assist, frange)?.into_iter() { | 738 | pub fn handle_code_action( |
742 | res.push(to_proto::code_action(&world, assist)?.into()); | 739 | world: WorldSnapshot, |
740 | params: lsp_types::CodeActionParams, | ||
741 | ) -> Result<Option<Vec<lsp_ext::CodeAction>>> { | ||
742 | let _p = profile("handle_code_action"); | ||
743 | // We intentionally don't support command-based actions, as those either | ||
744 | // requires custom client-code anyway, or requires server-initiated edits. | ||
745 | // Server initiated edits break causality, so we avoid those as well. | ||
746 | if !world.config.client_caps.code_action_literals { | ||
747 | return Ok(None); | ||
748 | } | ||
749 | |||
750 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; | ||
751 | let line_index = world.analysis().file_line_index(file_id)?; | ||
752 | let range = from_proto::text_range(&line_index, params.range); | ||
753 | let frange = FileRange { file_id, range }; | ||
754 | let mut res: Vec<lsp_ext::CodeAction> = Vec::new(); | ||
755 | |||
756 | handle_fixes(&world, ¶ms, &mut res)?; | ||
757 | |||
758 | if world.config.client_caps.resolve_code_action { | ||
759 | for assist in world.analysis().unresolved_assists(&world.config.assist, frange)?.into_iter() | ||
760 | { | ||
761 | res.push(to_proto::unresolved_code_action(&world, assist)?); | ||
762 | } | ||
763 | } else { | ||
764 | for assist in world.analysis().resolved_assists(&world.config.assist, frange)?.into_iter() { | ||
765 | res.push(to_proto::resolved_code_action(&world, assist)?); | ||
766 | } | ||
743 | } | 767 | } |
768 | |||
744 | Ok(Some(res)) | 769 | Ok(Some(res)) |
745 | } | 770 | } |
746 | 771 | ||
772 | pub fn handle_resolve_code_action( | ||
773 | world: WorldSnapshot, | ||
774 | params: lsp_ext::ResolveCodeActionParams, | ||
775 | ) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> { | ||
776 | if !world.config.client_caps.resolve_code_action { | ||
777 | return Ok(None); | ||
778 | } | ||
779 | |||
780 | let _p = profile("handle_resolve_code_action"); | ||
781 | let file_id = from_proto::file_id(&world, ¶ms.code_action_params.text_document.uri)?; | ||
782 | let line_index = world.analysis().file_line_index(file_id)?; | ||
783 | let range = from_proto::text_range(&line_index, params.code_action_params.range); | ||
784 | let frange = FileRange { file_id, range }; | ||
785 | let mut res: Vec<lsp_ext::CodeAction> = Vec::new(); | ||
786 | |||
787 | for assist in world.analysis().resolved_assists(&world.config.assist, frange)?.into_iter() { | ||
788 | res.push(to_proto::resolved_code_action(&world, assist)?); | ||
789 | } | ||
790 | Ok(res | ||
791 | .into_iter() | ||
792 | .find(|action| action.id.clone().unwrap() == params.id && action.title == params.label) | ||
793 | .and_then(|action| action.edit)) | ||
794 | } | ||
795 | |||
747 | pub fn handle_code_lens( | 796 | pub fn handle_code_lens( |
748 | world: WorldSnapshot, | 797 | world: WorldSnapshot, |
749 | params: lsp_types::CodeLensParams, | 798 | params: lsp_types::CodeLensParams, |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 85304aa87..3672b1a26 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange}; | |||
3 | use ra_ide::{ | 3 | use ra_ide::{ |
4 | Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind, | 4 | Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind, |
5 | FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel, | 5 | FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel, |
6 | InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Runnable, | 6 | InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, |
7 | RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit, | 7 | ResolvedAssist, Runnable, RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit, |
8 | }; | 8 | }; |
9 | use ra_syntax::{SyntaxKind, TextRange, TextSize}; | 9 | use ra_syntax::{SyntaxKind, TextRange, TextSize}; |
10 | use ra_vfs::LineEndings; | 10 | use ra_vfs::LineEndings; |
@@ -619,10 +619,41 @@ fn main() <fold>{ | |||
619 | } | 619 | } |
620 | } | 620 | } |
621 | 621 | ||
622 | pub(crate) fn code_action(world: &WorldSnapshot, assist: Assist) -> Result<lsp_ext::CodeAction> { | 622 | pub(crate) fn unresolved_code_action( |
623 | world: &WorldSnapshot, | ||
624 | assist: Assist, | ||
625 | ) -> Result<lsp_ext::CodeAction> { | ||
623 | let res = lsp_ext::CodeAction { | 626 | let res = lsp_ext::CodeAction { |
624 | title: assist.label, | 627 | title: assist.label, |
625 | group: if world.config.client_caps.code_action_group { assist.group_label } else { None }, | 628 | id: Some(assist.id.0.to_owned()), |
629 | group: assist.group.and_then(|it| { | ||
630 | if world.config.client_caps.code_action_group { | ||
631 | None | ||
632 | } else { | ||
633 | Some(it.0) | ||
634 | } | ||
635 | }), | ||
636 | kind: Some(String::new()), | ||
637 | edit: None, | ||
638 | command: None, | ||
639 | }; | ||
640 | Ok(res) | ||
641 | } | ||
642 | |||
643 | pub(crate) fn resolved_code_action( | ||
644 | world: &WorldSnapshot, | ||
645 | assist: ResolvedAssist, | ||
646 | ) -> Result<lsp_ext::CodeAction> { | ||
647 | let res = lsp_ext::CodeAction { | ||
648 | title: assist.assist.label, | ||
649 | id: Some(assist.assist.id.0.to_owned()), | ||
650 | group: assist.assist.group.and_then(|it| { | ||
651 | if world.config.client_caps.code_action_group { | ||
652 | None | ||
653 | } else { | ||
654 | Some(it.0) | ||
655 | } | ||
656 | }), | ||
626 | kind: Some(String::new()), | 657 | kind: Some(String::new()), |
627 | edit: Some(snippet_workspace_edit(world, assist.source_change)?), | 658 | edit: Some(snippet_workspace_edit(world, assist.source_change)?), |
628 | command: None, | 659 | command: None, |