unwrap format_body

This commit is contained in:
Brent Westbrook 2025-12-11 15:43:00 -05:00
parent 00745159d1
commit 3dfda9f6cc
No known key found for this signature in database
1 changed files with 50 additions and 54 deletions

View File

@ -360,63 +360,59 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
.unwrap_or(dangling.len()), .unwrap_or(dangling.len()),
); );
let fmt_body = format_with(|f: &mut PyFormatter| { // If the body is parenthesized and has its own leading comments, preserve the
// If the body is parenthesized and has its own leading comments, preserve the // separation between the dangling lambda comments and the body comments. For example,
// separation between the dangling lambda comments and the body comments. For example, // preserve this comment positioning:
// preserve this comment positioning: //
// // ```python
// ```python // (
// ( // lambda: # 1
// lambda: # 1 // # 2
// # 2 // ( # 3
// ( # 3 // x
// x // )
// ) // )
// ) // ```
// ``` //
// // 1 and 2 are dangling on the lambda and emitted first, followed by a hard line break
// 1 and 2 are dangling on the lambda and emitted first, followed by a hard line break // and the parenthesized body with its leading comments.
// and the parenthesized body with its leading comments. //
// // However, when removing 2, 1 and 3 can instead be formatted on the same line:
// However, when removing 2, 1 and 3 can instead be formatted on the same line: //
// // ```python
// ```python // (
// ( // lambda: ( # 1 # 3
// lambda: ( # 1 # 3 // x
// x // )
// ) // )
// ) // ```
// ``` let comments = f.context().comments();
let comments = f.context().comments(); if is_expression_parenthesized((*body).into(), comments.ranges(), f.context().source())
if is_expression_parenthesized((*body).into(), comments.ranges(), f.context().source()) && comments.has_leading(*body)
&& comments.has_leading(*body) {
{ trailing_comments(dangling).fmt(f)?;
trailing_comments(dangling).fmt(f)?;
if leading_body_comments.is_empty() { if leading_body_comments.is_empty() {
space().fmt(f)?; space().fmt(f)?;
} else {
hard_line_break().fmt(f)?;
}
body.format().with_options(Parentheses::Always).fmt(f)
} else { } else {
write!( hard_line_break().fmt(f)?;
f,
[
space(),
token("("),
trailing_comments(after_parameters_end_of_line),
block_indent(&format_args!(
leading_comments(leading_body_comments),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
} }
});
fmt_body.fmt(f) body.format().with_options(Parentheses::Always).fmt(f)
} else {
write!(
f,
[
space(),
token("("),
trailing_comments(after_parameters_end_of_line),
block_indent(&format_args!(
leading_comments(leading_body_comments),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
}
} }
} }