From a6d79c03b3e2052e35f8f0e21e944dc3e09627e2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 27 Sep 2023 20:16:40 -0400 Subject: [PATCH] Break `with` on end-of-line trailing comments (#7685) ## Summary Ensures that: ```python with ( a # comment ): pass ``` Retains its parentheses. Closes https://github.com/astral-sh/ruff/issues/6750. ## Test Plan `cargo test` --- .../test/fixtures/ruff/statement/with.py | 34 +++++++ .../src/statement/stmt_with.rs | 6 +- .../snapshots/format@statement__with.py.snap | 90 +++++++++++++++++-- 3 files changed, 118 insertions(+), 12 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py index 03ab7a0a3b..a9d94a2026 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py @@ -108,6 +108,40 @@ with ( b # trailing b same line comment ): ... +with ( + # comment + a +): + pass + +with ( + a # comment +): + pass + +with ( + a + # comment +): + pass + +with ( + # comment + a as b +): + pass + +with ( + a as b # comment +): + pass + +with ( + a as b + # comment +): + pass + with ( [ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", diff --git a/crates/ruff_python_formatter/src/statement/stmt_with.rs b/crates/ruff_python_formatter/src/statement/stmt_with.rs index 9aa5e54bda..a71f73e6b2 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_with.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_with.rs @@ -84,9 +84,9 @@ impl FormatNodeRule for FormatStmtWith { })) .fmt(f)?; } else if let [item] = item.items.as_slice() { - // This is similar to `maybe_parenthesize_expression`, but we're not dealing with an - // expression here, it's a `WithItem`. - if comments.has_leading(item) || comments.has_trailing_own_line(item) { + // This is similar to `maybe_parenthesize_expression`, but we're not + // dealing with an expression here, it's a `WithItem`. + if comments.has_leading(item) || comments.has_trailing(item) { optional_parentheses(&item.format()).fmt(f)?; } else { item.format().fmt(f)?; diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap index 1c54019b65..e49f4aaab2 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap @@ -114,6 +114,40 @@ with ( b # trailing b same line comment ): ... +with ( + # comment + a +): + pass + +with ( + a # comment +): + pass + +with ( + a + # comment +): + pass + +with ( + # comment + a as b +): + pass + +with ( + a as b # comment +): + pass + +with ( + a as b + # comment +): + pass + with ( [ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", @@ -297,10 +331,12 @@ with ( ... # body # body trailing own -with a as ( # a # as - # own line - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb -): # b +with ( + a as ( # a # as + # own line + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + ) # b +): pass @@ -372,13 +408,49 @@ with ( ... with ( - a - # trailing own line comment -) as ( # trailing as same line comment - b -): # trailing b same line comment + ( + a + # trailing own line comment + ) as ( # trailing as same line comment + b + ) # trailing b same line comment +): ... +with ( + # comment + a +): + pass + +with ( + a # comment +): + pass + +with ( + a + # comment +): + pass + +with ( + # comment + a as b +): + pass + +with ( + a as b # comment +): + pass + +with ( + a as b + # comment +): + pass + with ( [ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",