@@ -643,7 +643,7 @@ pub fn format_end_token(
643643 shape : Shape ,
644644) -> TokenReference {
645645 // Indent any comments leading a token, as these comments are technically part of the function body block
646- let formatted_leading_trivia: Vec < Token > = load_token_trivia (
646+ let mut formatted_leading_trivia: Vec < Token > = load_token_trivia (
647647 ctx,
648648 current_token. leading_trivia ( ) . collect ( ) ,
649649 FormatTokenType :: LeadingTrivia ,
@@ -662,37 +662,39 @@ pub fn format_end_token(
662662 shape,
663663 ) ;
664664
665- // Special case for block end tokens:
666- // We will reverse the leading trivia, and keep removing any newlines we find, until we find something else, then we stop.
667- // This is to remove unnecessary newlines at the end of the block.
668- let mut iter = formatted_leading_trivia. iter ( ) . rev ( ) . peekable ( ) ;
669-
670- let mut formatted_leading_trivia = Vec :: new ( ) ;
671- let mut stop_removal = false ;
672- while let Some ( x) = iter. next ( ) {
673- match x. token_type ( ) {
674- TokenType :: Whitespace { ref characters } => {
675- if !stop_removal
676- && characters. contains ( '\n' )
677- && !matches ! (
678- iter. peek( ) . map( |x| x. token_kind( ) ) ,
679- Some ( TokenKind :: SingleLineComment ) | Some ( TokenKind :: MultiLineComment )
680- )
681- {
682- continue ;
683- } else {
665+ if !ctx. should_preserve_trailing_block_newline_gaps ( ) {
666+ // Special case for block end tokens:
667+ // We will reverse the leading trivia, and keep removing any newlines we find, until we find something else, then we stop.
668+ // This is to remove unnecessary newlines at the end of the block.
669+ let original_leading_trivia = std:: mem:: take ( & mut formatted_leading_trivia) ;
670+ let mut iter = original_leading_trivia. iter ( ) . cloned ( ) . rev ( ) . peekable ( ) ;
671+
672+ let mut stop_removal = false ;
673+ while let Some ( x) = iter. next ( ) {
674+ match x. token_type ( ) {
675+ TokenType :: Whitespace { ref characters } => {
676+ if !stop_removal
677+ && characters. contains ( '\n' )
678+ && !matches ! (
679+ iter. peek( ) . map( |x| x. token_kind( ) ) ,
680+ Some ( TokenKind :: SingleLineComment ) | Some ( TokenKind :: MultiLineComment )
681+ )
682+ {
683+ continue ;
684+ } else {
685+ formatted_leading_trivia. push ( x. to_owned ( ) ) ;
686+ }
687+ }
688+ _ => {
684689 formatted_leading_trivia. push ( x. to_owned ( ) ) ;
690+ stop_removal = true ; // Stop removing newlines once we have seen some sort of comment
685691 }
686692 }
687- _ => {
688- formatted_leading_trivia. push ( x. to_owned ( ) ) ;
689- stop_removal = true ; // Stop removing newlines once we have seen some sort of comment
690- }
691693 }
692- }
693694
694- // Need to reverse the vector since we reversed the iterator
695- formatted_leading_trivia. reverse ( ) ;
695+ // Need to reverse the vector since we reversed the iterator
696+ formatted_leading_trivia. reverse ( ) ;
697+ }
696698
697699 TokenReference :: new (
698700 formatted_leading_trivia,
0 commit comments