mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-11 21:06:19 +00:00
Format delete
statement (#5169)
This commit is contained in:
parent
1782fb8c30
commit
15c7b6bcf7
3 changed files with 328 additions and 3 deletions
|
@ -1,5 +1,9 @@
|
|||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use crate::builders::{optional_parentheses, PyFormatterExtensions};
|
||||
use crate::comments::dangling_node_comments;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::{block_indent, format_with, space, text};
|
||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||
use rustpython_parser::ast::StmtDelete;
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -7,6 +11,38 @@ pub struct FormatStmtDelete;
|
|||
|
||||
impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
||||
fn fmt_fields(&self, item: &StmtDelete, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented(item)])
|
||||
let StmtDelete { range: _, targets } = item;
|
||||
|
||||
write!(f, [text("del"), space()])?;
|
||||
|
||||
match targets.as_slice() {
|
||||
[] => {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
// Handle special case of delete statements without targets.
|
||||
// ```
|
||||
// del (
|
||||
// # Dangling comment
|
||||
// )
|
||||
&text("("),
|
||||
block_indent(&dangling_node_comments(item)),
|
||||
&text(")"),
|
||||
]
|
||||
)
|
||||
}
|
||||
[single] => {
|
||||
write!(f, [single.format().with_options(Parenthesize::IfBreaks)])
|
||||
}
|
||||
targets => {
|
||||
let item = format_with(|f| f.join_comma_separated().nodes(targets.iter()).finish());
|
||||
optional_parentheses(&item).fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtDelete, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue