@@ -55,7 +55,7 @@ impl<'arena> FormatterState<'_, 'arena> {
5555 ///
5656 /// `true` if the next substantive line is a comment line, `false` otherwise.
5757 pub ( crate ) fn is_followed_by_comment_on_next_line ( & self , span : Span ) -> bool {
58- let Some ( first_char_offset) = self . skip_spaces ( Some ( span. end . offset ) , false ) else {
58+ let Some ( first_char_offset) = self . skip_spaces ( Some ( span. end_offset ( ) ) , false ) else {
5959 return false ;
6060 } ;
6161
@@ -93,15 +93,15 @@ impl<'arena> FormatterState<'_, 'arena> {
9393 break ;
9494 }
9595
96- if comment. end <= range. start . offset {
96+ if comment. end <= range. start_offset ( ) {
9797 if flags. contains ( CommentFlags :: Leading ) && comment. matches_flags ( flags) {
9898 return true ;
9999 }
100- } else if range. end . offset < comment. start && self . is_insignificant ( range. end . offset , comment. start ) {
100+ } else if range. end_offset ( ) < comment. start && self . is_insignificant ( range. end_offset ( ) , comment. start ) {
101101 if flags. contains ( CommentFlags :: Trailing ) && comment. matches_flags ( flags) {
102102 return true ;
103103 }
104- } else if comment. end <= range. end . offset {
104+ } else if comment. end <= range. end_offset ( ) {
105105 if flags. contains ( CommentFlags :: Dangling ) && comment. matches_flags ( flags) {
106106 return true ;
107107 }
@@ -117,11 +117,11 @@ impl<'arena> FormatterState<'_, 'arena> {
117117 #[ inline]
118118 pub fn has_inner_comment ( & self , range : Span ) -> bool {
119119 for comment in self . remaining_comments ( ) {
120- if comment. start > range. end . offset {
120+ if comment. start > range. end_offset ( ) {
121121 break ;
122122 }
123123
124- if comment. start >= range. start . offset && comment. end <= range. end . offset {
124+ if comment. start >= range. start_offset ( ) && comment. end <= range. end_offset ( ) {
125125 return true ;
126126 }
127127 }
@@ -141,7 +141,7 @@ impl<'arena> FormatterState<'_, 'arena> {
141141 while let Some ( trivia) = self . all_comments . get ( self . next_comment_index ) {
142142 let comment = Comment :: from_trivia ( self . file , trivia) ;
143143
144- if comment. end <= range. start . offset {
144+ if comment. end <= range. start_offset ( ) {
145145 // Check if comment is in an ignore region - if so, preserve as-is
146146 if self . get_ignore_region_for ( comment. start ) . is_some ( ) {
147147 self . print_preserved_leading_comment ( & mut parts, comment) ;
@@ -165,7 +165,7 @@ impl<'arena> FormatterState<'_, 'arena> {
165165 while let Some ( trivia) = self . all_comments . get ( self . next_comment_index ) {
166166 let comment = Comment :: from_trivia ( self . file , trivia) ;
167167
168- if range. end . offset < comment. start && self . is_insignificant ( range. end . offset , comment. start ) {
168+ if range. end_offset ( ) < comment. start && self . is_insignificant ( range. end_offset ( ) , comment. start ) {
169169 // Check if comment is in an ignore region - if so, preserve as-is
170170 if self . get_ignore_region_for ( comment. start ) . is_some ( ) {
171171 self . print_preserved_trailing_comment ( & mut parts, comment) ;
@@ -331,7 +331,7 @@ impl<'arena> FormatterState<'_, 'arena> {
331331 for trivia in & self . all_comments [ self . next_comment_index ..] {
332332 let comment = Comment :: from_trivia ( self . file , trivia) ;
333333
334- if comment. start >= range. start . offset && comment. end <= range. end . offset {
334+ if comment. start >= range. start_offset ( ) && comment. end <= range. end_offset ( ) {
335335 must_break = must_break || !comment. is_block ;
336336 if !should_indent && self . is_next_line_empty ( trivia. span ) {
337337 parts. push ( Document :: Array (
@@ -387,7 +387,7 @@ impl<'arena> FormatterState<'_, 'arena> {
387387 for trivia in & self . all_comments [ self . next_comment_index ..] {
388388 let comment = Comment :: from_trivia ( self . file , trivia) ;
389389
390- if comment. end <= range. end . offset {
390+ if comment. end <= range. end_offset ( ) {
391391 if !indented && self . is_next_line_empty ( trivia. span ) {
392392 parts. push ( Document :: Array (
393393 vec ! [ in self . arena; self . print_comment( comment) , Document :: Line ( Line :: hard( ) ) ] ,
@@ -435,9 +435,9 @@ impl<'arena> FormatterState<'_, 'arena> {
435435 for trivia in & self . all_comments [ self . next_comment_index ..] {
436436 let comment = Comment :: from_trivia ( self . file , trivia) ;
437437
438- if comment. start >= after. end . offset
439- && comment. end <= before. start . offset
440- && self . is_insignificant ( after. end . offset , comment. start )
438+ if comment. start >= after. end_offset ( )
439+ && comment. end <= before. start_offset ( )
440+ && self . is_insignificant ( after. end_offset ( ) , comment. start )
441441 {
442442 parts. push ( self . print_comment ( comment) ) ;
443443 consumed_count += 1 ;
@@ -462,6 +462,37 @@ impl<'arena> FormatterState<'_, 'arena> {
462462 ] ) )
463463 }
464464
465+ /// Prints trailing comments that appear between two nodes, suitable for use after `{`.
466+ /// Unlike `print_dangling_comments_between_nodes`, this uses LineSuffix for inline comments
467+ /// to keep them on the same line as the preceding content.
468+ #[ must_use]
469+ pub ( crate ) fn print_trailing_comments_between_nodes (
470+ & mut self ,
471+ after : Span ,
472+ before : Span ,
473+ ) -> Option < Document < ' arena > > {
474+ let mut parts = vec ! [ in self . arena] ;
475+ let mut previous_comment: Option < Comment > = None ;
476+
477+ while let Some ( trivia) = self . all_comments . get ( self . next_comment_index ) {
478+ let comment = Comment :: from_trivia ( self . file , trivia) ;
479+
480+ let is_between = comment. start >= after. end_offset ( ) && comment. end <= before. start_offset ( ) ;
481+ let gap_is_ok =
482+ after. end_offset ( ) == comment. start || self . is_insignificant ( after. end_offset ( ) , comment. start ) ;
483+
484+ if is_between && gap_is_ok {
485+ let previous = self . print_trailing_comment ( & mut parts, comment, previous_comment) ;
486+ previous_comment = Some ( previous) ;
487+ self . next_comment_index += 1 ;
488+ } else {
489+ break ;
490+ }
491+ }
492+
493+ if parts. is_empty ( ) { None } else { Some ( Document :: Array ( parts) ) }
494+ }
495+
465496 #[ must_use]
466497 fn print_comment ( & self , comment : Comment ) -> Document < ' arena > {
467498 let content = & self . source_text [ comment. start as usize ..comment. end as usize ] ;
0 commit comments