mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:18 +00:00
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:
parent
a128fe5148
commit
26bba11be6
4 changed files with 194 additions and 9 deletions
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue