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)?; let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?;
// Guard // 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)) 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 amount of space used up on this line for the pattern in
// the arm (excludes offset). // the arm (excludes offset).
pattern_width: usize, pattern_width: usize,
multiline_pattern: bool,
) -> Option<String> { ) -> Option<String> {
if let Some(ref guard) = *guard { if let Some(ref guard) = *guard {
// First try to fit the guard string on the same line as the pattern. // First try to fit the guard string on the same line as the pattern.
@ -457,6 +464,7 @@ fn rewrite_guard(
let cond_shape = shape let cond_shape = shape
.offset_left(pattern_width + 4) .offset_left(pattern_width + 4)
.and_then(|s| s.sub_width(5)); .and_then(|s| s.sub_width(5));
if !multiline_pattern {
if let Some(cond_shape) = cond_shape { if let Some(cond_shape) = cond_shape {
if let Some(cond_str) = guard.rewrite(context, cond_shape) { if let Some(cond_str) = guard.rewrite(context, cond_shape) {
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() { if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
@ -464,6 +472,7 @@ fn rewrite_guard(
} }
} }
} }
}
// Not enough space to put the guard after the pattern, try a newline. // Not enough space to put the guard after the pattern, try a newline.
// 3 = `if `, 5 = ` => {` // 3 = `if `, 5 = ` => {`

View file

@ -491,3 +491,19 @@ fn issue_2621() {
| Foo::I => println!("With comment"), // Comment after line | 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 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 => {}
}
}