Revert leading comment changes

This reverts commit 2cb98d4cdb.
This commit is contained in:
Brent Westbrook 2025-12-11 11:42:49 -05:00
parent 2cb98d4cdb
commit 8711b1a301
No known key found for this signature in database
2 changed files with 14 additions and 46 deletions

View File

@ -1884,19 +1884,14 @@ fn handle_lambda_comment<'a>(
return CommentPlacement::dangling(comment.enclosing_node(), comment);
}
} else {
// End-of-line comments between the lambda and the body are dangling on the lambda:
// Comments between the lambda and the body are dangling on the lambda:
// ```python
// (
// lambda: # comment
// y
// )
// ```
// But own-line comments after `:` are leading on the body.
if comment.start() < lambda.body.start() {
if comment.line_position().is_own_line() {
return CommentPlacement::leading(&*lambda.body, comment);
}
// If the value is parenthesized, and the comment is within the parentheses, it should
// be a leading comment on the value, not a dangling comment in the lambda, as in:
// ```python

View File

@ -377,46 +377,19 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
);
let fmt_body = format_with(|f: &mut PyFormatter| {
let body_comments = f.context().comments().leading_dangling_trailing(&**body);
if body_comments.has_leading() {
if body_comments
.leading
.iter()
.any(|comment| comment.line_position().is_own_line())
{
write!(
f,
[
trailing_comments(dangling),
hard_line_break(),
body.format().with_options(Parentheses::Always),
]
)
} else {
write!(
f,
[
space(),
trailing_comments(dangling),
body.format().with_options(Parentheses::Always),
]
)
}
} 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(")")
]
)
}
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(")")
]
)
});
match layout {