Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3d01fc1

Browse files
authored
Merge pull request rust-lang#3177 from kestred/kstenerson/preserve-brace-leading-ws
Format brace-delimited macros with a space after the name
2 parents 1dd54e6 + bc5124e commit 3d01fc1

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

src/macros.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub fn rewrite_macro_inner(
256256
}
257257
DelimToken::Paren => Some(format!("{}()", macro_name)),
258258
DelimToken::Bracket => Some(format!("{}[]", macro_name)),
259-
DelimToken::Brace => Some(format!("{}{{}}", macro_name)),
259+
DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
260260
_ => unreachable!(),
261261
};
262262
}
@@ -416,8 +416,15 @@ pub fn rewrite_macro_inner(
416416
}
417417
}
418418
DelimToken::Brace => {
419-
// Skip macro invocations with braces, for now.
420-
trim_left_preserve_layout(context.snippet(mac.span), shape.indent, &context.config)
419+
// For macro invocations with braces, always put a space between
420+
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
421+
// anything in between the braces (for now).
422+
let snippet = context.snippet(mac.span);
423+
let macro_raw = snippet.split_at(snippet.find('!')? + 1).1.trim_start();
424+
match trim_left_preserve_layout(macro_raw, &shape.indent, &context.config) {
425+
Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
426+
None => Some(format!("{} {}", macro_name, macro_raw)),
427+
}
421428
}
422429
_ => unreachable!(),
423430
}

tests/source/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ itemmacro!(this, is.now() .formatted(yay));
44

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

7-
itemmacro!{this, is.bracket().formatted()}
7+
itemmacro!{this, is.brace().formatted()}
88

99
peg_file! modname ("mygrammarfile.rustpeg");
1010

@@ -106,6 +106,7 @@ fn main() {
106106

107107
impl X {
108108
empty_invoc!{}
109+
empty_invoc! {}
109110
}
110111

111112
fn issue_1279() {

tests/target/issue-2917/packed_simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! impl_from_vector {
3131
}
3232
*/
3333

34-
test_if!{
34+
test_if! {
3535
$test_tt:
3636
interpolate_idents! {
3737
mod [$id _from_ $source] {

tests/target/lazy_static.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lazy_static! {
1010
// We need to be able to format `lazy_static!` without known syntax.
1111
lazy_static!(xxx, yyyy, zzzzz);
1212

13-
lazy_static!{}
13+
lazy_static! {}
1414

1515
// #2354
1616
lazy_static! {

tests/target/macros.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ itemmacro!(
99
.formatted()
1010
);
1111

12-
itemmacro!{this, is.bracket().formatted()}
12+
itemmacro! {this, is.brace().formatted()}
1313

1414
peg_file! modname("mygrammarfile.rustpeg");
1515

@@ -94,7 +94,7 @@ fn main() {
9494

9595
foo(makro!(1, 3));
9696

97-
hamkaas!{ () };
97+
hamkaas! { () };
9898

9999
macrowithbraces! {dont, format, me}
100100

@@ -104,11 +104,11 @@ fn main() {
104104

105105
some_macro![];
106106

107-
some_macro!{
107+
some_macro! {
108108
// comment
109109
};
110110

111-
some_macro!{
111+
some_macro! {
112112
// comment
113113
};
114114

@@ -131,7 +131,8 @@ fn main() {
131131
}
132132

133133
impl X {
134-
empty_invoc!{}
134+
empty_invoc! {}
135+
empty_invoc! {}
135136
}
136137

137138
fn issue_1279() {
@@ -949,7 +950,7 @@ macro_rules! m {
949950
};
950951
}
951952
fn foo() {
952-
f!{r#"
953+
f! {r#"
953954
test
954955
"#};
955956
}

tests/target/match.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,19 @@ fn issue355() {
204204
vec![3; 4]
205205
}
206206
// Funky bracketing styles
207-
t => println!{"a", b},
207+
t => println! {"a", b},
208208
u => vec![1, 2],
209209
v => vec![3; 4],
210210
w => println!["a", b],
211211
x => vec![1, 2],
212212
y => vec![3; 4],
213213
// Brackets with comments
214-
tc => println!{"a", b}, // comment
215-
uc => vec![1, 2], // comment
216-
vc => vec![3; 4], // comment
217-
wc => println!["a", b], // comment
218-
xc => vec![1, 2], // comment
219-
yc => vec![3; 4], // comment
214+
tc => println! {"a", b}, // comment
215+
uc => vec![1, 2], // comment
216+
vc => vec![3; 4], // comment
217+
wc => println!["a", b], // comment
218+
xc => vec![1, 2], // comment
219+
yc => vec![3; 4], // comment
220220
yd => looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(
221221
aaaaaaaaaa, bbbbbbbbbb, cccccccccc, dddddddddd,
222222
),

0 commit comments

Comments
 (0)