Merge pull request #663 from SingingTree/match_trailing_comma
WIP: Trailing commas for match block arms
This commit is contained in:
commit
0c5de26ef2
@ -308,4 +308,6 @@ create_config! {
|
||||
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
|
||||
wrap_comments: bool, false, "Break comments to fit on the line";
|
||||
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
|
||||
match_block_trailing_comma: bool, false,
|
||||
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
|
||||
}
|
||||
|
25
src/expr.rs
25
src/expr.rs
@ -20,7 +20,7 @@ use string::{StringFormat, rewrite_string};
|
||||
use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
|
||||
semicolon_for_stmt};
|
||||
use visitor::FmtVisitor;
|
||||
use config::{StructLitStyle, MultilineStyle};
|
||||
use config::{Config, StructLitStyle, MultilineStyle};
|
||||
use comment::{FindUncommented, rewrite_comment, contains_comment};
|
||||
use types::rewrite_path;
|
||||
use items::{span_lo_for_arg, span_hi_for_arg};
|
||||
@ -823,7 +823,7 @@ fn rewrite_match(context: &RewriteContext,
|
||||
// We couldn't format the arm, just reproduce the source.
|
||||
let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
|
||||
result.push_str(&snippet);
|
||||
result.push_str(arm_comma(&arm.body));
|
||||
result.push_str(arm_comma(&context.config, &arm.body));
|
||||
}
|
||||
}
|
||||
// BytePos(1) = closing match brace.
|
||||
@ -854,8 +854,10 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
|
||||
arm.body.span.hi
|
||||
}
|
||||
|
||||
fn arm_comma(body: &ast::Expr) -> &'static str {
|
||||
if let ast::ExprBlock(ref block) = body.node {
|
||||
fn arm_comma(config: &Config, body: &ast::Expr) -> &'static str {
|
||||
if config.match_block_trailing_comma {
|
||||
","
|
||||
} else if let ast::ExprBlock(ref block) = body.node {
|
||||
if let ast::DefaultBlock = block.rules {
|
||||
""
|
||||
} else {
|
||||
@ -950,7 +952,7 @@ impl Rewrite for ast::Arm {
|
||||
ref x => x,
|
||||
};
|
||||
|
||||
let comma = arm_comma(body);
|
||||
let comma = arm_comma(&context.config, body);
|
||||
|
||||
// Let's try and get the arm body on the same line as the condition.
|
||||
// 4 = ` => `.len()
|
||||
@ -958,10 +960,15 @@ impl Rewrite for ast::Arm {
|
||||
let budget = context.config.max_width - line_start - comma.len() - 4;
|
||||
let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent);
|
||||
let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
|
||||
let is_block = if let ast::ExprBlock(ref block) = body.node {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
match rewrite {
|
||||
Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
|
||||
comma.is_empty() => {
|
||||
is_block => {
|
||||
return Some(format!("{}{} => {}{}",
|
||||
attr_str.trim_left(),
|
||||
pats_str,
|
||||
@ -983,7 +990,11 @@ impl Rewrite for ast::Arm {
|
||||
body_budget));
|
||||
let indent_str = offset.block_indent(context.config).to_string(context.config);
|
||||
let (body_prefix, body_suffix) = if context.config.wrap_match_arms {
|
||||
(" {", "}")
|
||||
if context.config.match_block_trailing_comma {
|
||||
(" {", "},")
|
||||
} else {
|
||||
(" {", "}")
|
||||
}
|
||||
} else {
|
||||
("", "")
|
||||
};
|
||||
|
14
tests/source/match-block-trailing-comma.rs
Normal file
14
tests/source/match-block-trailing-comma.rs
Normal file
@ -0,0 +1,14 @@
|
||||
// rustfmt-match_block_trailing_comma: true
|
||||
// Match expressions, no unwrapping of block arms or wrapping of multiline
|
||||
// expressions.
|
||||
|
||||
fn foo() {
|
||||
match x {
|
||||
a => {
|
||||
"line1";
|
||||
"line2"
|
||||
}
|
||||
b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
|
||||
}
|
||||
}
|
15
tests/source/match-nowrap-trailing-comma.rs
Normal file
15
tests/source/match-nowrap-trailing-comma.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// rustfmt-wrap_match_arms: false
|
||||
// rustfmt-match_block_trailing_comma: true
|
||||
// Match expressions, no unwrapping of block arms or wrapping of multiline
|
||||
// expressions.
|
||||
|
||||
fn foo() {
|
||||
match x {
|
||||
a => {
|
||||
"line1";
|
||||
"line2"
|
||||
}
|
||||
b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
|
||||
}
|
||||
}
|
16
tests/target/match-block-trailing-comma.rs
Normal file
16
tests/target/match-block-trailing-comma.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// rustfmt-match_block_trailing_comma: true
|
||||
// Match expressions, no unwrapping of block arms or wrapping of multiline
|
||||
// expressions.
|
||||
|
||||
fn foo() {
|
||||
match x {
|
||||
a => {
|
||||
"line1";
|
||||
"line2"
|
||||
},
|
||||
b => {
|
||||
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
|
||||
},
|
||||
}
|
||||
}
|
15
tests/target/match-nowrap-trailing-comma.rs
Normal file
15
tests/target/match-nowrap-trailing-comma.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// rustfmt-wrap_match_arms: false
|
||||
// rustfmt-match_block_trailing_comma: true
|
||||
// Match expressions, no unwrapping of block arms or wrapping of multiline
|
||||
// expressions.
|
||||
|
||||
fn foo() {
|
||||
match x {
|
||||
a => {
|
||||
"line1";
|
||||
"line2"
|
||||
},
|
||||
b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user