Skip to content

Commit

Permalink
Always enforce exactly one space between macro! and braces ({})
Browse files Browse the repository at this point in the history
  • Loading branch information
kestred committed Nov 17, 2018
1 parent 1a3bc79 commit bc5124e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 35 deletions.
25 changes: 10 additions & 15 deletions src/macros.rs
Expand Up @@ -256,19 +256,7 @@ pub fn rewrite_macro_inner(
}
DelimToken::Paren => Some(format!("{}()", macro_name)),
DelimToken::Bracket => Some(format!("{}[]", macro_name)),
DelimToken::Brace => {
// Preserve at most one space before the braces.
let char_after_bang = context
.snippet(mac.span)
.split('!')
.nth(1)
.and_then(|x| x.chars().next());
if let Some(' ') = char_after_bang {
Some(format!("{} {{}}", macro_name))
} else {
Some(format!("{}{{}}", macro_name))
}
}
DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
_ => unreachable!(),
};
}
Expand Down Expand Up @@ -428,8 +416,15 @@ pub fn rewrite_macro_inner(
}
}
DelimToken::Brace => {
// Skip macro invocations with braces, for now.
trim_left_preserve_layout(context.snippet(mac.span), shape.indent, &context.config)
// For macro invocations with braces, always put a space between
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
// anything in between the braces (for now).
let snippet = context.snippet(mac.span);
let macro_raw = snippet.split_at(snippet.find('!')? + 1).1.trim_start();
match trim_left_preserve_layout(macro_raw, &shape.indent, &context.config) {
Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
None => Some(format!("{} {}", macro_name, macro_raw)),
}
}
_ => unreachable!(),
}
Expand Down
4 changes: 1 addition & 3 deletions tests/source/macros.rs
Expand Up @@ -4,7 +4,7 @@ itemmacro!(this, is.now() .formatted(yay));

itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());

itemmacro!{this, is.bracket().formatted()}
itemmacro!{this, is.brace().formatted()}

peg_file! modname ("mygrammarfile.rustpeg");

Expand Down Expand Up @@ -106,8 +106,6 @@ fn main() {

impl X {
empty_invoc!{}

// Don't format empty either!
empty_invoc! {}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/target/issue-2917/packed_simd.rs
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_from_vector {
}
*/

test_if!{
test_if! {
$test_tt:
interpolate_idents! {
mod [$id _from_ $source] {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/lazy_static.rs
Expand Up @@ -10,7 +10,7 @@ lazy_static! {
// We need to be able to format `lazy_static!` without known syntax.
lazy_static!(xxx, yyyy, zzzzz);

lazy_static!{}
lazy_static! {}

// #2354
lazy_static! {
Expand Down
14 changes: 6 additions & 8 deletions tests/target/macros.rs
Expand Up @@ -9,7 +9,7 @@ itemmacro!(
.formatted()
);

itemmacro!{this, is.bracket().formatted()}
itemmacro! {this, is.brace().formatted()}

peg_file! modname("mygrammarfile.rustpeg");

Expand Down Expand Up @@ -94,7 +94,7 @@ fn main() {

foo(makro!(1, 3));

hamkaas!{ () };
hamkaas! { () };

macrowithbraces! {dont, format, me}

Expand All @@ -104,11 +104,11 @@ fn main() {

some_macro![];

some_macro!{
some_macro! {
// comment
};

some_macro!{
some_macro! {
// comment
};

Expand All @@ -131,9 +131,7 @@ fn main() {
}

impl X {
empty_invoc!{}

// Don't format empty either!
empty_invoc! {}
empty_invoc! {}
}

Expand Down Expand Up @@ -952,7 +950,7 @@ macro_rules! m {
};
}
fn foo() {
f!{r#"
f! {r#"
test
"#};
}
Expand Down
14 changes: 7 additions & 7 deletions tests/target/match.rs
Expand Up @@ -204,19 +204,19 @@ fn issue355() {
vec![3; 4]
}
// Funky bracketing styles
t => println!{"a", b},
t => println! {"a", b},
u => vec![1, 2],
v => vec![3; 4],
w => println!["a", b],
x => vec![1, 2],
y => vec![3; 4],
// Brackets with comments
tc => println!{"a", b}, // comment
uc => vec![1, 2], // comment
vc => vec![3; 4], // comment
wc => println!["a", b], // comment
xc => vec![1, 2], // comment
yc => vec![3; 4], // comment
tc => println! {"a", b}, // comment
uc => vec![1, 2], // comment
vc => vec![3; 4], // comment
wc => println!["a", b], // comment
xc => vec![1, 2], // comment
yc => vec![3; 4], // comment
yd => looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(
aaaaaaaaaa, bbbbbbbbbb, cccccccccc, dddddddddd,
),
Expand Down

0 comments on commit bc5124e

Please sign in to comment.