From 1d7735fbc6795c3ea5f02950b47413e0b35d6677 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 6 Apr 2019 12:12:32 +0800 Subject: Add path test and empty eat_path handling --- crates/ra_mbe/src/lib.rs | 22 ++++++++++++++++++++++ crates/ra_mbe/src/mbe_expander.rs | 5 +++++ crates/ra_mbe/src/tt_cursor.rs | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 4203929d4..a5b7fab52 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -379,4 +379,26 @@ SOURCE_FILE@[0; 40) // [let] [s] [=] ["rust1"] [;] assert_eq!(to_literal(&stm_tokens[15 + 3]).text, "\"rust1\""); } + + /// The following tests are port from intellij-rust directly + /// https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt + + #[test] + fn test_path() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:path) => { + fn foo() { let a = $ i; } + } + } +"#, + ); + assert_expansion(&rules, "foo! { foo }", "fn foo () {let a = foo ;}"); + assert_expansion( + &rules, + "foo! { bar::::baz:: }", + "fn foo () {let a = bar :: < u8 > :: baz :: < u8 > ;}", + ); + } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 2dafd68f6..ce41d7225 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -139,6 +139,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { + let path = + input.eat_path().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(path.into())); + } _ => return Err(ExpandError::UnexpectedToken), } } diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 3128cb9ae..6f619621a 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -78,6 +78,10 @@ impl<'a> TtCursor<'a> { }) } + pub(crate) fn eat_path(&mut self) -> Option { + None + } + pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { if self.at_char(char) { self.bump(); -- cgit v1.2.3