From 1b9fd0134367be836ff53422175cfcff3eeb06d6 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 25 Jan 2018 13:28:55 +0000 Subject: [PATCH] Support compact macros 2.0 representation --- src/macros.rs | 36 ++++++++++++++++++++++++++---------- tests/target/macros.rs | 39 ++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 86ac93d19bed..a15227fb020b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -305,9 +305,16 @@ pub fn rewrite_macro_def( result += " "; result += &ident.name.as_str(); - result += " {"; - let mac_indent = indent.block_indent(context.config); + let multi_branch_style = def.legacy || parsed_def.branches.len() != 1; + + let mac_indent = if multi_branch_style { + result += " {"; + indent.block_indent(context.config) + } else { + indent + }; + let mac_indent_str = mac_indent.to_string(context.config); for branch in parsed_def.branches { @@ -317,11 +324,18 @@ pub fn rewrite_macro_def( return snippet; } - result += "\n"; - result += &mac_indent_str; - result += "("; - result += &format_macro_args(branch.args)?; - result += ") => {\n"; + let args = format!("({})", format_macro_args(branch.args)?); + + if multi_branch_style { + result += "\n"; + result += &mac_indent_str; + result += &args; + result += " =>"; + } else { + result += &args; + } + + result += " {\n"; // The macro body is the most interesting part. It might end up as various // AST nodes, but also has special variables (e.g, `$foo`) which can't be @@ -380,14 +394,16 @@ pub fn rewrite_macro_def( result += &mac_indent_str; result += "}"; - if def.legacy{ + if def.legacy { result += ";"; } result += "\n"; } - result += &indent.to_string(context.config); - result += "}"; + if multi_branch_style { + result += &indent.to_string(context.config); + result += "}"; + } Some(result) } diff --git a/tests/target/macros.rs b/tests/target/macros.rs index 471c90bf4707..82cc76554f5b 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -27,9 +27,8 @@ fn main() { ); kaas!( - // comments - a, // post macro - b // another + /* comments */ a, /* post macro */ + b /* another */ ); trailingcomma!(a, b, c,); @@ -890,29 +889,23 @@ fn macro_in_pattern_position() { }; } -macro foo { - () => { +macro foo() { +} + +pub macro bar($x: ident + $y: expr;) { + fn foo($x: Foo) { + long_function( + a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA), + $x.bar($y), + ); } } -pub macro bar { - ($x: ident + $y: expr;) => { - fn foo($x: Foo) { - long_function( - a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA), - $x.bar($y), - ); - } - } -} - -macro foo { - () => { - // a comment - fn foo() { - // another comment - bar(); - } +macro foo() { + // a comment + fn foo() { + // another comment + bar(); } }