diff options
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/expand_macro.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index be0ee03bf..eebae5ebe 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs | |||
@@ -3,9 +3,7 @@ use std::iter; | |||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use ide_db::RootDatabase; | 4 | use ide_db::RootDatabase; |
5 | use syntax::{ | 5 | use syntax::{ |
6 | algo::{find_node_at_offset, SyntaxRewriter}, | 6 | algo::find_node_at_offset, ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, |
7 | ast, AstNode, NodeOrToken, SyntaxKind, | ||
8 | SyntaxKind::*, | ||
9 | SyntaxNode, WalkEvent, T, | 7 | SyntaxNode, WalkEvent, T, |
10 | }; | 8 | }; |
11 | 9 | ||
@@ -46,26 +44,23 @@ fn expand_macro_recur( | |||
46 | sema: &Semantics<RootDatabase>, | 44 | sema: &Semantics<RootDatabase>, |
47 | macro_call: &ast::MacroCall, | 45 | macro_call: &ast::MacroCall, |
48 | ) -> Option<SyntaxNode> { | 46 | ) -> Option<SyntaxNode> { |
49 | let mut expanded = sema.expand(macro_call)?; | 47 | let expanded = sema.expand(macro_call)?.clone_for_update(); |
50 | 48 | ||
51 | let children = expanded.descendants().filter_map(ast::MacroCall::cast); | 49 | let children = expanded.descendants().filter_map(ast::MacroCall::cast); |
52 | let mut rewriter = SyntaxRewriter::default(); | 50 | let mut replacements = Vec::new(); |
53 | 51 | ||
54 | for child in children.into_iter() { | 52 | for child in children { |
55 | if let Some(new_node) = expand_macro_recur(sema, &child) { | 53 | if let Some(new_node) = expand_macro_recur(sema, &child) { |
56 | // Replace the whole node if it is root | 54 | // check if the whole original syntax is replaced |
57 | // `replace_descendants` will not replace the parent node | ||
58 | // but `SyntaxNode::descendants include itself | ||
59 | if expanded == *child.syntax() { | 55 | if expanded == *child.syntax() { |
60 | expanded = new_node; | 56 | return Some(new_node); |
61 | } else { | ||
62 | rewriter.replace(child.syntax(), &new_node) | ||
63 | } | 57 | } |
58 | replacements.push((child, new_node)); | ||
64 | } | 59 | } |
65 | } | 60 | } |
66 | 61 | ||
67 | let res = rewriter.rewrite(&expanded); | 62 | replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new)); |
68 | Some(res) | 63 | Some(expanded) |
69 | } | 64 | } |
70 | 65 | ||
71 | // FIXME: It would also be cool to share logic here and in the mbe tests, | 66 | // FIXME: It would also be cool to share logic here and in the mbe tests, |