Skip to content

Commit

Permalink
Collapse multiple lines after import into single line
Browse files Browse the repository at this point in the history
  • Loading branch information
kastkeepitjumpinlikekangaroos committed Oct 16, 2024
1 parent 9995bff commit 40f47c8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<!-- Changes that affect Black's stable style -->

- Collapse multiple lines into 1 after import (#4488)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
11 changes: 10 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,16 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: # noqa: C9
# Consume the first leaf's extra newlines.
first_leaf = current_line.leaves[0]
before = first_leaf.prefix.count("\n")
before = min(before, max_allowed)
before = (
1
if self.previous_line
and self.previous_line.is_import
and self.previous_line.depth == 0
and current_line.depth == 0
and not current_line.is_import
and not current_line.is_comment
else min(before, max_allowed)
)
first_leaf.prefix = ""
else:
before = 0
Expand Down
12 changes: 12 additions & 0 deletions tests/data/cases/import_line_collapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from middleman.authentication import validate_oauth_token


logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
13 changes: 13 additions & 0 deletions tests/data/cases/import_line_collapse_3_to_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from middleman.authentication import validate_oauth_token



logger = logging.getLogger(__name__)


# output


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)
1 change: 0 additions & 1 deletion tests/data/cases/preview_comments7.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def test_fails_invalid_post_data(
MyLovelyCompanyTeamProjectComponent as component, # DRY
)


result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down

0 comments on commit 40f47c8

Please sign in to comment.