ruff/crates/ruff_python_formatter/src/expression
Dhruv Manilawala f3dac27e9a
Fix f-string formatting in assignment statement (#14454)
## Summary

fixes: #13813

This PR fixes a bug in the formatting assignment statement when the
value is an f-string.

This is resolved by using custom best fit layouts if the f-string is (a)
not already a flat f-string (thus, cannot be multiline) and (b) is not a
multiline string (thus, cannot be flattened). So, it is used in cases
like the following:
```py
aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
    expression}moreeeeeeeeeeeeeeeee"
```
Which is (a) `FStringLayout::Multiline` and (b) not a multiline.

There are various other examples in the PR diff along with additional
explanation and context as code comments.

## Test Plan

Add multiple test cases for various scenarios.
2024-11-26 15:07:18 +05:30
..
binary_like.rs Join implicit concatenated strings when they fit on a line (#13663) 2024-10-24 11:52:22 +02:00
expr_attribute.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_await.rs Fix instability with await fluent style (#8676) 2023-11-17 12:24:19 -05:00
expr_bin_op.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_bool_op.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
expr_boolean_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_bytes_literal.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_call.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_compare.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_dict.rs Refactor the `ExprDict` node (#11267) 2024-05-07 11:46:10 +00:00
expr_dict_comp.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_ellipsis_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_f_string.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_generator.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_if.rs Remove `Expr` postfix from `ExprNamed`, `ExprIf`, and `ExprGenerator` (#10229) 2024-03-04 12:55:01 +01:00
expr_ipy_escape_command.rs Formatter parentheses support for `IpyEscapeCommand` (#8207) 2023-10-25 14:01:50 +00:00
expr_lambda.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_list.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_list_comp.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_name.rs Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
expr_named.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_none_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_number_literal.rs Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
expr_set.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_set_comp.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_slice.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_starred.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_string_literal.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_subscript.rs Fix parentheses around return type annotations (#13381) 2024-09-20 09:23:53 +02:00
expr_tuple.rs Implement `iter()`, `len()` and `is_empty()` for all display-literal AST nodes (#12807) 2024-08-12 10:39:28 +00:00
expr_unary_op.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
expr_yield.rs Avoid parenthesizing unsplittable because of comments (#8431) 2023-11-03 05:12:59 +00:00
expr_yield_from.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
mod.rs Fix preview style name in `can_omit_parentheses` to is_f_string_formatting_enabled (#13907) 2024-10-24 11:32:28 +00:00
operator.rs Split implicit concatenated strings before binary expressions (#7145) 2023-09-08 06:51:26 +00:00
parentheses.rs Parenthesize `match..case` `if` guards (#13513) 2024-09-26 06:44:33 +00:00