Manually format comments around := in named expressions (#6634)

## Summary

Attaches comments around the `:=` operator in a named expression as
dangling, and formats them manually in the `named_expr.rs` formatter.

Closes https://github.com/astral-sh/ruff/issues/5695.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-17 23:10:45 -04:00 committed by GitHub
parent a128fe5148
commit 26bba11be6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 194 additions and 9 deletions

View file

@ -1,7 +1,10 @@
use crate::comments::{dangling_comments, SourceComment};
use crate::context::PyFormatContext;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::prelude::{
format_args, group, hard_line_break, soft_line_break_or_space, space, text,
};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprNamedExpr;
@ -16,16 +19,35 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
value,
range: _,
} = item;
// This context, a dangling comment is an end-of-line comment on the same line as the `:=`.
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
write!(
f,
[
target.format(),
space(),
text(":="),
space(),
value.format(),
group(&format_args!(target.format(), soft_line_break_or_space())),
text(":=")
]
)
)?;
if dangling.is_empty() {
write!(f, [space()])?;
} else {
write!(f, [dangling_comments(dangling), hard_line_break()])?;
}
write!(f, [value.format()])
}
fn fmt_dangling_comments(
&self,
_dangling_comments: &[SourceComment],
_f: &mut PyFormatter,
) -> FormatResult<()> {
// Handled by `fmt_fields`
Ok(())
}
}