Skip to content

Commit e358d19

Browse files
authored
enhance const block formatting (#4493)
Signed-off-by: frbimo <[email protected]>
1 parent 30bda45 commit e358d19

File tree

5 files changed

+214
-1
lines changed

5 files changed

+214
-1
lines changed

src/formatting/expr.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,32 @@ pub(crate) fn format_expr(
125125
| ast::ExprKind::While(..) => to_control_flow(expr, expr_type)
126126
.and_then(|control_flow| control_flow.rewrite(context, shape)),
127127
ast::ExprKind::ConstBlock(ref anon_const) => {
128-
Some(format!("const {}", anon_const.rewrite(context, shape)?))
128+
let between_span = mk_sp(
129+
context.snippet_provider.span_after(expr.span, "const"),
130+
anon_const.value.span.lo(),
131+
);
132+
let anon_const_str = anon_const.rewrite(context, shape)?;
133+
let contains_comments =
134+
contains_comment(context.snippet_provider.span_to_snippet(between_span)?);
135+
match context.config.brace_style() {
136+
BraceStyle::AlwaysNextLine => combine_strs_with_missing_comments(
137+
context,
138+
"const",
139+
&anon_const_str,
140+
between_span,
141+
shape,
142+
!anon_const_str.contains('\n'),
143+
),
144+
_ if !contains_comments => Some(format!("const {}", &anon_const_str)),
145+
_ => combine_strs_with_missing_comments(
146+
context,
147+
"const",
148+
&anon_const_str,
149+
between_span,
150+
shape,
151+
true,
152+
),
153+
}
129154
}
130155
ast::ExprKind::Block(ref block, opt_label) => {
131156
match expr_type {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const
17+
{
18+
let x = 5 + 10;
19+
x / 3
20+
}
21+
}
22+
23+
fn foo() -> i32 {
24+
const // baz
25+
{
26+
let x = 5 + 10;
27+
x / 3
28+
}
29+
}
30+
31+
fn foo() -> i32 {
32+
const /*qux */ {
33+
let x = 5 + 10;
34+
x / 3
35+
}
36+
}
37+
38+
fn foo() -> i32 {
39+
const
40+
// baz
41+
{
42+
let x = 5 + 10;
43+
x / 3
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-brace_style: SameLineWhere
2+
// SameLineWhere brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const
17+
{
18+
let x = 5 + 10;
19+
x / 3
20+
}
21+
}
22+
23+
fn foo() -> i32 {
24+
const // baz
25+
{
26+
let x = 5 + 10;
27+
x / 3
28+
}
29+
}
30+
31+
fn foo() -> i32 {
32+
const /*qux */ {
33+
let x = 5 + 10;
34+
x / 3
35+
}
36+
}
37+
38+
fn foo() -> i32 {
39+
const
40+
// baz
41+
{
42+
let x = 5 + 10;
43+
x / 3
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for const blocks
3+
4+
fn foo() -> i32
5+
{
6+
const
7+
{
8+
let x = 5 + 10;
9+
x / 3
10+
}
11+
}
12+
13+
fn bar() -> i32
14+
{
15+
const { 4 }
16+
}
17+
18+
fn foo() -> i32
19+
{
20+
const
21+
{
22+
let x = 5 + 10;
23+
x / 3
24+
}
25+
}
26+
27+
fn foo() -> i32
28+
{
29+
const // baz
30+
{
31+
let x = 5 + 10;
32+
x / 3
33+
}
34+
}
35+
36+
fn foo() -> i32
37+
{
38+
const /*qux */
39+
{
40+
let x = 5 + 10;
41+
x / 3
42+
}
43+
}
44+
45+
fn foo() -> i32
46+
{
47+
const
48+
// baz
49+
{
50+
let x = 5 + 10;
51+
x / 3
52+
}
53+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-brace_style: SameLineWhere
2+
// SameLineWhere brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const {
17+
let x = 5 + 10;
18+
x / 3
19+
}
20+
}
21+
22+
fn foo() -> i32 {
23+
const // baz
24+
{
25+
let x = 5 + 10;
26+
x / 3
27+
}
28+
}
29+
30+
fn foo() -> i32 {
31+
const /*qux */
32+
{
33+
let x = 5 + 10;
34+
x / 3
35+
}
36+
}
37+
38+
fn foo() -> i32 {
39+
const
40+
// baz
41+
{
42+
let x = 5 + 10;
43+
x / 3
44+
}
45+
}

0 commit comments

Comments
 (0)