diff options
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 77275156c..871671de2 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -13,7 +13,7 @@ use ra_syntax::{ | |||
13 | }; | 13 | }; |
14 | use ra_text_edit::TextEditBuilder; | 14 | use ra_text_edit::TextEditBuilder; |
15 | 15 | ||
16 | use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist}; | 16 | use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist}; |
17 | 17 | ||
18 | #[derive(Clone, Debug)] | 18 | #[derive(Clone, Debug)] |
19 | pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); | 19 | pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); |
@@ -22,16 +22,16 @@ pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); | |||
22 | pub(crate) struct AssistInfo { | 22 | pub(crate) struct AssistInfo { |
23 | pub(crate) label: AssistLabel, | 23 | pub(crate) label: AssistLabel, |
24 | pub(crate) group_label: Option<GroupLabel>, | 24 | pub(crate) group_label: Option<GroupLabel>, |
25 | pub(crate) action: Option<SourceChange>, | 25 | pub(crate) source_change: Option<SourceChange>, |
26 | } | 26 | } |
27 | 27 | ||
28 | impl AssistInfo { | 28 | impl AssistInfo { |
29 | fn new(label: AssistLabel) -> AssistInfo { | 29 | fn new(label: AssistLabel) -> AssistInfo { |
30 | AssistInfo { label, group_label: None, action: None } | 30 | AssistInfo { label, group_label: None, source_change: None } |
31 | } | 31 | } |
32 | 32 | ||
33 | fn resolved(self, action: SourceChange) -> AssistInfo { | 33 | fn resolved(self, source_change: SourceChange) -> AssistInfo { |
34 | AssistInfo { action: Some(action), ..self } | 34 | AssistInfo { source_change: Some(source_change), ..self } |
35 | } | 35 | } |
36 | 36 | ||
37 | fn with_group(self, group_label: GroupLabel) -> AssistInfo { | 37 | fn with_group(self, group_label: GroupLabel) -> AssistInfo { |
@@ -40,7 +40,7 @@ impl AssistInfo { | |||
40 | 40 | ||
41 | pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> { | 41 | pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> { |
42 | let label = self.label; | 42 | let label = self.label; |
43 | self.action.map(|action| ResolvedAssist { label, action }) | 43 | self.source_change.map(|source_change| ResolvedAssist { label, source_change }) |
44 | } | 44 | } |
45 | } | 45 | } |
46 | 46 | ||
@@ -104,12 +104,12 @@ impl<'a> AssistCtx<'a> { | |||
104 | let change_label = label.label.clone(); | 104 | let change_label = label.label.clone(); |
105 | let mut info = AssistInfo::new(label); | 105 | let mut info = AssistInfo::new(label); |
106 | if self.should_compute_edit { | 106 | if self.should_compute_edit { |
107 | let action = { | 107 | let source_change = { |
108 | let mut edit = ActionBuilder::new(&self); | 108 | let mut edit = ActionBuilder::new(&self); |
109 | f(&mut edit); | 109 | f(&mut edit); |
110 | edit.build(change_label, self.frange.file_id) | 110 | edit.build(change_label) |
111 | }; | 111 | }; |
112 | info = info.resolved(action) | 112 | info = info.resolved(source_change) |
113 | }; | 113 | }; |
114 | 114 | ||
115 | Some(Assist(vec![info])) | 115 | Some(Assist(vec![info])) |
@@ -163,12 +163,12 @@ impl<'a> AssistGroup<'a> { | |||
163 | let change_label = label.label.clone(); | 163 | let change_label = label.label.clone(); |
164 | let mut info = AssistInfo::new(label).with_group(self.group.clone()); | 164 | let mut info = AssistInfo::new(label).with_group(self.group.clone()); |
165 | if self.ctx.should_compute_edit { | 165 | if self.ctx.should_compute_edit { |
166 | let action = { | 166 | let source_change = { |
167 | let mut edit = ActionBuilder::new(&self.ctx); | 167 | let mut edit = ActionBuilder::new(&self.ctx); |
168 | f(&mut edit); | 168 | f(&mut edit); |
169 | edit.build(change_label, self.ctx.frange.file_id) | 169 | edit.build(change_label) |
170 | }; | 170 | }; |
171 | info = info.resolved(action) | 171 | info = info.resolved(source_change) |
172 | }; | 172 | }; |
173 | 173 | ||
174 | self.assists.push(info) | 174 | self.assists.push(info) |
@@ -186,7 +186,7 @@ impl<'a> AssistGroup<'a> { | |||
186 | pub(crate) struct ActionBuilder<'a, 'b> { | 186 | pub(crate) struct ActionBuilder<'a, 'b> { |
187 | edit: TextEditBuilder, | 187 | edit: TextEditBuilder, |
188 | cursor_position: Option<TextSize>, | 188 | cursor_position: Option<TextSize>, |
189 | file: AssistFile, | 189 | file: FileId, |
190 | ctx: &'a AssistCtx<'b>, | 190 | ctx: &'a AssistCtx<'b>, |
191 | } | 191 | } |
192 | 192 | ||
@@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { | |||
195 | Self { | 195 | Self { |
196 | edit: TextEditBuilder::default(), | 196 | edit: TextEditBuilder::default(), |
197 | cursor_position: None, | 197 | cursor_position: None, |
198 | file: AssistFile::default(), | 198 | file: ctx.frange.file_id, |
199 | ctx, | 199 | ctx, |
200 | } | 200 | } |
201 | } | 201 | } |
@@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { | |||
254 | algo::diff(&node, &new).into_text_edit(&mut self.edit) | 254 | algo::diff(&node, &new).into_text_edit(&mut self.edit) |
255 | } | 255 | } |
256 | 256 | ||
257 | pub(crate) fn set_file(&mut self, assist_file: AssistFile) { | 257 | pub(crate) fn set_file(&mut self, assist_file: FileId) { |
258 | self.file = assist_file | 258 | self.file = assist_file; |
259 | } | 259 | } |
260 | 260 | ||
261 | fn build(self, change_label: String, current_file: FileId) -> SourceChange { | 261 | fn build(self, change_label: String) -> SourceChange { |
262 | let edit = self.edit.finish(); | 262 | let edit = self.edit.finish(); |
263 | if edit.is_empty() && self.cursor_position.is_none() { | 263 | if edit.is_empty() && self.cursor_position.is_none() { |
264 | panic!("Only call `add_assist` if the assist can be applied") | 264 | panic!("Only call `add_assist` if the assist can be applied") |
265 | } | 265 | } |
266 | let file = match self.file { | ||
267 | AssistFile::CurrentFile => current_file, | ||
268 | AssistFile::TargetFile(it) => it, | ||
269 | }; | ||
270 | SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } | 266 | SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } |
271 | .into_source_change(file) | 267 | .into_source_change(self.file) |
272 | } | 268 | } |
273 | } | 269 | } |