From 6794d50a9bd516d1ba49416a57b04e619ecd7074 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 4 Mar 2020 23:38:58 +0800 Subject: Fixed whitespace bug --- crates/ra_mbe/src/syntax_bridge.rs | 7 ++++--- crates/ra_mbe/src/tests.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 2aaf0215f..d8ee74faa 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -388,11 +388,12 @@ impl<'a> TreeSink for TtTreeSink<'a> { return; } + let mut last = self.cursor; for _ in 0..n_tokens { if self.cursor.eof() { break; } - + last = self.cursor; let text: SmolStr = match self.cursor.token_tree() { Some(tt::TokenTree::Leaf(leaf)) => { // Mark the range if needed @@ -441,11 +442,11 @@ impl<'a> TreeSink for TtTreeSink<'a> { self.inner.token(kind, text); // Add whitespace between adjoint puncts - let next = self.cursor.bump(); + let next = last.bump(); if let ( Some(tt::TokenTree::Leaf(tt::Leaf::Punct(curr))), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(_))), - ) = (self.cursor.token_tree(), next.token_tree()) + ) = (last.token_tree(), next.token_tree()) { if curr.spacing == tt::Spacing::Alone { self.inner.token(WHITESPACE, " ".into()); diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 5e6a090aa..304867881 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -838,6 +838,37 @@ fn test_tt_composite() { .assert_expand_items(r#"foo! { => }"#, r#"0"#); } +#[test] +fn test_tt_composite2() { + let node = parse_macro( + r#" + macro_rules! foo { + ($($tt:tt)*) => { abs!(=> $($tt)*) } + } + "#, + ) + .expand_items(r#"foo!{#}"#); + + let res = format!("{:#?}", &node); + assert_eq_text!( + res.trim(), + r###"MACRO_ITEMS@[0; 10) + MACRO_CALL@[0; 10) + PATH@[0; 3) + PATH_SEGMENT@[0; 3) + NAME_REF@[0; 3) + IDENT@[0; 3) "abs" + EXCL@[3; 4) "!" + TOKEN_TREE@[4; 10) + L_PAREN@[4; 5) "(" + EQ@[5; 6) "=" + R_ANGLE@[6; 7) ">" + WHITESPACE@[7; 8) " " + POUND@[8; 9) "#" + R_PAREN@[9; 10) ")""### + ); +} + #[test] fn test_lifetime() { parse_macro( -- cgit v1.2.3