diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-24 16:15:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-24 16:15:08 +0000 |
commit | 9690f6bc43c9624fcad73cb71b3f5a0ffd540ddf (patch) | |
tree | ef8ab1c38150cf22e7c143641ffacabcc5e10c7f /crates/ra_hir_expand/src/eager.rs | |
parent | 7c2cc85806b6b9104e9d614aa71f841fd3627596 (diff) | |
parent | 062f6e3bbeffaa880d7f1c0b59dfad86b40a57a1 (diff) |
Merge #3708
3708: Generalise syntax rewriting infrastructure to allow removal of nodes r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand/src/eager.rs')
-rw-r--r-- | crates/ra_hir_expand/src/eager.rs | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/crates/ra_hir_expand/src/eager.rs b/crates/ra_hir_expand/src/eager.rs index 4cbce4df5..687d40294 100644 --- a/crates/ra_hir_expand/src/eager.rs +++ b/crates/ra_hir_expand/src/eager.rs | |||
@@ -26,8 +26,8 @@ use crate::{ | |||
26 | }; | 26 | }; |
27 | 27 | ||
28 | use ra_parser::FragmentKind; | 28 | use ra_parser::FragmentKind; |
29 | use ra_syntax::{algo::replace_descendants, SyntaxElement, SyntaxNode}; | 29 | use ra_syntax::{algo::SyntaxRewriter, SyntaxNode}; |
30 | use std::{collections::HashMap, sync::Arc}; | 30 | use std::sync::Arc; |
31 | 31 | ||
32 | pub fn expand_eager_macro( | 32 | pub fn expand_eager_macro( |
33 | db: &dyn AstDatabase, | 33 | db: &dyn AstDatabase, |
@@ -95,10 +95,10 @@ fn eager_macro_recur( | |||
95 | curr: InFile<SyntaxNode>, | 95 | curr: InFile<SyntaxNode>, |
96 | macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, | 96 | macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, |
97 | ) -> Option<SyntaxNode> { | 97 | ) -> Option<SyntaxNode> { |
98 | let mut original = curr.value.clone(); | 98 | let original = curr.value.clone(); |
99 | 99 | ||
100 | let children = curr.value.descendants().filter_map(ast::MacroCall::cast); | 100 | let children = curr.value.descendants().filter_map(ast::MacroCall::cast); |
101 | let mut replaces: HashMap<SyntaxElement, SyntaxElement> = HashMap::default(); | 101 | let mut rewriter = SyntaxRewriter::default(); |
102 | 102 | ||
103 | // Collect replacement | 103 | // Collect replacement |
104 | for child in children { | 104 | for child in children { |
@@ -119,12 +119,9 @@ fn eager_macro_recur( | |||
119 | } | 119 | } |
120 | }; | 120 | }; |
121 | 121 | ||
122 | replaces.insert(child.syntax().clone().into(), insert.into()); | 122 | rewriter.replace(child.syntax(), &insert); |
123 | } | 123 | } |
124 | 124 | ||
125 | if !replaces.is_empty() { | 125 | let res = rewriter.rewrite(&original); |
126 | original = replace_descendants(&original, |n| replaces.get(n).cloned()); | 126 | Some(res) |
127 | } | ||
128 | |||
129 | Some(original) | ||
130 | } | 127 | } |