From faf23c335bc0f56a00adf98f07de9eb3e70e390e Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 17 Jun 2023 19:30:39 -0400 Subject: [PATCH 01/19] Format delete statement --- .../test/fixtures/ruff/expression/delete.py | 21 ++++++++++ .../src/statement/stmt_delete.rs | 42 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py new file mode 100644 index 0000000000000..5013369e80dc1 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py @@ -0,0 +1,21 @@ +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d +) # Trailing comment +# Dangling comment diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 0a75dcd0167fe..fc93aaf5f0e57 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,12 +1,46 @@ -use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; -use ruff_formatter::{write, Buffer, FormatResult}; -use rustpython_parser::ast::StmtDelete; +use crate::prelude::PyFormatContext; +use crate::{expression::parentheses::Parenthesize, AsFormat, FormatNodeRule, PyFormatter}; +use ruff_formatter::prelude::{format_with, space, text, Formatter}; +use ruff_formatter::{write, Buffer, Format, FormatResult}; +use rustpython_parser::ast::{Expr, StmtDelete}; #[derive(Default)] pub struct FormatStmtDelete; impl FormatNodeRule 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> for DeleteList<'_> { + fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { + write!( + f, + [&format_with(|f| { + let separator = text(", "); // TODO(cnpryer) + 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() + })] + ) } } From 1ef57eb2e436bb352a629ef90fe2b9e251e0b349 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 17 Jun 2023 22:40:35 -0400 Subject: [PATCH 02/19] Use black fixture --- .../simple_cases}/delete.py | 2 + .../black/simple_cases/delete.py.expect | 29 ++++ ...rmatter__tests__black_test__delete_py.snap | 133 ++++++++++++++++++ 3 files changed, 164 insertions(+) rename crates/ruff_python_formatter/resources/test/fixtures/{ruff/expression => black/simple_cases}/delete.py (91%) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect create mode 100644 crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py similarity index 91% rename from crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py rename to crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py index 5013369e80dc1..63593e52caf54 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py @@ -13,9 +13,11 @@ # Some comment del ( x, + # Slapping one here aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d + # Another one ) # Trailing comment # Dangling comment diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect b/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect new file mode 100644 index 0000000000000..603ffbc280529 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect @@ -0,0 +1,29 @@ +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap new file mode 100644 index 0000000000000..28409104322dd --- /dev/null +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap @@ -0,0 +1,133 @@ +--- +source: crates/ruff_python_formatter/src/lib.rs +expression: snapshot +input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py +--- +## Input + +```py +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment +``` + +## Black Differences + +```diff +--- Black ++++ Ruff +@@ -7,13 +7,7 @@ + # Dangling comment + + # Some comment +-del ( +- x, +- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, +- b, +- c, +- d, +-) # Trailing comment ++del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment + # Dangling comment + + # Some comment +@@ -23,7 +17,7 @@ + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, +- d ++ d, + # Another one + ) # Trailing comment + # Dangling comment +``` + +## Ruff Output + +```py +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment +``` + +## Black Output + +```py +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment +``` + + From b5df6709c4e1bc5dc6526a1b8587616c2617c8d6 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 18 Jun 2023 11:11:00 -0400 Subject: [PATCH 03/19] Move new delete fixture into ruff fixtures --- .../black/simple_cases/delete.py.expect | 29 ---- .../simple_cases => ruff/statement}/delete.py | 0 ...rmatter__tests__black_test__delete_py.snap | 133 ------------------ ...ests__ruff_test__statement__delete_py.snap | 67 +++++++++ 4 files changed, 67 insertions(+), 162 deletions(-) delete mode 100644 crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect rename crates/ruff_python_formatter/resources/test/fixtures/{black/simple_cases => ruff/statement}/delete.py (100%) delete mode 100644 crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap create mode 100644 crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect b/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect deleted file mode 100644 index 603ffbc280529..0000000000000 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py.expect +++ /dev/null @@ -1,29 +0,0 @@ -x = 1 -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) - -# Some comment -del x # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d, -) # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - # Slapping one here - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d - # Another one -) # Trailing comment -# Dangling comment diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py similarity index 100% rename from crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py rename to crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap deleted file mode 100644 index 28409104322dd..0000000000000 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__delete_py.snap +++ /dev/null @@ -1,133 +0,0 @@ ---- -source: crates/ruff_python_formatter/src/lib.rs -expression: snapshot -input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/delete.py ---- -## Input - -```py -x = 1 -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) - -# Some comment -del x # Trailing comment -# Dangling comment - -# Some comment -del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - # Slapping one here - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d - # Another one -) # Trailing comment -# Dangling comment -``` - -## Black Differences - -```diff ---- Black -+++ Ruff -@@ -7,13 +7,7 @@ - # Dangling comment - - # Some comment --del ( -- x, -- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, -- b, -- c, -- d, --) # Trailing comment -+del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment - # Dangling comment - - # Some comment -@@ -23,7 +17,7 @@ - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, -- d -+ d, - # Another one - ) # Trailing comment - # Dangling comment -``` - -## Ruff Output - -```py -x = 1 -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) - -# Some comment -del x # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d, -) # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - # Slapping one here - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d - # Another one -) # Trailing comment -# Dangling comment -``` - -## Black Output - -```py -x = 1 -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) - -# Some comment -del x # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d, -) # Trailing comment -# Dangling comment - -# Some comment -del ( - x, - # Slapping one here - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d - # Another one -) # Trailing comment -# Dangling comment -``` - - diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap new file mode 100644 index 0000000000000..71ffb2deb4978 --- /dev/null +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap @@ -0,0 +1,67 @@ +--- +source: crates/ruff_python_formatter/src/lib.rs +expression: snapshot +--- +## Input +```py +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment +``` + + + +## Output +```py +x = 1 +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 +b, c, d = (2, 3, 4) + +# Some comment +del x # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Trailing comment +# Dangling comment + +# Some comment +del ( + x, + # Slapping one here + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d + # Another one +) # Trailing comment +# Dangling comment +``` + + From 022e8eeb94f3824504c9c8e4f74a8b123b050374 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 18 Jun 2023 11:21:28 -0400 Subject: [PATCH 04/19] Add delete statement test case No attached node --- .../test/fixtures/ruff/statement/delete.py | 60 +++++++-- ...ests__ruff_test__statement__delete_py.snap | 114 ++++++++++++++---- 2 files changed, 141 insertions(+), 33 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py index 63593e52caf54..c24d6d67306da 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py @@ -2,22 +2,60 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 b, c, d = (2, 3, 4) -# Some comment -del x # Trailing comment -# Dangling comment +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 -# Some comment -del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment -# Dangling comment +# Delete something +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these +# Ready to delete -# Some comment +# Delete something del ( x, - # Slapping one here + # Deleting this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d - # Another one -) # Trailing comment -# Dangling comment + # Deleted +) # Completed +# Done diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap index 71ffb2deb4978..5342750c68680 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap @@ -8,25 +8,59 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 b, c, d = (2, 3, 4) -# Some comment -del x # Trailing comment -# Dangling comment +# Delete something +del x # Deleted something +# Done deleting -# Some comment -del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Trailing comment -# Dangling comment +# 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 -# Some comment +# Delete something del ( x, - # Slapping one here + # Deleting this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d - # Another one -) # Trailing comment -# Dangling comment + # Deleted +) # Completed +# Done ``` @@ -37,31 +71,67 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 b, c, d = (2, 3, 4) -# Some comment -del x # Trailing comment -# 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 -# Some comment +# Delete something del ( x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d, -) # Trailing comment -# Dangling comment +) # Delete these +# Ready to delete -# Some comment +# Delete something del ( x, - # Slapping one here + # Deleting this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d - # Another one -) # Trailing comment -# Dangling comment + # Deleted +) # Completed +# Done ``` From 3b12221153a64e8702799290609059aafb14b669 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Wed, 21 Jun 2023 17:58:39 -0400 Subject: [PATCH 05/19] Update delete statement snapshot Some differences with black but stable --- ...ests__ruff_test__statement__delete_py.snap | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap index 5342750c68680..750196e0eb6cc 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap @@ -8,6 +8,10 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 b, c, d = (2, 3, 4) +del ( + # Dangling comment +) + # Delete something del x # Deleted something # Done deleting @@ -71,6 +75,10 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 b, c, d = (2, 3, 4) +del ( + # Dangling comment +) + # Delete something del x # Deleted something # Done deleting @@ -80,8 +88,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes - # Completed -) +) # Completed # Done deleting # Delete something @@ -89,8 +96,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes - # Completed -) +) # Completed # Done deleting # Delete something @@ -107,18 +113,11 @@ del ( x # Deleted something # Finishing deletes # Dangling comment - # Completed -) +) # Completed # Done deleting # Delete something -del ( - x, - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d, -) # Delete these +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these # Ready to delete # Delete something @@ -128,7 +127,7 @@ del ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, - d + d, # Deleted ) # Completed # Done From a98d7213c1e1c241ff9aeceb133c0552a8b19060 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Wed, 21 Jun 2023 18:39:26 -0400 Subject: [PATCH 06/19] Write with just JoinBuilder --- .../src/statement/stmt_delete.rs | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index fc93aaf5f0e57..5262632e426bf 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,6 +1,9 @@ +use crate::expression::parentheses::Parenthesize; use crate::prelude::PyFormatContext; -use crate::{expression::parentheses::Parenthesize, AsFormat, FormatNodeRule, PyFormatter}; -use ruff_formatter::prelude::{format_with, space, text, Formatter}; +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}; @@ -28,19 +31,15 @@ impl<'a> DeleteList<'a> { impl Format> for DeleteList<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { - write!( - f, - [&format_with(|f| { - let separator = text(", "); // TODO(cnpryer) - let mut join = f.join_with(&separator); + let separator = + format_with(|f| group(&format_args![text(","), soft_line_break_or_space(),]).fmt(f)); + 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() - })] - ) + for element in self.delete_list { + join.entry(&format_with(|f| { + write!(f, [element.format().with_options(Parenthesize::IfBreaks)]) + })); + } + join.finish() } } From 3d7e86c85eebcf681acaf1083f997752a5afa3ed Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 24 Jun 2023 01:11:34 -0400 Subject: [PATCH 07/19] Update snapshot Set output to target --- .../test/fixtures/ruff/statement/delete.py | 21 ++-- ...ests__ruff_test__statement__delete_py.snap | 108 +++++++++++++++--- 2 files changed, 108 insertions(+), 21 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py index c24d6d67306da..198c230ff79c9 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py @@ -1,6 +1,12 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) +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 ( # Dangling comment @@ -15,7 +21,7 @@ # Deleting something x # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -23,7 +29,7 @@ # Deleting something x # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -31,7 +37,7 @@ # Deleting something x, # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -39,9 +45,8 @@ # Deleting something x # Deleted something # Finishing deletes - # Dangling comment -) # Completed +) # Completed # Done deleting # Delete something @@ -55,7 +60,7 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, - d + d, # Deleted -) # Completed +) # Completed # Done diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap index 750196e0eb6cc..906cd1e421fc0 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap @@ -6,7 +6,13 @@ expression: snapshot ```py x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) +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 ( # Dangling comment @@ -21,7 +27,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -29,7 +35,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -37,7 +43,7 @@ del ( # Deleting something x, # Deleted something # Finishing deletes -) # Completed +) # Completed # Done deleting # Delete something @@ -45,9 +51,8 @@ del ( # Deleting something x # Deleted something # Finishing deletes - # Dangling comment -) # Completed +) # Completed # Done deleting # Delete something @@ -61,9 +66,9 @@ del ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, - d + d, # Deleted -) # Completed +) # Completed # Done ``` @@ -73,7 +78,75 @@ del ( ```py x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -b, c, d = (2, 3, 4) +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 ( # Dangling comment @@ -88,7 +161,8 @@ del ( # Deleting something x # Deleted something # Finishing deletes -) # Completed + # Completed +) # Done deleting # Delete something @@ -96,7 +170,8 @@ del ( # Deleting something x # Deleted something # Finishing deletes -) # Completed + # Completed +) # Done deleting # Delete something @@ -113,11 +188,18 @@ del ( x # Deleted something # Finishing deletes # Dangling comment -) # Completed + # Completed +) # Done deleting # Delete something -del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Delete these # Ready to delete # Delete something From 2e676f3000320d80df4c6ee4c380eea3b0274330 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 8 Jul 2023 20:27:27 -0400 Subject: [PATCH 08/19] Update tests --- .../format@statement__delete.py.snap} | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) rename crates/ruff_python_formatter/{src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap => tests/snapshots/format@statement__delete.py.snap} (89%) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap similarity index 89% rename from crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap rename to crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap index 906cd1e421fc0..be5a509e7c476 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__delete_py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap @@ -1,12 +1,12 @@ --- -source: crates/ruff_python_formatter/src/lib.rs -expression: snapshot +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) +a, b, c, d = 1, 2, 3, 4 del a, b, c, d del a, b, c, d # Trailing @@ -72,13 +72,11 @@ del ( # Done ``` - - ## Output ```py x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -a, b, c, d = (1, 2, 3, 4) +a, b, c, d = 1, 2, 3, 4 del a, b, c, d del a, b, c, d # Trailing @@ -161,8 +159,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes - # Completed -) +) # Completed # Done deleting # Delete something @@ -170,8 +167,7 @@ del ( # Deleting something x # Deleted something # Finishing deletes - # Completed -) +) # Completed # Done deleting # Delete something @@ -188,18 +184,11 @@ del ( x # Deleted something # Finishing deletes # Dangling comment - # Completed -) +) # Completed # Done deleting # Delete something -del ( - x, - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - b, - c, - d, -) # Delete these +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these # Ready to delete # Delete something @@ -216,3 +205,4 @@ del ( ``` + From f57bc9e575fb5aaddafa32cd838db1e01d62ebd3 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 8 Jul 2023 20:28:17 -0400 Subject: [PATCH 09/19] Export default_expression_needs_parentheses for crate --- crates/ruff_python_formatter/src/expression/parentheses.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index c73b5773ce408..f97d8dec0d688 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -14,7 +14,7 @@ pub(crate) trait NeedsParentheses { ) -> Parentheses; } -pub(super) fn default_expression_needs_parentheses( +pub(crate) fn default_expression_needs_parentheses( node: AnyNodeRef, parenthesize: Parenthesize, source: &str, From a1d98558ebe5ff452920bf74bfa3c040e32c7d23 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sat, 8 Jul 2023 20:28:52 -0400 Subject: [PATCH 10/19] Use ExprSequence --- .../test/fixtures/ruff/statement/delete.py | 2 +- .../src/statement/stmt_delete.rs | 74 +++++++++++++------ 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py index 198c230ff79c9..b9da8ae91f738 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py @@ -1,6 +1,6 @@ x = 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1 -a, b, c, d = (1, 2, 3, 4) +a, b, c, d = 1, 2, 3, 4 del a, b, c, d del a, b, c, d # Trailing diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 5262632e426bf..6408e5c554694 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,9 +1,11 @@ -use crate::expression::parentheses::Parenthesize; +use crate::builders::{optional_parentheses, PyFormatterExtensions}; +use crate::comments::{dangling_node_comments, Comments}; +use crate::expression::parentheses::{ + default_expression_needs_parentheses, NeedsParentheses, 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::prelude::{block_indent, space, text, Formatter}; use ruff_formatter::{write, Buffer, Format, FormatResult}; use rustpython_parser::ast::{Expr, StmtDelete}; @@ -13,33 +15,63 @@ pub struct FormatStmtDelete; impl FormatNodeRule for FormatStmtDelete { fn fmt_fields(&self, item: &StmtDelete, f: &mut PyFormatter) -> FormatResult<()> { let StmtDelete { range: _, targets } = item; - write!(f, [text("del"), space(), DeleteList::new(targets)]) + + write!(f, [text("del"), space()])?; + + match targets.as_slice() { + [] => { + write!( + f, + [ + // Handle special case of delete statements without elements. + // ``` + // del ( + // # Dangling comment + // ) + &text("("), + block_indent(&dangling_node_comments(item)), + &text(")"), + ] + ) + } + // TODO(cnpryer): single and multiple targets should be handled the same since + // tuples require special formatting whereas this is just a sequence of expressions (tuple-like). + [single] => { + write!(f, [single.format().with_options(Parenthesize::IfBreaks)]) + } + targets => optional_parentheses(&ExprSequence::new(targets)).fmt(f), + } } } -// TODO(cnpryer): Impl FormatRuleWithOptions Parenthesize +// TODO(cnpryer): Shared `ExprSequence` (see expr_tuple.rs) #[derive(Debug)] -struct DeleteList<'a> { - delete_list: &'a [Expr], +struct ExprSequence<'a> { + targets: &'a [Expr], } -impl<'a> DeleteList<'a> { - const fn new(delete_list: &'a [Expr]) -> Self { - Self { delete_list } +impl<'a> ExprSequence<'a> { + const fn new(targets: &'a [Expr]) -> Self { + Self { targets } } } -impl Format> for DeleteList<'_> { +impl Format> for ExprSequence<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { - let separator = - format_with(|f| group(&format_args![text(","), soft_line_break_or_space(),]).fmt(f)); - 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)]) - })); + f.join_comma_separated().nodes(self.targets.iter()).finish() + } +} + +impl NeedsParentheses for StmtDelete { + fn needs_parentheses( + &self, + parenthesize: Parenthesize, + source: &str, + comments: &Comments, + ) -> Parentheses { + match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) { + Parentheses::Optional => Parentheses::Never, + parentheses => parentheses, } - join.finish() } } From 14b80b68ea75fb09fc6aa8cbad26b0963a184a26 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 12:49:26 -0400 Subject: [PATCH 11/19] Update snapshot for trailing comment test case Related: #5630 --- .../tests/snapshots/format@statement__delete.py.snap | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap index be5a509e7c476..1ca2a29e51025 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap @@ -188,7 +188,13 @@ del ( # Done deleting # Delete something -del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these +del ( + x, + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + b, + c, + d, +) # Delete these # Ready to delete # Delete something From 7ebd63c59fa39e41f23889d8b735cf4cfe01f989 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 14:16:40 -0400 Subject: [PATCH 12/19] Add test case for expecting no parentheses --- .../resources/test/fixtures/ruff/statement/delete.py | 5 +++++ crates/ruff_python_formatter/src/statement/stmt_delete.rs | 3 ++- .../tests/snapshots/format@statement__delete.py.snap | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py index b9da8ae91f738..7c8ec8101b8ab 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py @@ -8,6 +8,11 @@ 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 ) diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 6408e5c554694..a946dad9a991e 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -23,7 +23,7 @@ impl FormatNodeRule for FormatStmtDelete { write!( f, [ - // Handle special case of delete statements without elements. + // Handle special case of delete statements without targets. // ``` // del ( // # Dangling comment @@ -62,6 +62,7 @@ impl Format> for ExprSequence<'_> { } } +// NOTE: `default_expression_needs_parentheses` is reserved for expression nodes. impl NeedsParentheses for StmtDelete { fn needs_parentheses( &self, diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap index 1ca2a29e51025..b56a47d2c136f 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap @@ -14,6 +14,11 @@ 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 ) @@ -146,6 +151,8 @@ del ( a, ) # Trailing +del a, a + del ( # Dangling comment ) From 695df60cf4d1d4d2067749322d669b2c99f757ed Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 14:29:28 -0400 Subject: [PATCH 13/19] Update snapshot --- .../tests/snapshots/format@statement__delete.py.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap index b56a47d2c136f..8fd3462a27c3d 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap @@ -151,7 +151,7 @@ del ( a, ) # Trailing -del a, a +del (a, a) del ( # Dangling comment From ce249f5af3e65ac3eb1f5fddddf3607552b62dda Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 15:28:17 -0400 Subject: [PATCH 14/19] Don't expect trailing comments to trigger format --- .../resources/test/fixtures/ruff/statement/delete.py | 1 + .../tests/snapshots/format@statement__delete.py.snap | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py index 7c8ec8101b8ab..fa5efd652aa27 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/delete.py @@ -54,6 +54,7 @@ ) # 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 diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap index 8fd3462a27c3d..9ccbf27444cc4 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap @@ -60,6 +60,7 @@ del ( ) # 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 @@ -194,14 +195,9 @@ del ( ) # 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 +del x, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b, c, d # Delete these # Ready to delete # Delete something From b5fe6d58ddc80e6d56b0b9d4a5b4e0a9b9083b70 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 15:29:16 -0400 Subject: [PATCH 15/19] Move ExprSequence to sequence.rs --- .../src/expression/expr_tuple.rs | 19 +------- .../src/expression/mod.rs | 1 + .../src/expression/sequence.rs | 21 +++++++++ .../src/statement/stmt_delete.rs | 47 +++---------------- 4 files changed, 30 insertions(+), 58 deletions(-) create mode 100644 crates/ruff_python_formatter/src/expression/sequence.rs diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index de97a5bac95d7..c0b8dedacc042 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -10,6 +10,8 @@ use ruff_text_size::TextRange; use rustpython_parser::ast::ExprTuple; use rustpython_parser::ast::{Expr, Ranged}; +use super::sequence::ExprSequence; + #[derive(Eq, PartialEq, Debug, Default)] pub enum TupleParentheses { /// Effectively `None` in `Option` @@ -93,23 +95,6 @@ impl FormatNodeRule for FormatExprTuple { } } -#[derive(Debug)] -struct ExprSequence<'a> { - elts: &'a [Expr], -} - -impl<'a> ExprSequence<'a> { - const fn new(elts: &'a [Expr]) -> Self { - Self { elts } - } -} - -impl Format> for ExprSequence<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { - f.join_comma_separated().nodes(self.elts.iter()).finish() - } -} - impl NeedsParentheses for ExprTuple { fn needs_parentheses( &self, diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index ac3a4d682c637..1924b5a5de962 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -39,6 +39,7 @@ pub(crate) mod expr_unary_op; pub(crate) mod expr_yield; pub(crate) mod expr_yield_from; pub(crate) mod parentheses; +pub(crate) mod sequence; pub(crate) mod string; #[derive(Default)] diff --git a/crates/ruff_python_formatter/src/expression/sequence.rs b/crates/ruff_python_formatter/src/expression/sequence.rs new file mode 100644 index 0000000000000..9357a2100e16d --- /dev/null +++ b/crates/ruff_python_formatter/src/expression/sequence.rs @@ -0,0 +1,21 @@ +use ruff_formatter::{prelude::Formatter, Format, FormatResult}; +use rustpython_parser::ast::Expr; + +use crate::{builders::PyFormatterExtensions, context::PyFormatContext}; + +#[derive(Debug)] +pub(crate) struct ExprSequence<'a> { + elts: &'a [Expr], +} + +impl<'a> ExprSequence<'a> { + pub(crate) const fn new(elts: &'a [Expr]) -> Self { + Self { elts } + } +} + +impl Format> for ExprSequence<'_> { + fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { + f.join_comma_separated().nodes(self.elts.iter()).finish() + } +} diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index a946dad9a991e..9403968066b10 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,13 +1,11 @@ -use crate::builders::{optional_parentheses, PyFormatterExtensions}; -use crate::comments::{dangling_node_comments, Comments}; -use crate::expression::parentheses::{ - default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize, -}; -use crate::prelude::PyFormatContext; +use crate::builders::optional_parentheses; +use crate::comments::dangling_node_comments; +use crate::expression::parentheses::Parenthesize; +use crate::expression::sequence::ExprSequence; use crate::{AsFormat, FormatNodeRule, PyFormatter}; -use ruff_formatter::prelude::{block_indent, space, text, Formatter}; +use ruff_formatter::prelude::{block_indent, space, text}; use ruff_formatter::{write, Buffer, Format, FormatResult}; -use rustpython_parser::ast::{Expr, StmtDelete}; +use rustpython_parser::ast::StmtDelete; #[derive(Default)] pub struct FormatStmtDelete; @@ -43,36 +41,3 @@ impl FormatNodeRule for FormatStmtDelete { } } } - -// TODO(cnpryer): Shared `ExprSequence` (see expr_tuple.rs) -#[derive(Debug)] -struct ExprSequence<'a> { - targets: &'a [Expr], -} - -impl<'a> ExprSequence<'a> { - const fn new(targets: &'a [Expr]) -> Self { - Self { targets } - } -} - -impl Format> for ExprSequence<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { - f.join_comma_separated().nodes(self.targets.iter()).finish() - } -} - -// NOTE: `default_expression_needs_parentheses` is reserved for expression nodes. -impl NeedsParentheses for StmtDelete { - fn needs_parentheses( - &self, - parenthesize: Parenthesize, - source: &str, - comments: &Comments, - ) -> Parentheses { - match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) { - Parentheses::Optional => Parentheses::Never, - parentheses => parentheses, - } - } -} From dee077b5dcc31fb97739b8d9b4e70bedccc40c2d Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 20:24:51 -0400 Subject: [PATCH 16/19] Override fmt_dangling_comments --- crates/ruff_python_formatter/src/statement/stmt_delete.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 9403968066b10..df5f32ae9ad00 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -40,4 +40,9 @@ impl FormatNodeRule for FormatStmtDelete { targets => optional_parentheses(&ExprSequence::new(targets)).fmt(f), } } + + fn fmt_dangling_comments(&self, _node: &StmtDelete, _f: &mut PyFormatter) -> FormatResult<()> { + // Handled in `fmt_fields` + Ok(()) + } } From da5799d01b386d3edd87dbf1ff1c3d346df1a1a2 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 20:25:23 -0400 Subject: [PATCH 17/19] Small chores --- crates/ruff_python_formatter/src/expression/expr_tuple.rs | 3 +-- crates/ruff_python_formatter/src/expression/parentheses.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index c0b8dedacc042..48c0d86079317 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -4,14 +4,13 @@ use crate::expression::parentheses::{ default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses, Parenthesize, }; +use crate::expression::sequence::ExprSequence; use crate::prelude::*; use ruff_formatter::{format_args, write, FormatRuleWithOptions}; use ruff_text_size::TextRange; use rustpython_parser::ast::ExprTuple; use rustpython_parser::ast::{Expr, Ranged}; -use super::sequence::ExprSequence; - #[derive(Eq, PartialEq, Debug, Default)] pub enum TupleParentheses { /// Effectively `None` in `Option` diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index f97d8dec0d688..c73b5773ce408 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -14,7 +14,7 @@ pub(crate) trait NeedsParentheses { ) -> Parentheses; } -pub(crate) fn default_expression_needs_parentheses( +pub(super) fn default_expression_needs_parentheses( node: AnyNodeRef, parenthesize: Parenthesize, source: &str, From 71352fce03ab45c38bd4bda2aed254d0d8447ddc Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Sun, 9 Jul 2023 21:48:56 -0400 Subject: [PATCH 18/19] Use join_comma_separated instead of ExprSequence --- .../src/expression/expr_tuple.rs | 18 +++++++++++++++- .../src/expression/mod.rs | 1 - .../src/expression/sequence.rs | 21 ------------------- .../src/statement/stmt_delete.rs | 10 +++++---- 4 files changed, 23 insertions(+), 27 deletions(-) delete mode 100644 crates/ruff_python_formatter/src/expression/sequence.rs diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index 48c0d86079317..de97a5bac95d7 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -4,7 +4,6 @@ use crate::expression::parentheses::{ default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses, Parenthesize, }; -use crate::expression::sequence::ExprSequence; use crate::prelude::*; use ruff_formatter::{format_args, write, FormatRuleWithOptions}; use ruff_text_size::TextRange; @@ -94,6 +93,23 @@ impl FormatNodeRule for FormatExprTuple { } } +#[derive(Debug)] +struct ExprSequence<'a> { + elts: &'a [Expr], +} + +impl<'a> ExprSequence<'a> { + const fn new(elts: &'a [Expr]) -> Self { + Self { elts } + } +} + +impl Format> for ExprSequence<'_> { + fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { + f.join_comma_separated().nodes(self.elts.iter()).finish() + } +} + impl NeedsParentheses for ExprTuple { fn needs_parentheses( &self, diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 1924b5a5de962..ac3a4d682c637 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -39,7 +39,6 @@ pub(crate) mod expr_unary_op; pub(crate) mod expr_yield; pub(crate) mod expr_yield_from; pub(crate) mod parentheses; -pub(crate) mod sequence; pub(crate) mod string; #[derive(Default)] diff --git a/crates/ruff_python_formatter/src/expression/sequence.rs b/crates/ruff_python_formatter/src/expression/sequence.rs deleted file mode 100644 index 9357a2100e16d..0000000000000 --- a/crates/ruff_python_formatter/src/expression/sequence.rs +++ /dev/null @@ -1,21 +0,0 @@ -use ruff_formatter::{prelude::Formatter, Format, FormatResult}; -use rustpython_parser::ast::Expr; - -use crate::{builders::PyFormatterExtensions, context::PyFormatContext}; - -#[derive(Debug)] -pub(crate) struct ExprSequence<'a> { - elts: &'a [Expr], -} - -impl<'a> ExprSequence<'a> { - pub(crate) const fn new(elts: &'a [Expr]) -> Self { - Self { elts } - } -} - -impl Format> for ExprSequence<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { - f.join_comma_separated().nodes(self.elts.iter()).finish() - } -} diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index df5f32ae9ad00..0f9fadec976af 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,9 +1,8 @@ -use crate::builders::optional_parentheses; +use crate::builders::{optional_parentheses, PyFormatterExtensions}; use crate::comments::dangling_node_comments; use crate::expression::parentheses::Parenthesize; -use crate::expression::sequence::ExprSequence; use crate::{AsFormat, FormatNodeRule, PyFormatter}; -use ruff_formatter::prelude::{block_indent, space, text}; +use ruff_formatter::prelude::{block_indent, format_with, space, text}; use ruff_formatter::{write, Buffer, Format, FormatResult}; use rustpython_parser::ast::StmtDelete; @@ -37,7 +36,10 @@ impl FormatNodeRule for FormatStmtDelete { [single] => { write!(f, [single.format().with_options(Parenthesize::IfBreaks)]) } - targets => optional_parentheses(&ExprSequence::new(targets)).fmt(f), + targets => { + let item = format_with(|f| f.join_comma_separated().nodes(targets.iter()).finish()); + optional_parentheses(&item).fmt(f) + } } } From f16ecb1597e8b3d05917d402b8f6765a954f5414 Mon Sep 17 00:00:00 2001 From: Chris Pryer Date: Mon, 10 Jul 2023 12:14:19 -0400 Subject: [PATCH 19/19] Small chore --- crates/ruff_python_formatter/src/statement/stmt_delete.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 0f9fadec976af..e53ff784c7eef 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -31,8 +31,6 @@ impl FormatNodeRule for FormatStmtDelete { ] ) } - // TODO(cnpryer): single and multiple targets should be handled the same since - // tuples require special formatting whereas this is just a sequence of expressions (tuple-like). [single] => { write!(f, [single.format().with_options(Parenthesize::IfBreaks)]) }