@@ -164,7 +164,7 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
164
164
line_suffix(
165
165
& format_args![
166
166
empty_lines( lines_before_comment) ,
167
- format_comment( trailing)
167
+ format_comment( trailing) ,
168
168
] ,
169
169
// Reserving width isn't necessary because we don't split
170
170
// comments and the empty lines expand any enclosing group.
@@ -535,31 +535,21 @@ fn strip_comment_prefix(comment_text: &str) -> FormatResult<&str> {
535
535
/// ```
536
536
///
537
537
/// This builder will insert a single empty line before the comment.
538
- pub ( crate ) fn empty_lines_before_trailing_comments < ' a > (
539
- f : & PyFormatter ,
540
- comments : & ' a [ SourceComment ] ,
538
+ pub ( crate ) fn empty_lines_before_trailing_comments (
539
+ comments : & [ SourceComment ] ,
541
540
node_kind : NodeKind ,
542
- ) -> FormatEmptyLinesBeforeTrailingComments < ' a > {
543
- // Black has different rules for stub vs. non-stub and top level vs. indented
544
- let empty_lines = match ( f. options ( ) . source_type ( ) , f. context ( ) . node_level ( ) ) {
545
- ( PySourceType :: Stub , NodeLevel :: TopLevel ( _) ) => 1 ,
546
- ( PySourceType :: Stub , _) => u32:: from ( node_kind == NodeKind :: StmtClassDef ) ,
547
- ( _, NodeLevel :: TopLevel ( _) ) => 2 ,
548
- ( _, _) => 1 ,
549
- } ;
550
-
541
+ ) -> FormatEmptyLinesBeforeTrailingComments {
551
542
FormatEmptyLinesBeforeTrailingComments {
552
543
comments,
553
- empty_lines ,
544
+ node_kind ,
554
545
}
555
546
}
556
547
557
548
#[ derive( Copy , Clone , Debug ) ]
558
549
pub ( crate ) struct FormatEmptyLinesBeforeTrailingComments < ' a > {
559
550
/// The trailing comments of the node.
560
551
comments : & ' a [ SourceComment ] ,
561
- /// The expected number of empty lines before the trailing comments.
562
- empty_lines : u32 ,
552
+ node_kind : NodeKind ,
563
553
}
564
554
565
555
impl Format < PyFormatContext < ' _ > > for FormatEmptyLinesBeforeTrailingComments < ' _ > {
@@ -569,9 +559,17 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesBeforeTrailingComments<'_>
569
559
. iter ( )
570
560
. find ( |comment| comment. line_position ( ) . is_own_line ( ) )
571
561
{
562
+ // Black has different rules for stub vs. non-stub and top level vs. indented
563
+ let empty_lines = match ( f. options ( ) . source_type ( ) , f. context ( ) . node_level ( ) ) {
564
+ ( PySourceType :: Stub , NodeLevel :: TopLevel ( _) ) => 1 ,
565
+ ( PySourceType :: Stub , _) => u32:: from ( self . node_kind == NodeKind :: StmtClassDef ) ,
566
+ ( _, NodeLevel :: TopLevel ( _) ) => 2 ,
567
+ ( _, _) => 1 ,
568
+ } ;
569
+
572
570
let actual = lines_before ( comment. start ( ) , f. context ( ) . source ( ) ) . saturating_sub ( 1 ) ;
573
- for _ in actual..self . empty_lines {
574
- write ! ( f , [ empty_line( ) ] ) ?;
571
+ for _ in actual..empty_lines {
572
+ empty_line ( ) . fmt ( f ) ?;
575
573
}
576
574
}
577
575
Ok ( ( ) )
@@ -590,30 +588,16 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesBeforeTrailingComments<'_>
590
588
///
591
589
/// While `leading_comments` will preserve the existing empty line, this builder will insert an
592
590
/// additional empty line before the comment.
593
- pub ( crate ) fn empty_lines_after_leading_comments < ' a > (
594
- f : & PyFormatter ,
595
- comments : & ' a [ SourceComment ] ,
596
- ) -> FormatEmptyLinesAfterLeadingComments < ' a > {
597
- // Black has different rules for stub vs. non-stub and top level vs. indented
598
- let empty_lines = match ( f. options ( ) . source_type ( ) , f. context ( ) . node_level ( ) ) {
599
- ( PySourceType :: Stub , NodeLevel :: TopLevel ( _) ) => 1 ,
600
- ( PySourceType :: Stub , _) => 0 ,
601
- ( _, NodeLevel :: TopLevel ( _) ) => 2 ,
602
- ( _, _) => 1 ,
603
- } ;
604
-
605
- FormatEmptyLinesAfterLeadingComments {
606
- comments,
607
- empty_lines,
608
- }
591
+ pub ( crate ) fn empty_lines_after_leading_comments (
592
+ comments : & [ SourceComment ] ,
593
+ ) -> FormatEmptyLinesAfterLeadingComments {
594
+ FormatEmptyLinesAfterLeadingComments { comments }
609
595
}
610
596
611
597
#[ derive( Copy , Clone , Debug ) ]
612
598
pub ( crate ) struct FormatEmptyLinesAfterLeadingComments < ' a > {
613
599
/// The leading comments of the node.
614
600
comments : & ' a [ SourceComment ] ,
615
- /// The expected number of empty lines after the leading comments.
616
- empty_lines : u32 ,
617
601
}
618
602
619
603
impl Format < PyFormatContext < ' _ > > for FormatEmptyLinesAfterLeadingComments < ' _ > {
@@ -624,6 +608,14 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesAfterLeadingComments<'_> {
624
608
. rev ( )
625
609
. find ( |comment| comment. line_position ( ) . is_own_line ( ) )
626
610
{
611
+ // Black has different rules for stub vs. non-stub and top level vs. indented
612
+ let empty_lines = match ( f. options ( ) . source_type ( ) , f. context ( ) . node_level ( ) ) {
613
+ ( PySourceType :: Stub , NodeLevel :: TopLevel ( _) ) => 1 ,
614
+ ( PySourceType :: Stub , _) => 0 ,
615
+ ( _, NodeLevel :: TopLevel ( _) ) => 2 ,
616
+ ( _, _) => 1 ,
617
+ } ;
618
+
627
619
let actual = lines_after ( comment. end ( ) , f. context ( ) . source ( ) ) . saturating_sub ( 1 ) ;
628
620
// If there are no empty lines, keep the comment tight to the node.
629
621
if actual == 0 {
@@ -632,12 +624,12 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesAfterLeadingComments<'_> {
632
624
633
625
// If there are more than enough empty lines already, `leading_comments` will
634
626
// trim them as necessary.
635
- if actual >= self . empty_lines {
627
+ if actual >= empty_lines {
636
628
return Ok ( ( ) ) ;
637
629
}
638
630
639
- for _ in actual..self . empty_lines {
640
- write ! ( f , [ empty_line( ) ] ) ?;
631
+ for _ in actual..empty_lines {
632
+ empty_line ( ) . fmt ( f ) ?;
641
633
}
642
634
}
643
635
Ok ( ( ) )
0 commit comments