diff options
Diffstat (limited to 'crates')
9 files changed, 124 insertions, 46 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 0e5dc56fd..23168c3ae 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs | |||
@@ -123,6 +123,7 @@ pub struct ClientCapsConfig { | |||
123 | pub code_action_literals: bool, | 123 | pub code_action_literals: bool, |
124 | pub work_done_progress: bool, | 124 | pub work_done_progress: bool, |
125 | pub code_action_group: bool, | 125 | pub code_action_group: bool, |
126 | pub resolve_code_action: bool, | ||
126 | } | 127 | } |
127 | 128 | ||
128 | impl Default for Config { | 129 | impl Default for Config { |
@@ -336,7 +337,11 @@ impl Config { | |||
336 | 337 | ||
337 | let code_action_group = | 338 | let code_action_group = |
338 | experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true); | 339 | experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true); |
339 | self.client_caps.code_action_group = code_action_group | 340 | self.client_caps.code_action_group = code_action_group; |
341 | |||
342 | let resolve_code_action = | ||
343 | experimental.get("resolveCodeAction").and_then(|it| it.as_bool()) == Some(true); | ||
344 | self.client_caps.resolve_code_action = resolve_code_action; | ||
340 | } | 345 | } |
341 | } | 346 | } |
342 | } | 347 | } |
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 33b516e26..f0273315e 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 6a6e7b457..04e286780 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..3b957534d 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -97,6 +97,22 @@ 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 | } | ||
115 | |||
100 | pub enum OnEnter {} | 116 | pub enum OnEnter {} |
101 | 117 | ||
102 | impl Request for OnEnter { | 118 | impl Request for OnEnter { |
@@ -202,6 +218,8 @@ impl Request for CodeActionRequest { | |||
202 | pub struct CodeAction { | 218 | pub struct CodeAction { |
203 | pub title: String, | 219 | pub title: String, |
204 | #[serde(skip_serializing_if = "Option::is_none")] | 220 | #[serde(skip_serializing_if = "Option::is_none")] |
221 | pub id: Option<String>, | ||
222 | #[serde(skip_serializing_if = "Option::is_none")] | ||
205 | pub group: Option<String>, | 223 | pub group: Option<String>, |
206 | #[serde(skip_serializing_if = "Option::is_none")] | 224 | #[serde(skip_serializing_if = "Option::is_none")] |
207 | pub kind: Option<String>, | 225 | pub kind: Option<String>, |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 1f8f6b978..e60337b8e 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -509,6 +509,7 @@ fn on_request( | |||
509 | .on::<lsp_ext::Runnables>(handlers::handle_runnables)? | 509 | .on::<lsp_ext::Runnables>(handlers::handle_runnables)? |
510 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)? | 510 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)? |
511 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? | 511 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? |
512 | .on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)? | ||
512 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? | 513 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? |
513 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? | 514 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? |
514 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)? | 515 | .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 c2a5bf4d6..a3361d6dc 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs | |||
@@ -701,37 +701,27 @@ pub fn handle_formatting( | |||
701 | }])) | 701 | }])) |
702 | } | 702 | } |
703 | 703 | ||
704 | pub fn handle_code_action( | 704 | fn handle_fixes( |
705 | snap: GlobalStateSnapshot, | 705 | snap: &GlobalStateSnapshot, |
706 | params: lsp_types::CodeActionParams, | 706 | params: &lsp_types::CodeActionParams, |
707 | ) -> Result<Option<Vec<lsp_ext::CodeAction>>> { | 707 | res: &mut Vec<lsp_ext::CodeAction>, |
708 | let _p = profile("handle_code_action"); | 708 | ) -> Result<()> { |
709 | // We intentionally don't support command-based actions, as those either | ||
710 | // requires custom client-code anyway, or requires server-initiated edits. | ||
711 | // Server initiated edits break causality, so we avoid those as well. | ||
712 | if !snap.config.client_caps.code_action_literals { | ||
713 | return Ok(None); | ||
714 | } | ||
715 | |||
716 | let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; | 709 | let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; |
717 | let line_index = snap.analysis().file_line_index(file_id)?; | 710 | let line_index = snap.analysis().file_line_index(file_id)?; |
718 | let range = from_proto::text_range(&line_index, params.range); | 711 | let range = from_proto::text_range(&line_index, params.range); |
719 | let frange = FileRange { file_id, range }; | ||
720 | |||
721 | let diagnostics = snap.analysis().diagnostics(file_id)?; | 712 | let diagnostics = snap.analysis().diagnostics(file_id)?; |
722 | let mut res: Vec<lsp_ext::CodeAction> = Vec::new(); | ||
723 | 713 | ||
724 | let fixes_from_diagnostics = diagnostics | 714 | let fixes_from_diagnostics = diagnostics |
725 | .into_iter() | 715 | .into_iter() |
726 | .filter_map(|d| Some((d.range, d.fix?))) | 716 | .filter_map(|d| Some((d.range, d.fix?))) |
727 | .filter(|(diag_range, _fix)| diag_range.intersect(range).is_some()) | 717 | .filter(|(diag_range, _fix)| diag_range.intersect(range).is_some()) |
728 | .map(|(_range, fix)| fix); | 718 | .map(|(_range, fix)| fix); |
729 | |||
730 | for fix in fixes_from_diagnostics { | 719 | for fix in fixes_from_diagnostics { |
731 | let title = fix.label; | 720 | let title = fix.label; |
732 | let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?; | 721 | let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?; |
733 | let action = lsp_ext::CodeAction { | 722 | let action = lsp_ext::CodeAction { |
734 | title, | 723 | title, |
724 | id: None, | ||
735 | group: None, | 725 | group: None, |
736 | kind: Some(lsp_types::code_action_kind::QUICKFIX.into()), | 726 | kind: Some(lsp_types::code_action_kind::QUICKFIX.into()), |
737 | edit: Some(edit), | 727 | edit: Some(edit), |
@@ -747,13 +737,63 @@ pub fn handle_code_action( | |||
747 | } | 737 | } |
748 | res.push(fix.action.clone()); | 738 | res.push(fix.action.clone()); |
749 | } | 739 | } |
740 | Ok(()) | ||
741 | } | ||
742 | |||
743 | pub fn handle_code_action( | ||
744 | snap: GlobalStateSnapshot, | ||
745 | params: lsp_types::CodeActionParams, | ||
746 | ) -> Result<Option<Vec<lsp_ext::CodeAction>>> { | ||
747 | let _p = profile("handle_code_action"); | ||
748 | // We intentionally don't support command-based actions, as those either | ||
749 | // requires custom client-code anyway, or requires server-initiated edits. | ||
750 | // Server initiated edits break causality, so we avoid those as well. | ||
751 | if !snap.config.client_caps.code_action_literals { | ||
752 | return Ok(None); | ||
753 | } | ||
754 | |||
755 | let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; | ||
756 | let line_index = snap.analysis().file_line_index(file_id)?; | ||
757 | let range = from_proto::text_range(&line_index, params.range); | ||
758 | let frange = FileRange { file_id, range }; | ||
759 | let mut res: Vec<lsp_ext::CodeAction> = Vec::new(); | ||
750 | 760 | ||
751 | for assist in snap.analysis().assists(&snap.config.assist, frange)?.into_iter() { | 761 | handle_fixes(&snap, ¶ms, &mut res)?; |
752 | res.push(to_proto::code_action(&snap, assist)?.into()); | 762 | |
763 | if snap.config.client_caps.resolve_code_action { | ||
764 | for (index, assist) in | ||
765 | snap.analysis().unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate() | ||
766 | { | ||
767 | res.push(to_proto::unresolved_code_action(&snap, assist, index)?); | ||
768 | } | ||
769 | } else { | ||
770 | for assist in snap.analysis().resolved_assists(&snap.config.assist, frange)?.into_iter() { | ||
771 | res.push(to_proto::resolved_code_action(&snap, assist)?); | ||
772 | } | ||
753 | } | 773 | } |
774 | |||
754 | Ok(Some(res)) | 775 | Ok(Some(res)) |
755 | } | 776 | } |
756 | 777 | ||
778 | pub fn handle_resolve_code_action( | ||
779 | snap: GlobalStateSnapshot, | ||
780 | params: lsp_ext::ResolveCodeActionParams, | ||
781 | ) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> { | ||
782 | let _p = profile("handle_resolve_code_action"); | ||
783 | let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?; | ||
784 | let line_index = snap.analysis().file_line_index(file_id)?; | ||
785 | let range = from_proto::text_range(&line_index, params.code_action_params.range); | ||
786 | let frange = FileRange { file_id, range }; | ||
787 | |||
788 | let assists = snap.analysis().resolved_assists(&snap.config.assist, frange)?; | ||
789 | let id_components = params.id.split(":").collect::<Vec<&str>>(); | ||
790 | let index = id_components.last().unwrap().parse::<usize>().unwrap(); | ||
791 | let id_string = id_components.first().unwrap(); | ||
792 | let assist = &assists[index]; | ||
793 | assert!(assist.assist.id.0 == *id_string); | ||
794 | Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit) | ||
795 | } | ||
796 | |||
757 | pub fn handle_code_lens( | 797 | pub fn handle_code_lens( |
758 | snap: GlobalStateSnapshot, | 798 | snap: GlobalStateSnapshot, |
759 | params: lsp_types::CodeLensParams, | 799 | params: lsp_types::CodeLensParams, |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 0915a7fcb..1da4d80ec 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; |
@@ -623,20 +623,36 @@ fn main() <fold>{ | |||
623 | } | 623 | } |
624 | } | 624 | } |
625 | 625 | ||
626 | pub(crate) fn code_action( | 626 | pub(crate) fn unresolved_code_action( |
627 | snap: &GlobalStateSnapshot, | 627 | snap: &GlobalStateSnapshot, |
628 | assist: Assist, | 628 | assist: Assist, |
629 | index: usize, | ||
629 | ) -> Result<lsp_ext::CodeAction> { | 630 | ) -> Result<lsp_ext::CodeAction> { |
630 | let res = lsp_ext::CodeAction { | 631 | let res = lsp_ext::CodeAction { |
631 | title: assist.label, | 632 | title: assist.label, |
632 | group: if snap.config.client_caps.code_action_group { assist.group_label } else { None }, | 633 | id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())), |
634 | group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0), | ||
633 | kind: Some(String::new()), | 635 | kind: Some(String::new()), |
634 | edit: Some(snippet_workspace_edit(snap, assist.source_change)?), | 636 | edit: None, |
635 | command: None, | 637 | command: None, |
636 | }; | 638 | }; |
637 | Ok(res) | 639 | Ok(res) |
638 | } | 640 | } |
639 | 641 | ||
642 | pub(crate) fn resolved_code_action( | ||
643 | snap: &GlobalStateSnapshot, | ||
644 | assist: ResolvedAssist, | ||
645 | ) -> Result<lsp_ext::CodeAction> { | ||
646 | let change = assist.source_change; | ||
647 | unresolved_code_action(snap, assist.assist, 0).and_then(|it| { | ||
648 | Ok(lsp_ext::CodeAction { | ||
649 | id: None, | ||
650 | edit: Some(snippet_workspace_edit(snap, change)?), | ||
651 | ..it | ||
652 | }) | ||
653 | }) | ||
654 | } | ||
655 | |||
640 | pub(crate) fn runnable( | 656 | pub(crate) fn runnable( |
641 | snap: &GlobalStateSnapshot, | 657 | snap: &GlobalStateSnapshot, |
642 | file_id: FileId, | 658 | file_id: FileId, |