diff options
Diffstat (limited to 'crates/ra_hir/src/macros.rs')
-rw-r--r-- | crates/ra_hir/src/macros.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index 647df4260..9ba381685 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs | |||
@@ -16,6 +16,7 @@ use std::sync::Arc; | |||
16 | 16 | ||
17 | use ra_syntax::{ | 17 | use ra_syntax::{ |
18 | TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, SyntaxNodePtr, | 18 | TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, SyntaxNodePtr, |
19 | SyntaxKind::*, | ||
19 | ast::{self, NameOwner}, | 20 | ast::{self, NameOwner}, |
20 | }; | 21 | }; |
21 | 22 | ||
@@ -201,6 +202,39 @@ pub(crate) fn expand_macro_invocation( | |||
201 | def.expand(input).map(Arc::new) | 202 | def.expand(input).map(Arc::new) |
202 | } | 203 | } |
203 | 204 | ||
204 | fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::TokenTree> { | 205 | fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> { |
205 | None | 206 | let tt = call.token_tree()?; |
207 | convert_tt(tt.syntax()) | ||
208 | } | ||
209 | |||
210 | fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> { | ||
211 | let first_child = tt.first_child()?; | ||
212 | let last_child = tt.last_child()?; | ||
213 | let delimiter = match (first_child.kind(), last_child.kind()) { | ||
214 | (L_PAREN, R_PAREN) => tt::Delimiter::Parenthesis, | ||
215 | (L_CURLY, R_CURLY) => tt::Delimiter::Brace, | ||
216 | (L_BRACK, R_BRACK) => tt::Delimiter::Bracket, | ||
217 | _ => return None, | ||
218 | }; | ||
219 | let mut token_trees = Vec::new(); | ||
220 | for child in tt.children().skip(1) { | ||
221 | if child == first_child || child == last_child || child.kind().is_trivia() { | ||
222 | continue; | ||
223 | } | ||
224 | let child = if child.kind() == TOKEN_TREE { | ||
225 | convert_tt(child)?.into() | ||
226 | } else if child.kind().is_keyword() { | ||
227 | let text = child.leaf_text().unwrap().clone(); | ||
228 | tt::Leaf::from(tt::Ident { text }).into() | ||
229 | } else { | ||
230 | return None; | ||
231 | }; | ||
232 | token_trees.push(child) | ||
233 | } | ||
234 | |||
235 | let res = tt::Subtree { | ||
236 | delimiter, | ||
237 | token_trees, | ||
238 | }; | ||
239 | Some(res) | ||
206 | } | 240 | } |