Skip to content

Commit

Permalink
Ignore blank lines between comments when counting newlines-after-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 22, 2023
1 parent 8bfe9bd commit 0229d14
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Expand Up @@ -56,3 +56,12 @@ def func():


x = 1

# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os

# Defaults for arguments are defined here
# args.threshold = None;


logger = logging.getLogger("FastProject")
7 changes: 6 additions & 1 deletion crates/ruff_python_formatter/src/statement/suite.rs
Expand Up @@ -190,7 +190,12 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
// a leading comment.
match self.kind {
SuiteKind::TopLevel => {
match lines_after_ignoring_trivia(preceding.end(), source) {
let end = if let Some(last_trailing) = preceding_comments.trailing.last() {
last_trailing.end()
} else {
preceding.end()
};
match lines_after(end, source) {
0..=2 => empty_line().fmt(f)?,
_ => match source_type {
PySourceType::Stub => {
Expand Down
Expand Up @@ -62,6 +62,15 @@ def func():
x = 1
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os
# Defaults for arguments are defined here
# args.threshold = None;
logger = logging.getLogger("FastProject")
```

## Output
Expand Down Expand Up @@ -130,6 +139,16 @@ def func():
import sys
x = 1
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os
# Defaults for arguments are defined here
# args.threshold = None;
logger = logging.getLogger("FastProject")
```


Expand Down

0 comments on commit 0229d14

Please sign in to comment.