aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/assist_context.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-04 08:07:06 +0100
committerGitHub <[email protected]>2021-05-04 08:07:06 +0100
commit0323045631e26bc919bcb2e50af2dfae385980ed (patch)
tree9eb9c5cf836fdd36ad39b6b39b47e9a1f5058494 /crates/ide_assists/src/assist_context.rs
parent2350b3f05f96a4a9ed81dc5bde246a1ef1d0da41 (diff)
parentae6b9c25ede237dffc6e4b8767677872b758371f (diff)
Merge #8714
8714: internal: remove one more usage of the rewriter r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/assist_context.rs')
-rw-r--r--crates/ide_assists/src/assist_context.rs39
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
191pub(crate) struct TreeMutator {
192 immutable: SyntaxNode,
193 mutable_clone: SyntaxNode,
194}
195
196impl 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
191impl AssistBuilder { 213impl 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.