@@ -576,16 +576,22 @@ impl<'a> CommentRewrite<'a> {
576
576
cr
577
577
}
578
578
579
- fn join_block ( s : & str , sep : & str ) -> String {
579
+ fn join_block ( s : & str , sep : & str , add_sep_prefix : bool ) -> String {
580
580
let mut result = String :: with_capacity ( s. len ( ) + 128 ) ;
581
581
let mut iter = s. lines ( ) . peekable ( ) ;
582
+ let get_sep = |line : Option < & & str > | -> & str {
583
+ match line {
584
+ Some ( & "" ) => sep. trim_end ( ) ,
585
+ Some ( ..) => sep,
586
+ None => "" ,
587
+ }
588
+ } ;
589
+ if add_sep_prefix {
590
+ result. push_str ( get_sep ( iter. peek ( ) ) ) ;
591
+ }
582
592
while let Some ( line) = iter. next ( ) {
583
593
result. push_str ( line) ;
584
- result. push_str ( match iter. peek ( ) {
585
- Some ( next_line) if next_line. is_empty ( ) => sep. trim_end ( ) ,
586
- Some ( ..) => & sep,
587
- None => "" ,
588
- } ) ;
594
+ result. push_str ( get_sep ( iter. peek ( ) ) ) ;
589
595
}
590
596
result
591
597
}
@@ -594,10 +600,10 @@ impl<'a> CommentRewrite<'a> {
594
600
if !self . code_block_buffer . is_empty ( ) {
595
601
// There is a code block that is not properly enclosed by backticks.
596
602
// We will leave them untouched.
597
- self . result . push_str ( & self . comment_line_separator ) ;
598
603
self . result . push_str ( & Self :: join_block (
599
604
& trim_custom_comment_prefix ( & self . code_block_buffer ) ,
600
605
& self . comment_line_separator ,
606
+ true , /* add_sep_prefix */
601
607
) ) ;
602
608
}
603
609
@@ -615,10 +621,12 @@ impl<'a> CommentRewrite<'a> {
615
621
Some ( s) => self . result . push_str ( & Self :: join_block (
616
622
& s,
617
623
& format ! ( "{}{}" , self . comment_line_separator, ib. line_start) ,
624
+ false , /* add_sep_prefix */
618
625
) ) ,
619
626
None => self . result . push_str ( & Self :: join_block (
620
627
& ib. original_block_as_string ( ) ,
621
628
& self . comment_line_separator ,
629
+ false , /* add_sep_prefix */
622
630
) ) ,
623
631
} ;
624
632
}
@@ -658,10 +666,12 @@ impl<'a> CommentRewrite<'a> {
658
666
Some ( s) => self . result . push_str ( & Self :: join_block (
659
667
& s,
660
668
& format ! ( "{}{}" , self . comment_line_separator, ib. line_start) ,
669
+ false , /* add_sep_prefix */
661
670
) ) ,
662
671
None => self . result . push_str ( & Self :: join_block (
663
672
& ib. original_block_as_string ( ) ,
664
673
& self . comment_line_separator ,
674
+ false , /* add_sep_prefix */
665
675
) ) ,
666
676
} ;
667
677
} else if self . code_block_attr . is_some ( ) {
@@ -689,9 +699,11 @@ impl<'a> CommentRewrite<'a> {
689
699
}
690
700
} ;
691
701
if !code_block. is_empty ( ) {
692
- self . result . push_str ( & self . comment_line_separator ) ;
693
- self . result
694
- . push_str ( & Self :: join_block ( & code_block, & self . comment_line_separator ) ) ;
702
+ self . result . push_str ( & Self :: join_block (
703
+ & code_block,
704
+ & self . comment_line_separator ,
705
+ true , /* add_sep_prefix */
706
+ ) ) ;
695
707
}
696
708
self . code_block_buffer . clear ( ) ;
697
709
self . result . push_str ( & self . comment_line_separator ) ;
0 commit comments