From 299d97b6d98cec673ff056c188ac45a17febc7d4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 24 Apr 2019 23:01:32 +0800 Subject: Add handling `token` seperator in mbe --- crates/ra_mbe/src/syntax_bridge.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'crates/ra_mbe/src/syntax_bridge.rs') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 38a481029..9cca19dbb 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -133,7 +133,9 @@ fn convert_tt( }; let mut token_trees = Vec::new(); - for child in tt.children_with_tokens().skip(skip_first as usize) { + let mut child_iter = tt.children_with_tokens().skip(skip_first as usize).peekable(); + + while let Some(child) = child_iter.next() { if (skip_first && (child == first_child || child == last_child)) || child.kind().is_trivia() { continue; @@ -152,12 +154,25 @@ fn convert_tt( prev = Some(char) } if let Some(char) = prev { - token_trees.push( - tt::Leaf::from(tt::Punct { char, spacing: tt::Spacing::Alone }).into(), - ); + let spacing = match child_iter.peek() { + Some(SyntaxElement::Token(token)) => { + if token.kind().is_punct() { + tt::Spacing::Joint + } else { + tt::Spacing::Alone + } + } + _ => tt::Spacing::Alone, + }; + + token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); } } else { - let child: tt::TokenTree = if token.kind().is_keyword() + let child: tt::TokenTree = if token.kind() == SyntaxKind::TRUE_KW + || token.kind() == SyntaxKind::FALSE_KW + { + tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() + } else if token.kind().is_keyword() || token.kind() == IDENT || token.kind() == LIFETIME { -- cgit v1.2.3