Skip to content
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

Merged
merged 21 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next on my list

Copy link
Contributor Author

@cnpryer cnpryer Jun 24, 2023

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


# Delete something
del (
x,
# Deleting this
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
b,
c,
d,
# Deleted
) # Completed
# Done
```


41 changes: 37 additions & 4 deletions crates/ruff_python_formatter/src/statement/stmt_delete.rs
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
let separator =
format_with(|f| group(&format_args![text(","), soft_line_break_or_space(),]).fmt(f));
let separator = group(&format_args![text(","), soft_line_break_or_space()]);

Copy link
Member

Choose a reason for hiding this comment

The 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 soft_line_breaks become hard_line_breaks when a single element doesn't fit on the line. Or you may not need the group at all. But that depends on the expected formatting.

Copy link
Contributor Author

@cnpryer cnpryer Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let separator = group(&format_args![text(","), soft_line_break_or_space()]);

Hmm. I think I tried this but had temporary value issues. I'll check it out.

You probably want to wrap the whole list in a group rather than each separator

I'll play around with it. Thanks!

Copy link
Contributor Author

@cnpryer cnpryer Jul 9, 2023

Choose a reason for hiding this comment

The 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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatted may not work for you because it doesn't allow you to pass any options (at least not to my knowledge)

}
}