From 7f1456a2c9019f543fde71493aa476f132c84a57 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 21 Sep 2023 10:52:38 -0400 Subject: [PATCH] Allow up to two newlines before trailing clause body comments (#7575) ## Summary This is the peer to https://github.com/astral-sh/ruff/pull/7557, but for "leading" clause comments, like: ```python if True: pass # comment else: pass ``` In this case, we again want to allow up to two newlines at the top level. ## Test Plan `cargo test` No changes. Before: | 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 | 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 | 22 ++++++++++ .../src/comments/format.rs | 10 +++-- .../snapshots/format@statement__if.py.snap | 44 +++++++++++++++++++ 3 files changed, 73 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 a8a359f19c..1ed151c97d 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 @@ -186,6 +186,28 @@ if True: else: pass +if True: + pass +# comment +else: + pass + +if True: + pass + +# comment +else: + pass + +if True: + pass + + +# comment +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 559b7e605b..a5004da717 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -86,9 +86,13 @@ impl Format> for FormatLeadingAlternateBranchComments<'_> { if let Some(first_leading) = self.comments.first() { // Leading comments only preserves the lines after the comment but not before. // Insert the necessary lines. - if lines_before(first_leading.start(), f.context().source()) > 1 { - write!(f, [empty_line()])?; - } + write!( + f, + [empty_lines(lines_before( + first_leading.start(), + f.context().source() + ))] + )?; write!(f, [leading_comments(self.comments)])?; } else if let Some(last_preceding) = self.last_node { 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 dbafb690c0..f85326b157 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 @@ -192,6 +192,28 @@ if True: else: pass +if True: + pass +# comment +else: + pass + +if True: + pass + +# comment +else: + pass + +if True: + pass + + +# comment +else: + pass + + # Regression test for https://github.com/astral-sh/ruff/issues/5337 if parent_body: if current_body: @@ -400,6 +422,28 @@ if True: else: pass +if True: + pass +# comment +else: + pass + +if True: + pass + +# comment +else: + pass + +if True: + pass + + +# comment +else: + pass + + # Regression test for https://github.com/astral-sh/ruff/issues/5337 if parent_body: if current_body: