diff options
Diffstat (limited to 'crates/ide_assists/src/assist_context.rs')
-rw-r--r-- | crates/ide_assists/src/assist_context.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/crates/ide_assists/src/assist_context.rs b/crates/ide_assists/src/assist_context.rs index 8714e4978..4b0bba2ab 100644 --- a/crates/ide_assists/src/assist_context.rs +++ b/crates/ide_assists/src/assist_context.rs | |||
@@ -185,7 +185,29 @@ pub(crate) struct AssistBuilder { | |||
185 | source_change: SourceChange, | 185 | source_change: SourceChange, |
186 | 186 | ||
187 | /// Maps the original, immutable `SyntaxNode` to a `clone_for_update` twin. | 187 | /// Maps the original, immutable `SyntaxNode` to a `clone_for_update` twin. |
188 | mutated_tree: Option<(SyntaxNode, SyntaxNode)>, | 188 | mutated_tree: Option<TreeMutator>, |
189 | } | ||
190 | |||
191 | pub(crate) struct TreeMutator { | ||
192 | immutable: SyntaxNode, | ||
193 | mutable_clone: SyntaxNode, | ||
194 | } | ||
195 | |||
196 | impl TreeMutator { | ||
197 | pub(crate) fn new(immutable: &SyntaxNode) -> TreeMutator { | ||
198 | let immutable = immutable.ancestors().last().unwrap(); | ||
199 | let mutable_clone = immutable.clone_for_update(); | ||
200 | TreeMutator { immutable, mutable_clone } | ||
201 | } | ||
202 | |||
203 | pub(crate) fn make_mut<N: AstNode>(&self, node: &N) -> N { | ||
204 | N::cast(self.make_syntax_mut(node.syntax())).unwrap() | ||
205 | } | ||
206 | |||
207 | pub(crate) fn make_syntax_mut(&self, node: &SyntaxNode) -> SyntaxNode { | ||
208 | let ptr = SyntaxNodePtr::new(node); | ||
209 | ptr.to_node(&self.mutable_clone) | ||
210 | } | ||
189 | } | 211 | } |
190 | 212 | ||
191 | impl AssistBuilder { | 213 | impl AssistBuilder { |
@@ -204,8 +226,8 @@ impl AssistBuilder { | |||
204 | } | 226 | } |
205 | 227 | ||
206 | fn commit(&mut self) { | 228 | fn commit(&mut self) { |
207 | if let Some((old, new)) = self.mutated_tree.take() { | 229 | if let Some(tm) = self.mutated_tree.take() { |
208 | algo::diff(&old, &new).into_text_edit(&mut self.edit) | 230 | algo::diff(&tm.immutable, &tm.mutable_clone).into_text_edit(&mut self.edit) |
209 | } | 231 | } |
210 | 232 | ||
211 | let edit = mem::take(&mut self.edit).finish(); | 233 | let edit = mem::take(&mut self.edit).finish(); |
@@ -228,16 +250,7 @@ impl AssistBuilder { | |||
228 | /// phase, and then get their mutable couterparts using `make_mut` in the | 250 | /// phase, and then get their mutable couterparts using `make_mut` in the |
229 | /// mutable state. | 251 | /// mutable state. |
230 | pub(crate) fn make_mut(&mut self, node: SyntaxNode) -> SyntaxNode { | 252 | pub(crate) fn make_mut(&mut self, node: SyntaxNode) -> SyntaxNode { |
231 | let root = &self | 253 | self.mutated_tree.get_or_insert_with(|| TreeMutator::new(&node)).make_syntax_mut(&node) |
232 | .mutated_tree | ||
233 | .get_or_insert_with(|| { | ||
234 | let immutable = node.ancestors().last().unwrap(); | ||
235 | let mutable = immutable.clone_for_update(); | ||
236 | (immutable, mutable) | ||
237 | }) | ||
238 | .1; | ||
239 | let ptr = SyntaxNodePtr::new(&&node); | ||
240 | ptr.to_node(root) | ||
241 | } | 254 | } |
242 | 255 | ||
243 | /// Remove specified `range` of text. | 256 | /// Remove specified `range` of text. |