-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format delete
statement
#5169
Format delete
statement
#5169
Changes from 6 commits
faf23c3
1ef57eb
b5df670
022e8ee
3b12221
a98d721
3466a0b
3d7e86c
dfc1122
2e676f3
f57bc9e
a1d9855
14b80b6
7ebd63c
695df60
ce249f5
b5fe6d5
dee077b
da5799d
71352fc
f16ecb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
x = 1 | ||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 | ||
b, c, d = (2, 3, 4) | ||
|
||
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 | ||
|
||
# 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--- | ||
source: crates/ruff_python_formatter/src/lib.rs | ||
expression: snapshot | ||
--- | ||
## Input | ||
```py | ||
x = 1 | ||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 | ||
b, c, d = (2, 3, 4) | ||
|
||
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 | ||
|
||
# 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 | ||
b, c, d = (2, 3, 4) | ||
|
||
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 | ||
|
||
# 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 | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,45 @@ | ||||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; | ||||||||
use ruff_formatter::{write, Buffer, FormatResult}; | ||||||||
use rustpython_parser::ast::StmtDelete; | ||||||||
use crate::expression::parentheses::Parenthesize; | ||||||||
use crate::prelude::PyFormatContext; | ||||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter}; | ||||||||
use ruff_formatter::prelude::{ | ||||||||
format_args, format_with, group, soft_line_break_or_space, space, text, Formatter, | ||||||||
}; | ||||||||
use ruff_formatter::{write, Buffer, Format, FormatResult}; | ||||||||
use rustpython_parser::ast::{Expr, StmtDelete}; | ||||||||
|
||||||||
#[derive(Default)] | ||||||||
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(), DeleteList::new(targets)]) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// TODO(cnpryer): Impl FormatRuleWithOptions Parenthesize | ||||||||
#[derive(Debug)] | ||||||||
struct DeleteList<'a> { | ||||||||
delete_list: &'a [Expr], | ||||||||
} | ||||||||
|
||||||||
impl<'a> DeleteList<'a> { | ||||||||
const fn new(delete_list: &'a [Expr]) -> Self { | ||||||||
Self { delete_list } | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
impl Format<PyFormatContext<'_>> for DeleteList<'_> { | ||||||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> { | ||||||||
let separator = | ||||||||
format_with(|f| group(&format_args![text(","), soft_line_break_or_space(),]).fmt(f)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably want to wrap the whole list in a group rather than each separator so that all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm. I think I tried this but had temporary value issues. I'll check it out.
I'll play around with it. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think #5169 (comment) might resolve this as well |
||||||||
let mut join = f.join_with(separator); | ||||||||
|
||||||||
for element in self.delete_list { | ||||||||
join.entry(&format_with(|f| { | ||||||||
write!(f, [element.format().with_options(Parenthesize::IfBreaks)]) | ||||||||
})); | ||||||||
} | ||||||||
join.finish() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. f.join_with(...)
.entries(self.delete_list.iter().formatted())
// ... I don't think is what I'm looking for, but I'll look into it more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next on my list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set to fail in d103b07