Skip to content

Commit d773cc5

Browse files
committed
Added loop/match brace style control option
This adds Allman style loop/match braces as an alternative to the current default style.
1 parent 5bd6036 commit d773cc5

6 files changed

+219
-6
lines changed

src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ configuration_option_enum! { BraceStyle:
3838
SameLineWhere,
3939
}
4040

41+
configuration_option_enum! { ControlBraceStyle:
42+
// K&R/Stroustrup style, Rust community default
43+
AlwaysSameLine,
44+
// Allman style
45+
AlwaysNextLine,
46+
}
47+
4148
configuration_option_enum! { ElseIfBraceStyle:
4249
// K&R style, Rust community default
4350
AlwaysSameLine,
@@ -326,6 +333,8 @@ create_config! {
326333
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
327334
else_if_brace_style: ElseIfBraceStyle, ElseIfBraceStyle::AlwaysSameLine,
328335
"Brace style for if, else if, and else constructs";
336+
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine,
337+
"Brace style for match, loop, for, and while constructs";
329338
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
330339
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
331340
fn_single_line: bool, false, "Put single-expression functions on a single line";

src/expr.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use string::{StringFormat, rewrite_string};
2323
use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search,
2424
first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr};
2525
use visitor::FmtVisitor;
26-
use config::{Config, StructLitStyle, MultilineStyle, ElseIfBraceStyle};
26+
use config::{Config, StructLitStyle, MultilineStyle, ElseIfBraceStyle, ControlBraceStyle};
2727
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
2828
use types::rewrite_path;
2929
use items::{span_lo_for_arg, span_hi_for_arg};
@@ -648,14 +648,20 @@ impl<'a> Rewrite for Loop<'a> {
648648
None => String::new(),
649649
};
650650

651+
let alt_block_sep = String::from("\n") + &context.block_indent.to_string(context.config);
652+
let block_sep = match context.config.control_brace_style {
653+
ControlBraceStyle::AlwaysNextLine => alt_block_sep.as_str(),
654+
ControlBraceStyle::AlwaysSameLine => " ",
655+
};
651656
// FIXME: this drops any comment between "loop" and the block.
652657
self.block
653658
.rewrite(context, width, offset)
654659
.map(|result| {
655-
format!("{}{}{} {}",
660+
format!("{}{}{}{}{}",
656661
label_string,
657662
self.keyword,
658663
pat_expr_string,
664+
block_sep,
659665
result)
660666
})
661667
}
@@ -940,7 +946,12 @@ fn rewrite_match(context: &RewriteContext,
940946
// `match `cond` {`
941947
let cond_budget = try_opt!(width.checked_sub(8));
942948
let cond_str = try_opt!(cond.rewrite(context, cond_budget, offset + 6));
943-
let mut result = format!("match {} {{", cond_str);
949+
let alt_block_sep = String::from("\n") + &context.block_indent.to_string(context.config);
950+
let block_sep = match context.config.control_brace_style {
951+
ControlBraceStyle::AlwaysSameLine => " ",
952+
ControlBraceStyle::AlwaysNextLine => alt_block_sep.as_str(),
953+
};
954+
let mut result = format!("match {}{}{{", cond_str, block_sep);
944955

945956
let nested_context = context.nested_context();
946957
let arm_indent = nested_context.block_indent;
@@ -1099,6 +1110,7 @@ impl Rewrite for ast::Arm {
10991110
};
11001111

11011112
let comma = arm_comma(&context.config, self, body);
1113+
let alt_block_sep = String::from("\n") + &context.block_indent.to_string(context.config);
11021114

11031115
// Let's try and get the arm body on the same line as the condition.
11041116
// 4 = ` => `.len()
@@ -1112,12 +1124,17 @@ impl Rewrite for ast::Arm {
11121124
false
11131125
};
11141126

1127+
let block_sep = match context.config.control_brace_style {
1128+
ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
1129+
_ => " ",
1130+
};
11151131
match rewrite {
11161132
Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
11171133
is_block => {
1118-
return Some(format!("{}{} => {}{}",
1134+
return Some(format!("{}{} =>{}{}{}",
11191135
attr_str.trim_left(),
11201136
pats_str,
1137+
block_sep,
11211138
body_str,
11221139
comma));
11231140
}
@@ -1145,10 +1162,14 @@ impl Rewrite for ast::Arm {
11451162
("", "")
11461163
};
11471164

1148-
Some(format!("{}{} =>{}\n{}{}\n{}{}",
1165+
let block_sep = match context.config.control_brace_style {
1166+
ControlBraceStyle::AlwaysNextLine => alt_block_sep,
1167+
ControlBraceStyle::AlwaysSameLine => String::from(body_prefix) + "\n",
1168+
};
1169+
Some(format!("{}{} =>{}{}{}\n{}{}",
11491170
attr_str.trim_left(),
11501171
pats_str,
1151-
body_prefix,
1172+
block_sep,
11521173
indent_str,
11531174
next_line_body,
11541175
offset.to_string(context.config),
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-control_brace_style: AlwaysNextLine
2+
3+
fn main() {
4+
loop {
5+
();
6+
();
7+
}
8+
9+
10+
'loop_label: loop // loop comment
11+
{
12+
();
13+
}
14+
15+
16+
cond = true;
17+
while cond {
18+
();
19+
}
20+
21+
22+
'while_label: while cond { // while comment
23+
();
24+
}
25+
26+
27+
for obj in iter {
28+
for sub_obj in obj
29+
{
30+
'nested_while_label: while cond {
31+
();
32+
}
33+
}
34+
}
35+
36+
match some_var { // match comment
37+
pattern0 => val0,
38+
pattern1 => val1,
39+
pattern2 | pattern3 => {
40+
do_stuff();
41+
val2
42+
},
43+
};
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-control_brace_style: AlwaysSameLine
2+
3+
fn main() {
4+
loop {
5+
();
6+
();
7+
}
8+
9+
10+
'loop_label: loop // loop comment
11+
{
12+
();
13+
}
14+
15+
16+
cond = true;
17+
while cond {
18+
();
19+
}
20+
21+
22+
'while_label: while cond { // while comment
23+
();
24+
}
25+
26+
27+
for obj in iter {
28+
for sub_obj in obj
29+
{
30+
'nested_while_label: while cond {
31+
();
32+
}
33+
}
34+
}
35+
36+
match some_var { // match comment
37+
pattern0 => val0,
38+
pattern1 => val1,
39+
pattern2 | pattern3 => {
40+
do_stuff();
41+
val2
42+
},
43+
};
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// rustfmt-control_brace_style: AlwaysNextLine
2+
3+
fn main() {
4+
loop
5+
{
6+
();
7+
();
8+
}
9+
10+
11+
'loop_label: loop
12+
{
13+
();
14+
}
15+
16+
17+
cond = true;
18+
while cond
19+
{
20+
();
21+
}
22+
23+
24+
'while_label: while cond
25+
{
26+
// while comment
27+
();
28+
}
29+
30+
31+
for obj in iter
32+
{
33+
for sub_obj in obj
34+
{
35+
'nested_while_label: while cond
36+
{
37+
();
38+
}
39+
}
40+
}
41+
42+
match some_var
43+
{ // match comment
44+
pattern0 => val0,
45+
pattern1 => val1,
46+
pattern2 | pattern3 =>
47+
{
48+
do_stuff();
49+
val2
50+
}
51+
};
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-control_brace_style: AlwaysSameLine
2+
3+
fn main() {
4+
loop {
5+
();
6+
();
7+
}
8+
9+
10+
'loop_label: loop {
11+
();
12+
}
13+
14+
15+
cond = true;
16+
while cond {
17+
();
18+
}
19+
20+
21+
'while_label: while cond {
22+
// while comment
23+
();
24+
}
25+
26+
27+
for obj in iter {
28+
for sub_obj in obj {
29+
'nested_while_label: while cond {
30+
();
31+
}
32+
}
33+
}
34+
35+
match some_var { // match comment
36+
pattern0 => val0,
37+
pattern1 => val1,
38+
pattern2 | pattern3 => {
39+
do_stuff();
40+
val2
41+
}
42+
};
43+
}

0 commit comments

Comments
 (0)