Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kastkeepitjumpinlikekangaroos committed Nov 15, 2024
1 parent 4d01064 commit bc95691
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 72 deletions.
20 changes: 10 additions & 10 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,7 @@ 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 = (
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 Preview.always_one_newline_after_import in self.mode
else min(before, max_allowed)
)
before = min(before, max_allowed)
first_leaf.prefix = ""
else:
before = 0
Expand Down Expand Up @@ -680,6 +671,15 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: # noqa: C9
current_line, before, user_had_newline
)

if (
self.previous_line.is_import
and self.previous_line.depth == 0
and current_line.depth == 0
and not current_line.is_import
and Preview.always_one_newline_after_import in self.mode
):
return 1, 0

if (
self.previous_line.is_import
and not current_line.is_import
Expand Down
108 changes: 108 additions & 0 deletions tests/data/cases/preview_import_line_collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,61 @@
logger = logging.getLogger(__name__)


from middleman.authentication import validate_oauth_token



logger = logging.getLogger(__name__)


# case 4 try catch with import after import
import os
import os



try:
import os
except Exception:
pass

try:
import os
def func():
a = 1
except Exception:
pass


# case 5 multiple imports
import os
import os

import os
import os





for i in range(10):
print(i)


# case 6 import in function
def func():
print()
import os
def func():
pass
print()


def func():
import os
a = 1
print()

# output


Expand All @@ -39,3 +94,56 @@

# comment
logger = logging.getLogger(__name__)


from middleman.authentication import validate_oauth_token

logger = logging.getLogger(__name__)


# case 4 try catch with import after import
import os
import os

try:
import os
except Exception:
pass

try:
import os

def func():
a = 1

except Exception:
pass


# case 5 multiple imports
import os
import os

import os
import os

for i in range(10):
print(i)


# case 6 import in function
def func():
print()
import os

def func():
pass

print()


def func():
import os

a = 1
print()
62 changes: 0 additions & 62 deletions tests/data/cases/preview_import_line_collapse_3_to_1.py

This file was deleted.

0 comments on commit bc95691

Please sign in to comment.