mirror of https://github.com/astral-sh/ruff
working for Micha's exact example
This commit is contained in:
parent
9d7d94c4f0
commit
2cb98d4cdb
|
|
@ -1884,14 +1884,19 @@ fn handle_lambda_comment<'a>(
|
|||
return CommentPlacement::dangling(comment.enclosing_node(), comment);
|
||||
}
|
||||
} else {
|
||||
// Comments between the lambda and the body are dangling on the lambda:
|
||||
// End-of-line 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
|
||||
|
|
|
|||
|
|
@ -377,19 +377,46 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
|
|||
);
|
||||
|
||||
let fmt_body = format_with(|f: &mut PyFormatter| {
|
||||
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(")")
|
||||
]
|
||||
)
|
||||
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(")")
|
||||
]
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
match layout {
|
||||
|
|
|
|||
Loading…
Reference in New Issue