diff options
-rw-r--r-- | crates/ra_ide/src/lib.rs | 39 | ||||
-rw-r--r-- | crates/rust-analyzer/src/config.rs | 7 | ||||
-rw-r--r-- | crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/diagnostics/to_proto.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/lsp_ext.rs | 19 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop/handlers.rs | 89 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 39 | ||||
-rw-r--r-- | editors/code/src/client.ts | 76 | ||||
-rw-r--r-- | editors/code/src/commands.ts | 19 | ||||
-rw-r--r-- | editors/code/src/lsp_ext.ts | 7 | ||||
-rw-r--r-- | editors/code/src/main.ts | 1 |
13 files changed, 215 insertions, 85 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, |
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index d64f9a3f9..a25091f79 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts | |||
@@ -1,8 +1,11 @@ | |||
1 | import * as lc from 'vscode-languageclient'; | 1 | import * as lc from 'vscode-languageclient'; |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import * as ra from '../src/lsp_ext'; | ||
4 | import * as Is from 'vscode-languageclient/lib/utils/is'; | ||
3 | 5 | ||
4 | import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed'; | 6 | import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed'; |
5 | import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed'; | 7 | import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed'; |
8 | import { assert } from './util'; | ||
6 | 9 | ||
7 | export function createClient(serverPath: string, cwd: string): lc.LanguageClient { | 10 | export function createClient(serverPath: string, cwd: string): lc.LanguageClient { |
8 | // '.' Is the fallback if no folder is open | 11 | // '.' Is the fallback if no folder is open |
@@ -32,6 +35,8 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient | |||
32 | if (res === undefined) throw new Error('busy'); | 35 | if (res === undefined) throw new Error('busy'); |
33 | return res; | 36 | return res; |
34 | }, | 37 | }, |
38 | // Using custom handling of CodeActions where each code action is resloved lazily | ||
39 | // That's why we are not waiting for any command or edits | ||
35 | async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) { | 40 | async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) { |
36 | const params: lc.CodeActionParams = { | 41 | const params: lc.CodeActionParams = { |
37 | textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), | 42 | textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), |
@@ -43,32 +48,38 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient | |||
43 | const result: (vscode.CodeAction | vscode.Command)[] = []; | 48 | const result: (vscode.CodeAction | vscode.Command)[] = []; |
44 | const groups = new Map<string, { index: number; items: vscode.CodeAction[] }>(); | 49 | const groups = new Map<string, { index: number; items: vscode.CodeAction[] }>(); |
45 | for (const item of values) { | 50 | for (const item of values) { |
51 | // In our case we expect to get code edits only from diagnostics | ||
46 | if (lc.CodeAction.is(item)) { | 52 | if (lc.CodeAction.is(item)) { |
53 | assert(!item.command, "We don't expect to receive commands in CodeActions"); | ||
47 | const action = client.protocol2CodeConverter.asCodeAction(item); | 54 | const action = client.protocol2CodeConverter.asCodeAction(item); |
48 | const group = actionGroup(item); | 55 | result.push(action); |
49 | if (isSnippetEdit(item) || group) { | 56 | continue; |
50 | action.command = { | 57 | } |
51 | command: "rust-analyzer.applySnippetWorkspaceEdit", | 58 | assert(isCodeActionWithoutEditsAndCommands(item), "We don't expect edits or commands here"); |
52 | title: "", | 59 | const action = new vscode.CodeAction(item.title); |
53 | arguments: [action.edit], | 60 | const group = (item as any).group; |
54 | }; | 61 | const id = (item as any).id; |
55 | action.edit = undefined; | 62 | const resolveParams: ra.ResolveCodeActionParams = { |
56 | } | 63 | id: id, |
57 | 64 | // TODO: delete after discussions if needed | |
58 | if (group) { | 65 | label: item.title, |
59 | let entry = groups.get(group); | 66 | codeActionParams: params |
60 | if (!entry) { | 67 | }; |
61 | entry = { index: result.length, items: [] }; | 68 | action.command = { |
62 | groups.set(group, entry); | 69 | command: "rust-analyzer.resolveCodeAction", |
63 | result.push(action); | 70 | title: item.title, |
64 | } | 71 | arguments: [resolveParams], |
65 | entry.items.push(action); | 72 | }; |
66 | } else { | 73 | if (group) { |
74 | let entry = groups.get(group); | ||
75 | if (!entry) { | ||
76 | entry = { index: result.length, items: [] }; | ||
77 | groups.set(group, entry); | ||
67 | result.push(action); | 78 | result.push(action); |
68 | } | 79 | } |
80 | entry.items.push(action); | ||
69 | } else { | 81 | } else { |
70 | const command = client.protocol2CodeConverter.asCommand(item); | 82 | result.push(action); |
71 | result.push(command); | ||
72 | } | 83 | } |
73 | } | 84 | } |
74 | for (const [group, { index, items }] of groups) { | 85 | for (const [group, { index, items }] of groups) { |
@@ -80,7 +91,7 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient | |||
80 | command: "rust-analyzer.applyActionGroup", | 91 | command: "rust-analyzer.applyActionGroup", |
81 | title: "", | 92 | title: "", |
82 | arguments: [items.map((item) => { | 93 | arguments: [items.map((item) => { |
83 | return { label: item.title, edit: item.command!!.arguments!![0] }; | 94 | return { label: item.title, arguments: item.command!!.arguments!![0] }; |
84 | })], | 95 | })], |
85 | }; | 96 | }; |
86 | result[index] = action; | 97 | result[index] = action; |
@@ -119,24 +130,17 @@ class ExperimentalFeatures implements lc.StaticFeature { | |||
119 | const caps: any = capabilities.experimental ?? {}; | 130 | const caps: any = capabilities.experimental ?? {}; |
120 | caps.snippetTextEdit = true; | 131 | caps.snippetTextEdit = true; |
121 | caps.codeActionGroup = true; | 132 | caps.codeActionGroup = true; |
133 | caps.resolveCodeAction = true; | ||
122 | capabilities.experimental = caps; | 134 | capabilities.experimental = caps; |
123 | } | 135 | } |
124 | initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void { | 136 | initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void { |
125 | } | 137 | } |
126 | } | 138 | } |
127 | 139 | ||
128 | function isSnippetEdit(action: lc.CodeAction): boolean { | 140 | function isCodeActionWithoutEditsAndCommands(value: any): boolean { |
129 | const documentChanges = action.edit?.documentChanges ?? []; | 141 | const candidate: lc.CodeAction = value; |
130 | for (const edit of documentChanges) { | 142 | return candidate && Is.string(candidate.title) && |
131 | if (lc.TextDocumentEdit.is(edit)) { | 143 | (candidate.diagnostics === void 0 || Is.typedArray(candidate.diagnostics, lc.Diagnostic.is)) && |
132 | if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) { | 144 | (candidate.kind === void 0 || Is.string(candidate.kind)) && |
133 | return true; | 145 | (candidate.edit === void 0 && candidate.command === void 0); |
134 | } | ||
135 | } | ||
136 | } | ||
137 | return false; | ||
138 | } | ||
139 | |||
140 | function actionGroup(action: lc.CodeAction): string | undefined { | ||
141 | return (action as any).group; | ||
142 | } | 146 | } |
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 534d2a984..3e9c3aa0e 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts | |||
@@ -343,10 +343,25 @@ export function showReferences(ctx: Ctx): Cmd { | |||
343 | } | 343 | } |
344 | 344 | ||
345 | export function applyActionGroup(_ctx: Ctx): Cmd { | 345 | export function applyActionGroup(_ctx: Ctx): Cmd { |
346 | return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => { | 346 | return async (actions: { label: string; arguments: ra.ResolveCodeActionParams }[]) => { |
347 | const selectedAction = await vscode.window.showQuickPick(actions); | 347 | const selectedAction = await vscode.window.showQuickPick(actions); |
348 | if (!selectedAction) return; | 348 | if (!selectedAction) return; |
349 | await applySnippetWorkspaceEdit(selectedAction.edit); | 349 | vscode.commands.executeCommand( |
350 | 'rust-analyzer.resolveCodeAction', | ||
351 | selectedAction.arguments, | ||
352 | ); | ||
353 | }; | ||
354 | } | ||
355 | |||
356 | export function resolveCodeAction(ctx: Ctx): Cmd { | ||
357 | const client = ctx.client; | ||
358 | return async (params: ra.ResolveCodeActionParams) => { | ||
359 | const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params); | ||
360 | if (!item) { | ||
361 | return; | ||
362 | } | ||
363 | const edit = client.protocol2CodeConverter.asWorkspaceEdit(item); | ||
364 | await applySnippetWorkspaceEdit(edit); | ||
350 | }; | 365 | }; |
351 | } | 366 | } |
352 | 367 | ||
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index c51acfccb..35d73ce31 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts | |||
@@ -33,6 +33,13 @@ export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position | |||
33 | 33 | ||
34 | export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule"); | 34 | export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule"); |
35 | 35 | ||
36 | export interface ResolveCodeActionParams { | ||
37 | id: string; | ||
38 | label: string; | ||
39 | codeActionParams: lc.CodeActionParams; | ||
40 | } | ||
41 | export const resolveCodeAction = new lc.RequestType<ResolveCodeActionParams, lc.WorkspaceEdit, unknown>('experimental/resolveCodeAction'); | ||
42 | |||
36 | export interface JoinLinesParams { | 43 | export interface JoinLinesParams { |
37 | textDocument: lc.TextDocumentIdentifier; | 44 | textDocument: lc.TextDocumentIdentifier; |
38 | ranges: lc.Range[]; | 45 | ranges: lc.Range[]; |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index b7337621c..a92c676fa 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -98,6 +98,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
98 | ctx.registerCommand('debugSingle', commands.debugSingle); | 98 | ctx.registerCommand('debugSingle', commands.debugSingle); |
99 | ctx.registerCommand('showReferences', commands.showReferences); | 99 | ctx.registerCommand('showReferences', commands.showReferences); |
100 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); | 100 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); |
101 | ctx.registerCommand('resolveCodeAction', commands.resolveCodeAction); | ||
101 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); | 102 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); |
102 | 103 | ||
103 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); | 104 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); |