Support compact macros 2.0 representation

This commit is contained in:
Ingvar Stepanyan
2018-01-25 13:28:55 +00:00
parent 9318b4d2cf
commit 1b9fd01343
2 changed files with 42 additions and 33 deletions

View File

@@ -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)
}

View File

@@ -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();
}
}