consolidate layout and preview checks

This commit is contained in:
Brent Westbrook 2025-12-12 10:05:24 -05:00
parent 93a958a734
commit 17a1065fad
No known key found for this signature in database
1 changed files with 189 additions and 201 deletions

View File

@ -137,12 +137,16 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
write!(f, [dangling_comments(dangling)])?;
}
FormatBody {
body,
dangling,
layout: self.layout,
if !preview {
return body.format().fmt(f);
}
let fmt_body = FormatBody { body, dangling };
match self.layout {
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
ExprLambdaLayout::Default => fmt_body.fmt(f),
}
.fmt(f)
}
}
@ -203,26 +207,16 @@ impl NeedsParentheses for ExprLambda {
struct FormatBody<'a> {
body: &'a Expr,
dangling: &'a [SourceComment],
layout: ExprLambdaLayout,
}
impl Format<PyFormatContext<'_>> for FormatBody<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let FormatBody {
dangling,
body,
layout,
} = self;
if !is_parenthesize_lambda_bodies_enabled(f.context()) {
return body.format().fmt(f);
}
let FormatBody { dangling, body } = self;
let body = *body;
let comments = f.context().comments().clone();
let body_comments = comments.leading_dangling_trailing(body);
let fmt_body = format_with(|f: &mut PyFormatter| {
if !dangling.is_empty() {
// Can't use partition_point because there can be additional end of line comments
// after the initial set. All of these comments are dangling, for example:
@ -411,11 +405,5 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
else {
parenthesize_if_expands(&body.format().with_options(Parentheses::Never)).fmt(f)
}
});
match layout {
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
ExprLambdaLayout::Default => fmt_body.fmt(f),
}
}
}