Skip to content

Commit

Permalink
wrap else { for long, single-lined initializer expressions
Browse files Browse the repository at this point in the history
This helps to prevent max width errors.
  • Loading branch information
ytmimi authored and calebcartwright committed Jun 20, 2023
1 parent e4a9892 commit 9386b32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `)`, `]`, `}`.
Expand Down
12 changes: 6 additions & 6 deletions tests/source/let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,27 @@ 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};
}

fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_near_max_width() {
// 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:
Expand Down
18 changes: 10 additions & 8 deletions tests/target/let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand All @@ -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;
};

Expand Down

0 comments on commit 9386b32

Please sign in to comment.