Use reserved width to include line suffix measurement (#6901)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Chris Pryer 2023-08-30 04:07:11 -04:00 committed by GitHub
parent edfd888bd6
commit a3f4d7745a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 504 additions and 80 deletions

View file

@ -247,10 +247,13 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
if format_expression.inspect(f)?.will_break() {
// The group here is necessary because `format_expression` may contain IR elements
// that refer to the group id
group(&format_expression)
.with_group_id(Some(group_id))
.should_expand(true)
.fmt(f)
group(&format_args![
text("("),
soft_block_indent(&format_expression),
text(")")
])
.with_group_id(Some(group_id))
.fmt(f)
} else {
// Only add parentheses if it makes the expression fit on the line.
// Using the flat version as the most expanded version gives a left-to-right splitting behavior