Skip to content

Commit 14c3f17

Browse files
The Pyink Maintainerscopybara-github
authored andcommitted
Prevent adding blank lines between docstrings and subsequent class/def definitions when formatting a line range.
PiperOrigin-RevId: 877891171
1 parent eef4ed8 commit 14c3f17

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

patches/pyink.patch

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,16 @@
688688
and bool(current_line.leaves)
689689
and "\f\n" in current_line.leaves[0].prefix
690690
)
691-
@@ -618,7 +639,7 @@ class EmptyLineTracker:
691+
@@ -611,6 +632,8 @@ class EmptyLineTracker:
692+
or current_line.is_def
693+
):
694+
return False
695+
+ if previous_block.original_line.depth:
696+
+ return False
697+
while previous_block := previous_block.previous_block:
698+
if not previous_block.original_line.is_comment:
699+
return False
700+
@@ -618,7 +641,7 @@ class EmptyLineTracker:
692701

693702
def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]:
694703
max_allowed = 1
@@ -697,7 +706,7 @@
697706
max_allowed = 1 if self.mode.is_pyi else 2
698707

699708
if current_line.leaves:
700-
@@ -635,7 +656,7 @@ class EmptyLineTracker:
709+
@@ -635,7 +658,7 @@ class EmptyLineTracker:
701710

702711
# Mutate self.previous_defs, remainder of this function should be pure
703712
previous_def = None
@@ -706,7 +715,7 @@
706715
previous_def = self.previous_defs.pop()
707716
if current_line.is_def or current_line.is_class:
708717
self.previous_defs.append(current_line)
709-
@@ -692,8 +713,8 @@ class EmptyLineTracker:
718+
@@ -692,8 +715,8 @@ class EmptyLineTracker:
710719

711720
if (
712721
self.previous_line.is_import
@@ -717,7 +726,7 @@
717726
and not current_line.is_import
718727
and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
719728
and Preview.always_one_newline_after_import in self.mode
720-
@@ -701,10 +722,25 @@ class EmptyLineTracker:
729+
@@ -701,10 +724,25 @@ class EmptyLineTracker:
721730
return 1, 0
722731

723732
if (
@@ -745,7 +754,7 @@
745754
):
746755
return (before or 1), 0
747756

748-
@@ -721,8 +757,9 @@ class EmptyLineTracker:
757+
@@ -721,8 +759,9 @@ class EmptyLineTracker:
749758
return 0, 1
750759
return 0, 0
751760

@@ -757,7 +766,7 @@
757766
):
758767
if self.mode.is_pyi:
759768
return 0, 0
760-
@@ -731,7 +768,7 @@ class EmptyLineTracker:
769+
@@ -731,7 +770,7 @@ class EmptyLineTracker:
761770
comment_to_add_newlines: LinesBlock | None = None
762771
if (
763772
self.previous_line.is_comment
@@ -766,7 +775,7 @@
766775
and before == 0
767776
):
768777
slc = self.semantic_leading_comment
769-
@@ -748,9 +785,9 @@ class EmptyLineTracker:
778+
@@ -748,9 +787,9 @@ class EmptyLineTracker:
770779

771780
if self.mode.is_pyi:
772781
if current_line.is_class or self.previous_line.is_class:
@@ -778,7 +787,7 @@
778787
newlines = 1
779788
elif current_line.is_stub_class and self.previous_line.is_stub_class:
780789
# No blank line between classes with an empty body
781-
@@ -779,7 +816,11 @@ class EmptyLineTracker:
790+
@@ -779,7 +818,11 @@ class EmptyLineTracker:
782791
newlines = 1 if current_line.depth else 2
783792
# If a user has left no space after a dummy implementation, don't insert
784793
# new lines. This is useful for instance for @overload or Protocols.
@@ -791,7 +800,7 @@
791800
newlines = 0
792801
if comment_to_add_newlines is not None:
793802
previous_block = comment_to_add_newlines.previous_block
794-
@@ -1054,7 +1095,7 @@ def can_omit_invisible_parens(
803+
@@ -1054,7 +1097,7 @@ def can_omit_invisible_parens(
795804
def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
796805
"""See `can_omit_invisible_parens`."""
797806
remainder = False
@@ -800,7 +809,7 @@
800809
_index = -1
801810
for _index, leaf, leaf_length in line.enumerate_with_length():
802811
if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
803-
@@ -1078,7 +1119,7 @@ def _can_omit_opening_paren(line: Line,
812+
@@ -1078,7 +1121,7 @@ def _can_omit_opening_paren(line: Line,
804813

805814
def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
806815
"""See `can_omit_invisible_parens`."""

src/pyink/lines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,8 @@ def _line_is_module_docstring(self, current_line: Line) -> bool:
632632
or current_line.is_def
633633
):
634634
return False
635+
if previous_block.original_line.depth:
636+
return False
635637
while previous_block := previous_block.previous_block:
636638
if not previous_block.original_line.is_comment:
637639
return False

0 commit comments

Comments
 (0)