Merge pull request #1778 from topecongiro/rfc/match-arm-multiline-pattern
Rfc: fix up match arm pattern
This commit is contained in:
commit
4cfc2bfbd4
41
src/expr.rs
41
src/expr.rs
@ -1701,25 +1701,20 @@ impl Rewrite for ast::Arm {
|
||||
};
|
||||
let pats_str = try_opt!(write_list(&items, &fmt));
|
||||
|
||||
let guard_shape = if pats_str.contains('\n') {
|
||||
shape.with_max_width(context.config)
|
||||
} else {
|
||||
shape
|
||||
};
|
||||
|
||||
let guard_str = try_opt!(rewrite_guard(
|
||||
context,
|
||||
guard,
|
||||
guard_shape,
|
||||
shape,
|
||||
trimmed_last_line_width(&pats_str),
|
||||
));
|
||||
|
||||
let pats_len = pats_str.len();
|
||||
let pats_str = format!("{}{}", pats_str, guard_str);
|
||||
|
||||
let (mut extend, body) = match body.node {
|
||||
ast::ExprKind::Block(ref block)
|
||||
if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
|
||||
context.config.wrap_match_arms() => {
|
||||
context.config.wrap_match_arms() => {
|
||||
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
|
||||
(false, &**expr)
|
||||
} else {
|
||||
@ -1761,11 +1756,15 @@ impl Rewrite for ast::Arm {
|
||||
match rewrite {
|
||||
Some(ref body_str)
|
||||
if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) ||
|
||||
!context.config.wrap_match_arms() ||
|
||||
(extend && first_line_width(body_str) <= arm_shape.width) ||
|
||||
is_block => {
|
||||
!context.config.wrap_match_arms() ||
|
||||
(extend && first_line_width(body_str) <= arm_shape.width) ||
|
||||
is_block =>
|
||||
{
|
||||
let block_sep = match context.config.control_brace_style() {
|
||||
ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
|
||||
_ if guard.is_some() && pats_str.contains('\n') && is_block &&
|
||||
body_str != "{}" &&
|
||||
pats_len > context.config.tab_spaces() => alt_block_sep.as_str(),
|
||||
_ => " ",
|
||||
};
|
||||
|
||||
@ -1848,33 +1847,30 @@ fn rewrite_guard(
|
||||
// First try to fit the guard string on the same line as the pattern.
|
||||
// 4 = ` if `, 5 = ` => {`
|
||||
if let Some(cond_shape) = shape
|
||||
.shrink_left(pattern_width + 4)
|
||||
.offset_left(pattern_width + 4)
|
||||
.and_then(|s| s.sub_width(5))
|
||||
{
|
||||
if let Some(cond_str) = guard
|
||||
.rewrite(context, cond_shape)
|
||||
.and_then(|s| s.rewrite(context, cond_shape))
|
||||
{
|
||||
if !cond_str.contains('\n') {
|
||||
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
|
||||
return Some(format!(" if {}", cond_str));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not enough space to put the guard after the pattern, try a newline.
|
||||
// 3 == `if `
|
||||
if let Some(cond_shape) = Shape::indented(
|
||||
shape.indent.block_indent(context.config) + 3,
|
||||
context.config,
|
||||
).sub_width(3)
|
||||
// 3 = `if `, 5 = ` => {`
|
||||
if let Some(cond_shape) =
|
||||
Shape::indented(shape.indent.block_indent(context.config), context.config)
|
||||
.offset_left(3)
|
||||
.and_then(|s| s.sub_width(5))
|
||||
{
|
||||
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
|
||||
return Some(format!(
|
||||
"\n{}if {}",
|
||||
shape
|
||||
.indent
|
||||
.block_indent(context.config)
|
||||
.to_string(context.config),
|
||||
cond_shape.indent.to_string(context.config),
|
||||
cond_str
|
||||
));
|
||||
}
|
||||
@ -1920,7 +1916,6 @@ fn rewrite_pat_expr(
|
||||
|
||||
// The expression won't fit on the current line, jump to next.
|
||||
let nested_shape = shape
|
||||
.block()
|
||||
.block_indent(context.config.tab_spaces())
|
||||
.with_max_width(context.config);
|
||||
let nested_indent_str = nested_shape.indent.to_string(context.config);
|
||||
|
@ -210,7 +210,8 @@ fn rewrite_segment(
|
||||
match **params {
|
||||
ast::PathParameters::AngleBracketed(ref data)
|
||||
if !data.lifetimes.is_empty() || !data.types.is_empty() ||
|
||||
!data.bindings.is_empty() => {
|
||||
!data.bindings.is_empty() =>
|
||||
{
|
||||
let param_list = data.lifetimes
|
||||
.iter()
|
||||
.map(SegmentParam::LifeTime)
|
||||
|
@ -1,4 +1,5 @@
|
||||
// rustfmt-format_strings: true
|
||||
// rustfmt-force_format_strings: true
|
||||
// rustfmt-max_width: 80
|
||||
// rustfmt-control_brace_style: AlwaysNextLine
|
||||
|
||||
|
@ -28,9 +28,10 @@ fn issue1656() {
|
||||
match rewrite {
|
||||
Some(ref body_str)
|
||||
if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) ||
|
||||
!context.config.wrap_match_arms() ||
|
||||
(extend && first_line_width(body_str) <= arm_shape.width) ||
|
||||
is_block => {
|
||||
!context.config.wrap_match_arms() ||
|
||||
(extend && first_line_width(body_str) <= arm_shape.width) ||
|
||||
is_block =>
|
||||
{
|
||||
return None;
|
||||
}
|
||||
_ => {}
|
||||
|
@ -1,4 +1,5 @@
|
||||
// rustfmt-format_strings: true
|
||||
// rustfmt-force_format_strings: true
|
||||
// rustfmt-max_width: 80
|
||||
// rustfmt-control_brace_style: AlwaysNextLine
|
||||
|
||||
|
@ -324,8 +324,8 @@ fn guards() {
|
||||
if foooooooooooooo && barrrrrrrrrrrr => {}
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
if fooooooooooooooooooooo &&
|
||||
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
|
||||
cccccccccccccccccccccccccccccccccccccccc) => {}
|
||||
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
|
||||
cccccccccccccccccccccccccccccccccccccccc) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user