From 67a58e4af16996c79eacabd5f6d80792f0c17cfd Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 22 Nov 2019 02:35:30 +0800 Subject: Add test for match_ast --- crates/ra_ide_api/src/expand_macro.rs | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'crates/ra_ide_api/src/expand_macro.rs') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 7f39262dc..789d6cbde 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -173,6 +173,49 @@ fn some_thing() -> u32 { let a = 0; a+10 } +"###); + } + + #[test] + fn macro_expand_match_ast() { + let res = check_expand_macro( + r#" + //- /lib.rs + macro_rules! match_ast { + (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) }; + + (match ($node:expr) { + $( ast::$ast:ident($it:ident) => $res:block, )* + _ => $catch_all:expr $(,)? + }) => {{ + $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )* + { $catch_all } + }}; + } + + fn main() { + mat<|>ch_ast! { + match container { + ast::TraitDef(it) => {}, + ast::ImplBlock(it) => {}, + _ => { continue }, + } + } + } + "#, + ); + + assert_eq!(res.name, "match_ast"); + assert_snapshot!(res.expansion, @r###" +{ + if let Some(it) = ast::TraitDef::cast(container.clone()){} + else if let Some(it) = ast::ImplBlock::cast(container.clone()){} + else { + { + continue + } + } +} "###); } } -- cgit v1.2.3 From 144dc6652c1bdb0a8a522d86d5bb4be612968675 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 22 Nov 2019 02:35:49 +0800 Subject: Fix insert_whitespaces --- crates/ra_ide_api/src/expand_macro.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide_api/src/expand_macro.rs') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 789d6cbde..7dbf33a16 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -84,24 +84,19 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { }; res += &match token.kind() { - k @ _ - if (k.is_keyword() || k.is_literal() || k == IDENT) - && is_next(|it| !it.is_punct(), true) => - { + k @ _ if is_text(k) && is_next(|it| !it.is_punct(), true) => { token.text().to_string() + " " } L_CURLY if is_next(|it| it != R_CURLY, true) => { indent += 1; - format!(" {{\n{}", " ".repeat(indent)) + let leading_space = if is_last(|it| is_text(it), false) { " " } else { "" }; + format!("{}{{\n{}", leading_space, " ".repeat(indent)) } R_CURLY if is_last(|it| it != L_CURLY, true) => { indent = indent.checked_sub(1).unwrap_or(0); - format!("\n}}{}", " ".repeat(indent)) - } - R_CURLY => { - indent = indent.checked_sub(1).unwrap_or(0); - format!("}}\n{}", " ".repeat(indent)) + format!("\n{}}}", " ".repeat(indent)) } + R_CURLY => format!("}}\n{}", " ".repeat(indent)), T![;] => format!(";\n{}", " ".repeat(indent)), T![->] => " -> ".to_string(), T![=] => " = ".to_string(), @@ -112,7 +107,11 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { last = Some(token.kind()); } - res + return res; + + fn is_text(k: SyntaxKind) -> bool { + k.is_keyword() || k.is_literal() || k == IDENT + } } #[cfg(test)] -- cgit v1.2.3