@@ -565,14 +565,9 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
565
565
)
566
566
before , after = self ._maybe_empty_lines (current_line )
567
567
previous_after = self .previous_block .after if self .previous_block else 0
568
- before = (
569
- # Black should not insert empty lines at the beginning
570
- # of the file
571
- 0
572
- if self .previous_line is None
573
- else before - previous_after
574
- )
568
+ before = max (0 , before - previous_after )
575
569
if (
570
+ # Always have one empty line after a module docstring
576
571
self .previous_block
577
572
and self .previous_block .previous_block is None
578
573
and len (self .previous_block .original_line .leaves ) == 1
@@ -607,10 +602,11 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
607
602
self .previous_block = block
608
603
return block
609
604
610
- def _maybe_empty_lines (self , current_line : Line ) -> Tuple [int , int ]:
605
+ def _maybe_empty_lines (self , current_line : Line ) -> Tuple [int , int ]: # noqa: C901
611
606
max_allowed = 1
612
607
if current_line .depth == 0 :
613
608
max_allowed = 1 if self .mode .is_pyi else 2
609
+
614
610
if current_line .leaves :
615
611
# Consume the first leaf's extra newlines.
616
612
first_leaf = current_line .leaves [0 ]
@@ -623,9 +619,22 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
623
619
user_had_newline = bool (before )
624
620
depth = current_line .depth
625
621
622
+ # Mutate self.previous_defs, remainder of this function should be pure
626
623
previous_def = None
627
624
while self .previous_defs and self .previous_defs [- 1 ].depth >= depth :
628
625
previous_def = self .previous_defs .pop ()
626
+ if current_line .is_def or current_line .is_class :
627
+ self .previous_defs .append (current_line )
628
+
629
+ if self .previous_line is None :
630
+ # Don't insert empty lines before the first line in the file.
631
+ return 0 , 0
632
+
633
+ if current_line .is_docstring :
634
+ if self .previous_line .is_class :
635
+ return 0 , 1
636
+ if self .previous_line .opens_block and self .previous_line .is_def :
637
+ return 0 , 0
629
638
630
639
if previous_def is not None :
631
640
assert self .previous_line is not None
@@ -668,58 +677,32 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
668
677
)
669
678
670
679
if (
671
- self .previous_line
672
- and self .previous_line .is_import
680
+ self .previous_line .is_import
673
681
and not current_line .is_import
674
682
and not current_line .is_fmt_pass_converted (first_leaf_matches = is_import )
675
683
and depth == self .previous_line .depth
676
684
):
677
685
return (before or 1 ), 0
678
686
679
- if (
680
- self .previous_line
681
- and self .previous_line .is_class
682
- and current_line .is_docstring
683
- ):
684
- return 0 , 1
685
-
686
- # In preview mode, always allow blank lines, except right before a function
687
- # docstring
688
- is_empty_first_line_ok = not current_line .is_docstring or (
689
- self .previous_line and not self .previous_line .is_def
690
- )
691
-
692
- if (
693
- self .previous_line
694
- and self .previous_line .opens_block
695
- and not is_empty_first_line_ok
696
- ):
697
- return 0 , 0
698
687
return before , 0
699
688
700
689
def _maybe_empty_lines_for_class_or_def ( # noqa: C901
701
690
self , current_line : Line , before : int , user_had_newline : bool
702
691
) -> Tuple [int , int ]:
703
- if not current_line .is_decorator :
704
- self .previous_defs .append (current_line )
705
- if self .previous_line is None :
706
- # Don't insert empty lines before the first line in the file.
707
- return 0 , 0
692
+ assert self .previous_line is not None
708
693
709
694
if self .previous_line .is_decorator :
710
695
if self .mode .is_pyi and current_line .is_stub_class :
711
696
# Insert an empty line after a decorated stub class
712
697
return 0 , 1
713
-
714
698
return 0 , 0
715
699
716
700
if self .previous_line .depth < current_line .depth and (
717
701
self .previous_line .is_class or self .previous_line .is_def
718
702
):
719
703
if self .mode .is_pyi :
720
704
return 0 , 0
721
- else :
722
- return 1 if user_had_newline else 0 , 0
705
+ return 1 if user_had_newline else 0 , 0
723
706
724
707
comment_to_add_newlines : Optional [LinesBlock ] = None
725
708
if (
@@ -750,9 +733,6 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
750
733
newlines = 0
751
734
else :
752
735
newlines = 1
753
- # Remove case `self.previous_line.depth > current_line.depth` below when
754
- # this becomes stable.
755
- #
756
736
# Don't inspect the previous line if it's part of the body of the previous
757
737
# statement in the same level, we always want a blank line if there's
758
738
# something with a body preceding.
@@ -769,8 +749,6 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
769
749
# Blank line between a block of functions (maybe with preceding
770
750
# decorators) and a block of non-functions
771
751
newlines = 1
772
- elif self .previous_line .depth > current_line .depth :
773
- newlines = 1
774
752
else :
775
753
newlines = 0
776
754
else :
0 commit comments