wrap else { for long, single-lined initializer expressions

This helps to prevent max width errors.
This commit is contained in:
Yacin Tmimi 2023-04-01 11:02:27 -04:00 committed by Caleb Cartwright
parent e4a9892b7a
commit 9386b32f5a
3 changed files with 20 additions and 16 deletions

View file

@ -175,8 +175,10 @@ fn same_line_else_kw_and_brace(
init_shape: Shape,
) -> bool {
if !init_str.contains('\n') {
// initializer expression is single lined so the "else {" should be placed on the same line
return true;
// initializer expression is single lined. The "else {" can only be placed on the same line
// as the initializer expression if there is enough room for it.
// 7 = ` else {`
return init_shape.width.saturating_sub(init_str.len()) >= 7;
}
// 1. The initializer expression ends with one or more `)`, `]`, `}`.

View file

@ -79,18 +79,18 @@ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width()
fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() {
// Pre Formatting:
// The length of `(indent)let pat = init else {` is 100 (max_width)
// The length of `(indent)let pat = init else {` is 99 (< max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// and the else block is formatted over multiple lines because we can't fit the
// else block on the same line as the initializer expr.
let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else {return};
let Some(x) = some_really_really_really_really_really_really_really_really_long_name___E else {return};
// Pre Formatting:
// The length of `(indent)let pat = init else {` is 101 (> max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// which leads to the `{` exceeding the max width
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
// They are formatted on the next line.
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {return};
}
@ -98,8 +98,8 @@ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_n
// Pre Formatting:
// The length of `(indent)let pat = init` is 99 (< max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// which leads to the `else {` exceeding the max width
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
// They are formatted on the next line.
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {return};
// Pre Formatting:

View file

@ -130,21 +130,22 @@ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width()
fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() {
// Pre Formatting:
// The length of `(indent)let pat = init else {` is 100 (max_width)
// The length of `(indent)let pat = init else {` is 99 (< max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// and the else block is formatted over multiple lines because we can't fit the
// else block on the same line as the initializer expr.
let Some(x) = some_really_really_really_really_really_really_really_really_long_name____E else {
let Some(x) = some_really_really_really_really_really_really_really_really_long_name___E else {
return;
};
// Pre Formatting:
// The length of `(indent)let pat = init else {` is 101 (> max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// which leads to the `{` exceeding the max width
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
// They are formatted on the next line.
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F
else {
return;
};
}
@ -153,9 +154,10 @@ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_n
// Pre Formatting:
// The length of `(indent)let pat = init` is 99 (< max_width)
// Post Formatting:
// The else keyword and opening brace remain on the same line as the initializer expr,
// which leads to the `else {` exceeding the max width
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
// They are formatted on the next line.
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G
else {
return;
};