diff options
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 1a65b5dc0..9d533fa0c 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. | 1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. |
2 | use either::Either; | ||
2 | use hir::{db::HirDatabase, InFile, SourceAnalyzer}; | 3 | use hir::{db::HirDatabase, InFile, SourceAnalyzer}; |
3 | use ra_db::FileRange; | 4 | use ra_db::FileRange; |
4 | use ra_fmt::{leading_indent, reindent}; | 5 | use ra_fmt::{leading_indent, reindent}; |
@@ -9,12 +10,12 @@ use ra_syntax::{ | |||
9 | }; | 10 | }; |
10 | use ra_text_edit::TextEditBuilder; | 11 | use ra_text_edit::TextEditBuilder; |
11 | 12 | ||
12 | use crate::{AssistAction, AssistId, AssistLabel}; | 13 | use crate::{AssistAction, AssistId, AssistLabel, ResolvedAssist}; |
13 | 14 | ||
14 | #[derive(Clone, Debug)] | 15 | #[derive(Clone, Debug)] |
15 | pub(crate) enum Assist { | 16 | pub(crate) enum Assist { |
16 | Unresolved { label: AssistLabel }, | 17 | Unresolved { label: AssistLabel }, |
17 | Resolved { label: AssistLabel, action: AssistAction }, | 18 | Resolved { assist: ResolvedAssist }, |
18 | } | 19 | } |
19 | 20 | ||
20 | /// `AssistCtx` allows to apply an assist or check if it could be applied. | 21 | /// `AssistCtx` allows to apply an assist or check if it could be applied. |
@@ -81,18 +82,45 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
81 | self, | 82 | self, |
82 | id: AssistId, | 83 | id: AssistId, |
83 | label: impl Into<String>, | 84 | label: impl Into<String>, |
84 | f: impl FnOnce(&mut AssistBuilder), | 85 | f: impl FnOnce(&mut ActionBuilder), |
85 | ) -> Option<Assist> { | 86 | ) -> Option<Assist> { |
86 | let label = AssistLabel { label: label.into(), id }; | 87 | let label = AssistLabel { label: label.into(), id }; |
87 | assert!(label.label.chars().nth(0).unwrap().is_uppercase()); | 88 | assert!(label.label.chars().nth(0).unwrap().is_uppercase()); |
88 | 89 | ||
89 | let assist = if self.should_compute_edit { | 90 | let assist = if self.should_compute_edit { |
90 | let action = { | 91 | let action = { |
91 | let mut edit = AssistBuilder::default(); | 92 | let mut edit = ActionBuilder::default(); |
92 | f(&mut edit); | 93 | f(&mut edit); |
93 | edit.build() | 94 | edit.build() |
94 | }; | 95 | }; |
95 | Assist::Resolved { label, action } | 96 | Assist::Resolved { assist: ResolvedAssist { label, action_data: Either::Left(action) } } |
97 | } else { | ||
98 | Assist::Unresolved { label } | ||
99 | }; | ||
100 | |||
101 | Some(assist) | ||
102 | } | ||
103 | |||
104 | #[allow(dead_code)] // will be used for auto import assist with multiple actions | ||
105 | pub(crate) fn add_assist_group( | ||
106 | self, | ||
107 | id: AssistId, | ||
108 | label: impl Into<String>, | ||
109 | f: impl FnOnce() -> Vec<ActionBuilder>, | ||
110 | ) -> Option<Assist> { | ||
111 | let label = AssistLabel { label: label.into(), id }; | ||
112 | let assist = if self.should_compute_edit { | ||
113 | let actions = f(); | ||
114 | assert!(!actions.is_empty(), "Assist cannot have no"); | ||
115 | |||
116 | Assist::Resolved { | ||
117 | assist: ResolvedAssist { | ||
118 | label, | ||
119 | action_data: Either::Right( | ||
120 | actions.into_iter().map(ActionBuilder::build).collect(), | ||
121 | ), | ||
122 | }, | ||
123 | } | ||
96 | } else { | 124 | } else { |
97 | Assist::Unresolved { label } | 125 | Assist::Unresolved { label } |
98 | }; | 126 | }; |
@@ -128,13 +156,20 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
128 | } | 156 | } |
129 | 157 | ||
130 | #[derive(Default)] | 158 | #[derive(Default)] |
131 | pub(crate) struct AssistBuilder { | 159 | pub(crate) struct ActionBuilder { |
132 | edit: TextEditBuilder, | 160 | edit: TextEditBuilder, |
133 | cursor_position: Option<TextUnit>, | 161 | cursor_position: Option<TextUnit>, |
134 | target: Option<TextRange>, | 162 | target: Option<TextRange>, |
163 | label: Option<String>, | ||
135 | } | 164 | } |
136 | 165 | ||
137 | impl AssistBuilder { | 166 | impl ActionBuilder { |
167 | #[allow(dead_code)] | ||
168 | /// Adds a custom label to the action, if it needs to be different from the assist label | ||
169 | pub(crate) fn label(&mut self, label: impl Into<String>) { | ||
170 | self.label = Some(label.into()) | ||
171 | } | ||
172 | |||
138 | /// Replaces specified `range` of text with a given string. | 173 | /// Replaces specified `range` of text with a given string. |
139 | pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) { | 174 | pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) { |
140 | self.edit.replace(range, replace_with.into()) | 175 | self.edit.replace(range, replace_with.into()) |
@@ -193,6 +228,7 @@ impl AssistBuilder { | |||
193 | edit: self.edit.finish(), | 228 | edit: self.edit.finish(), |
194 | cursor_position: self.cursor_position, | 229 | cursor_position: self.cursor_position, |
195 | target: self.target, | 230 | target: self.target, |
231 | label: self.label, | ||
196 | } | 232 | } |
197 | } | 233 | } |
198 | } | 234 | } |