mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-30 15:17:40 +00:00
Format delete
statement (#5169)
This commit is contained in:
parent
1782fb8c30
commit
15c7b6bcf7
3 changed files with 328 additions and 3 deletions
72
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py
vendored
Normal file
72
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py
vendored
Normal file
|
@ -0,0 +1,72 @@
|
|||
x = 1
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1
|
||||
a, b, c, d = 1, 2, 3, 4
|
||||
|
||||
del a, b, c, d
|
||||
del a, b, c, d # Trailing
|
||||
|
||||
del a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a
|
||||
del a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a # Trailing
|
||||
|
||||
del (
|
||||
a,
|
||||
a
|
||||
)
|
||||
|
||||
del (
|
||||
# Dangling comment
|
||||
)
|
||||
|
||||
# Delete something
|
||||
del x # Deleted something
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x, # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
# Dangling comment
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# NOTE: This shouldn't format. See https://github.com/astral-sh/ruff/issues/5630.
|
||||
# Delete something
|
||||
del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these
|
||||
# Ready to delete
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
x,
|
||||
# Deleting this
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
# Deleted
|
||||
) # Completed
|
||||
# Done
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,217 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
x = 1
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1
|
||||
a, b, c, d = 1, 2, 3, 4
|
||||
|
||||
del a, b, c, d
|
||||
del a, b, c, d # Trailing
|
||||
|
||||
del a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a
|
||||
del a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a # Trailing
|
||||
|
||||
del (
|
||||
a,
|
||||
a
|
||||
)
|
||||
|
||||
del (
|
||||
# Dangling comment
|
||||
)
|
||||
|
||||
# Delete something
|
||||
del x # Deleted something
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x, # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
# Dangling comment
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# NOTE: This shouldn't format. See https://github.com/astral-sh/ruff/issues/5630.
|
||||
# Delete something
|
||||
del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these
|
||||
# Ready to delete
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
x,
|
||||
# Deleting this
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
# Deleted
|
||||
) # Completed
|
||||
# Done
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
x = 1
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1
|
||||
a, b, c, d = 1, 2, 3, 4
|
||||
|
||||
del a, b, c, d
|
||||
del a, b, c, d # Trailing
|
||||
|
||||
del (
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
)
|
||||
del (
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
) # Trailing
|
||||
|
||||
del (a, a)
|
||||
|
||||
del (
|
||||
# Dangling comment
|
||||
)
|
||||
|
||||
# Delete something
|
||||
del x # Deleted something
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x, # Deleted something
|
||||
# Finishing deletes
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
# Deleting something
|
||||
x # Deleted something
|
||||
# Finishing deletes
|
||||
# Dangling comment
|
||||
) # Completed
|
||||
# Done deleting
|
||||
|
||||
# NOTE: This shouldn't format. See https://github.com/astral-sh/ruff/issues/5630.
|
||||
# Delete something
|
||||
del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these
|
||||
# Ready to delete
|
||||
|
||||
# Delete something
|
||||
del (
|
||||
x,
|
||||
# Deleting this
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
# Deleted
|
||||
) # Completed
|
||||
# Done
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue