@@ -164,7 +164,7 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
164164 line_suffix(
165165 & format_args![
166166 empty_lines( lines_before_comment) ,
167- format_comment( trailing)
167+ format_comment( trailing) ,
168168 ] ,
169169 // Reserving width isn't necessary because we don't split
170170 // comments and the empty lines expand any enclosing group.
@@ -535,31 +535,21 @@ fn strip_comment_prefix(comment_text: &str) -> FormatResult<&str> {
535535/// ```
536536///
537537/// 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 ] ,
541540 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 {
551542 FormatEmptyLinesBeforeTrailingComments {
552543 comments,
553- empty_lines ,
544+ node_kind ,
554545 }
555546}
556547
557548#[ derive( Copy , Clone , Debug ) ]
558549pub ( crate ) struct FormatEmptyLinesBeforeTrailingComments < ' a > {
559550 /// The trailing comments of the node.
560551 comments : & ' a [ SourceComment ] ,
561- /// The expected number of empty lines before the trailing comments.
562- empty_lines : u32 ,
552+ node_kind : NodeKind ,
563553}
564554
565555impl Format < PyFormatContext < ' _ > > for FormatEmptyLinesBeforeTrailingComments < ' _ > {
@@ -569,9 +559,17 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesBeforeTrailingComments<'_>
569559 . iter ( )
570560 . find ( |comment| comment. line_position ( ) . is_own_line ( ) )
571561 {
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+
572570 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 ) ?;
575573 }
576574 }
577575 Ok ( ( ) )
@@ -590,30 +588,16 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesBeforeTrailingComments<'_>
590588///
591589/// While `leading_comments` will preserve the existing empty line, this builder will insert an
592590/// 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 }
609595}
610596
611597#[ derive( Copy , Clone , Debug ) ]
612598pub ( crate ) struct FormatEmptyLinesAfterLeadingComments < ' a > {
613599 /// The leading comments of the node.
614600 comments : & ' a [ SourceComment ] ,
615- /// The expected number of empty lines after the leading comments.
616- empty_lines : u32 ,
617601}
618602
619603impl Format < PyFormatContext < ' _ > > for FormatEmptyLinesAfterLeadingComments < ' _ > {
@@ -624,6 +608,14 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesAfterLeadingComments<'_> {
624608 . rev ( )
625609 . find ( |comment| comment. line_position ( ) . is_own_line ( ) )
626610 {
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+
627619 let actual = lines_after ( comment. end ( ) , f. context ( ) . source ( ) ) . saturating_sub ( 1 ) ;
628620 // If there are no empty lines, keep the comment tight to the node.
629621 if actual == 0 {
@@ -632,12 +624,12 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLinesAfterLeadingComments<'_> {
632624
633625 // If there are more than enough empty lines already, `leading_comments` will
634626 // trim them as necessary.
635- if actual >= self . empty_lines {
627+ if actual >= empty_lines {
636628 return Ok ( ( ) ) ;
637629 }
638630
639- for _ in actual..self . empty_lines {
640- write ! ( f , [ empty_line( ) ] ) ?;
631+ for _ in actual..empty_lines {
632+ empty_line ( ) . fmt ( f ) ?;
641633 }
642634 }
643635 Ok ( ( ) )
0 commit comments