From 2759db66043240dae65e683e320d268abdd2a119 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 21 Sep 2023 10:04:49 -0400 Subject: [PATCH] Allow up to two newlines after trailing clause body comments (#7557) ## Summary The number of newlines after a trailing comment in a clause body needs to follow the usual rules -- so, up to two for top-level, up to one for nested, etc. For example, Black preserves both newlines after `# comment` here: ```python if True: pass # comment else: pass ``` But it truncates to one newline here: ```python if True: if True: pass # comment else: pass else: pass ``` ## Test Plan Significant improvement on `transformers`. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99957 | 2587 | 402 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | **transformers** | **0.99963** | **2587** | **323** | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | --- .../test/fixtures/ruff/statement/if.py | 48 ++++++++++ .../src/comments/format.rs | 4 +- .../snapshots/format@statement__if.py.snap | 95 +++++++++++++++++++ 3 files changed, 144 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py index 682e85e763..a8a359f19c 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py @@ -138,6 +138,54 @@ if True: else: pass +if True: + pass + + # comment + +else: + pass + +if True: + pass + + # comment + + +else: + pass + +if True: + pass + + # comment + + + +else: + pass + +if True: + if True: + pass + # comment + + else: + pass +else: + pass + +if True: + if True: + pass + # comment + + + else: + pass +else: + pass + # Regression test for https://github.com/astral-sh/ruff/issues/5337 if parent_body: if current_body: diff --git a/crates/ruff_python_formatter/src/comments/format.rs b/crates/ruff_python_formatter/src/comments/format.rs index 7da0eaf46b..559b7e605b 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -101,9 +101,7 @@ impl Format> for FormatLeadingAlternateBranchComments<'_> { } else { last_preceding.end() }; - if lines_after(end, f.context().source()) > 1 { - write!(f, [empty_line()])?; - } + write!(f, [empty_lines(lines_after(end, f.context().source()))])?; } Ok(()) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap index 02ff05a955..dbafb690c0 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap @@ -144,6 +144,54 @@ if True: else: pass +if True: + pass + + # comment + +else: + pass + +if True: + pass + + # comment + + +else: + pass + +if True: + pass + + # comment + + + +else: + pass + +if True: + if True: + pass + # comment + + else: + pass +else: + pass + +if True: + if True: + pass + # comment + + + else: + pass +else: + pass + # Regression test for https://github.com/astral-sh/ruff/issues/5337 if parent_body: if current_body: @@ -197,6 +245,7 @@ elif aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ]: ... + else: ... @@ -305,6 +354,52 @@ if True: else: pass +if True: + pass + + # comment + +else: + pass + +if True: + pass + + # comment + + +else: + pass + +if True: + pass + + # comment + + +else: + pass + +if True: + if True: + pass + # comment + + else: + pass +else: + pass + +if True: + if True: + pass + # comment + + else: + pass +else: + pass + # Regression test for https://github.com/astral-sh/ruff/issues/5337 if parent_body: if current_body: