aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assist_ctx.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs41
1 files changed, 27 insertions, 14 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index e240c35d6..e9c4f0a23 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -9,9 +9,10 @@ use ra_fmt::{leading_indent, reindent};
9 9
10use crate::{AssistLabel, AssistAction}; 10use crate::{AssistLabel, AssistAction};
11 11
12#[derive(Clone, Debug)]
12pub(crate) enum Assist { 13pub(crate) enum Assist {
13 Unresolved(AssistLabel), 14 Unresolved(Vec<AssistLabel>),
14 Resolved(AssistLabel, AssistAction), 15 Resolved(Vec<(AssistLabel, AssistAction)>),
15} 16}
16 17
17/// `AssistCtx` allows to apply an assist or check if it could be applied. 18/// `AssistCtx` allows to apply an assist or check if it could be applied.
@@ -50,6 +51,7 @@ pub(crate) struct AssistCtx<'a, DB> {
50 pub(crate) frange: FileRange, 51 pub(crate) frange: FileRange,
51 source_file: &'a SourceFile, 52 source_file: &'a SourceFile,
52 should_compute_edit: bool, 53 should_compute_edit: bool,
54 assist: Assist,
53} 55}
54 56
55impl<'a, DB> Clone for AssistCtx<'a, DB> { 57impl<'a, DB> Clone for AssistCtx<'a, DB> {
@@ -59,6 +61,7 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> {
59 frange: self.frange, 61 frange: self.frange,
60 source_file: self.source_file, 62 source_file: self.source_file,
61 should_compute_edit: self.should_compute_edit, 63 should_compute_edit: self.should_compute_edit,
64 assist: self.assist.clone(),
62 } 65 }
63 } 66 }
64} 67}
@@ -69,25 +72,35 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
69 F: FnOnce(AssistCtx<DB>) -> T, 72 F: FnOnce(AssistCtx<DB>) -> T,
70 { 73 {
71 let source_file = &db.parse(frange.file_id); 74 let source_file = &db.parse(frange.file_id);
72 let ctx = AssistCtx { db, frange, source_file, should_compute_edit }; 75 let assist =
76 if should_compute_edit { Assist::Resolved(vec![]) } else { Assist::Unresolved(vec![]) };
77
78 let ctx = AssistCtx { db, frange, source_file, should_compute_edit, assist };
73 f(ctx) 79 f(ctx)
74 } 80 }
75 81
76 pub(crate) fn build( 82 pub(crate) fn add_action(
77 self, 83 &mut self,
78 label: impl Into<String>, 84 label: impl Into<String>,
79 f: impl FnOnce(&mut AssistBuilder), 85 f: impl FnOnce(&mut AssistBuilder),
80 ) -> Option<Assist> { 86 ) -> &mut Self {
81 let label = AssistLabel { label: label.into() }; 87 let label = AssistLabel { label: label.into() };
82 if !self.should_compute_edit { 88 match &mut self.assist {
83 return Some(Assist::Unresolved(label)); 89 Assist::Unresolved(labels) => labels.push(label),
90 Assist::Resolved(labels_actions) => {
91 let action = {
92 let mut edit = AssistBuilder::default();
93 f(&mut edit);
94 edit.build()
95 };
96 labels_actions.push((label, action));
97 }
84 } 98 }
85 let action = { 99 self
86 let mut edit = AssistBuilder::default(); 100 }
87 f(&mut edit); 101
88 edit.build() 102 pub(crate) fn build(self) -> Option<Assist> {
89 }; 103 Some(self.assist)
90 Some(Assist::Resolved(label, action))
91 } 104 }
92 105
93 pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<&'a SyntaxNode> { 106 pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<&'a SyntaxNode> {