Indent a match guard if the pattern is multiline

Closes #2377
This commit is contained in:
Nick Cameron 2018-06-25 12:16:17 +12:00
parent 1cca758d4d
commit be4d37da4b
3 changed files with 48 additions and 5 deletions

View File

@ -289,7 +289,13 @@ fn rewrite_match_pattern(
let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?;
// Guard
let guard_str = rewrite_guard(context, guard, shape, trimmed_last_line_width(&pats_str))?;
let guard_str = rewrite_guard(
context,
guard,
shape,
trimmed_last_line_width(&pats_str),
pats_str.contains("\n"),
)?;
Some(format!("{}{}", pats_str, guard_str))
}
@ -450,6 +456,7 @@ fn rewrite_guard(
// The amount of space used up on this line for the pattern in
// the arm (excludes offset).
pattern_width: usize,
multiline_pattern: bool,
) -> Option<String> {
if let Some(ref guard) = *guard {
// First try to fit the guard string on the same line as the pattern.
@ -457,10 +464,12 @@ fn rewrite_guard(
let cond_shape = shape
.offset_left(pattern_width + 4)
.and_then(|s| s.sub_width(5));
if let Some(cond_shape) = cond_shape {
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
return Some(format!(" if {}", cond_str));
if !multiline_pattern {
if let Some(cond_shape) = cond_shape {
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
return Some(format!(" if {}", cond_str));
}
}
}
}

View File

@ -491,3 +491,19 @@ fn issue_2621() {
| Foo::I => println!("With comment"), // Comment after line
}
}
fn issue_2377() {
match tok {
Tok::Not
| Tok::BNot
| Tok::Plus
| Tok::Minus
| Tok::PlusPlus
| Tok::MinusMinus
| Tok::Void
| Tok::Delete if prec <= 16 => {
// code here...
}
Tok::TypeOf if prec <= 16 => {}
}
}

View File

@ -518,3 +518,21 @@ fn issue_2621() {
Foo::I => println!("With comment"), // Comment after line
}
}
fn issue_2377() {
match tok {
Tok::Not
| Tok::BNot
| Tok::Plus
| Tok::Minus
| Tok::PlusPlus
| Tok::MinusMinus
| Tok::Void
| Tok::Delete
if prec <= 16 =>
{
// code here...
}
Tok::TypeOf if prec <= 16 => {}
}
}