Skip to content

Commit 194d049

Browse files
committed
Support compact macros 2.0 representation
1 parent 74a3e99 commit 194d049

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

src/macros.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,16 @@ pub fn rewrite_macro_def(
305305

306306
result += " ";
307307
result += &ident.name.as_str();
308-
result += " {";
309308

310-
let mac_indent = indent.block_indent(&context.config);
309+
let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
310+
311+
let mac_indent = if multi_branch_style {
312+
result += " {";
313+
indent.block_indent(&context.config)
314+
} else {
315+
indent
316+
};
317+
311318
let mac_indent_str = mac_indent.to_string(&context.config);
312319

313320
for branch in parsed_def.branches {
@@ -317,11 +324,18 @@ pub fn rewrite_macro_def(
317324
return snippet;
318325
}
319326

320-
result += "\n";
321-
result += &mac_indent_str;
322-
result += "(";
323-
result += &format_macro_args(branch.args)?;
324-
result += ") => {\n";
327+
let args = format!("({})", format_macro_args(branch.args)?);
328+
329+
if multi_branch_style {
330+
result += "\n";
331+
result += &mac_indent_str;
332+
result += &args;
333+
result += " =>";
334+
} else {
335+
result += &args;
336+
}
337+
338+
result += " {\n";
325339

326340
// The macro body is the most interesting part. It might end up as various
327341
// AST nodes, but also has special variables (e.g, `$foo`) which can't be
@@ -377,14 +391,16 @@ pub fn rewrite_macro_def(
377391

378392
result += &mac_indent_str;
379393
result += "}";
380-
if def.legacy{
394+
if def.legacy {
381395
result += ";";
382396
}
383397
result += "\n";
384398
}
385399

386-
result += &indent.to_string(&context.config);
387-
result += "}";
400+
if multi_branch_style {
401+
result += &indent.to_string(&context.config);
402+
result += "}";
403+
}
388404

389405
Some(result)
390406
}

tests/target/macros.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ fn main() {
2727
);
2828

2929
kaas!(
30-
// comments
31-
a, // post macro
32-
b // another
30+
/* comments */ a, /* post macro */
31+
b /* another */
3332
);
3433

3534
trailingcomma!(a, b, c,);
@@ -890,28 +889,22 @@ fn macro_in_pattern_position() {
890889
};
891890
}
892891

893-
macro foo {
894-
() => {
895-
}
892+
macro foo() {
896893
}
897894

898-
pub macro bar {
899-
($x: ident + $y: expr;) => {
900-
fn foo($x: Foo) {
901-
long_function(
902-
a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
903-
$x.bar($y),
904-
);
905-
}
895+
pub macro bar($x: ident + $y: expr;) {
896+
fn foo($x: Foo) {
897+
long_function(
898+
a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
899+
$x.bar($y),
900+
);
906901
}
907902
}
908903

909-
macro foo {
910-
() => {
911-
// a comment
912-
fn foo() {
913-
// another comment
914-
bar();
915-
}
904+
macro foo() {
905+
// a comment
906+
fn foo() {
907+
// another comment
908+
bar();
916909
}
917910
}

0 commit comments

Comments
 (0)