Format delete statement (#5169)

This commit is contained in:
Chris Pryer 2023-07-11 02:36:26 -04:00 committed by GitHub
parent 1782fb8c30
commit 15c7b6bcf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 328 additions and 3 deletions

View file

@ -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(())
}
}