@@ -36,23 +36,38 @@ impl<'a> Writer<'a> {
3636 Some ( self . out . buf )
3737 }
3838
39- pub fn write_rsx_call ( & mut self , body : & TemplateBody ) -> Result {
40- if body. roots . is_empty ( ) {
39+ pub fn write_rsx_call ( & mut self , body : & CallBody ) -> Result {
40+ if body. body . roots . is_empty ( ) {
4141 return Ok ( ( ) ) ;
4242 }
4343
44- if Self :: is_short_rsx_call ( & body. roots ) {
44+ if Self :: is_short_rsx_call ( & body. body . roots ) {
4545 write ! ( self . out, " " ) ?;
46- self . write_ident ( & body. roots [ 0 ] ) ?;
46+ self . write_ident ( & body. body . roots [ 0 ] ) ?;
4747 write ! ( self . out, " " ) ?;
4848 } else {
4949 self . out . new_line ( ) ?;
50- self . write_body_indented ( & body. roots ) ?
50+ self . write_body_indented ( & body. body . roots ) ?;
51+ self . write_trailing_body_comments ( body) ?;
5152 }
5253
5354 Ok ( ( ) )
5455 }
5556
57+ fn write_trailing_body_comments ( & mut self , body : & CallBody ) -> Result {
58+ if let Some ( span) = body. span {
59+ self . out . indent_level += 1 ;
60+ let comments = self . accumulate_comments ( span. span ( ) . end ( ) ) ;
61+ if !comments. is_empty ( ) {
62+ self . out . new_line ( ) ?;
63+ self . apply_comments ( comments) ?;
64+ self . out . buf . pop ( ) ; // remove the trailing newline, forcing us to end at the end of the comment
65+ }
66+ self . out . indent_level -= 1 ;
67+ }
68+ Ok ( ( ) )
69+ }
70+
5671 // Expects to be written directly into place
5772 pub fn write_ident ( & mut self , node : & BodyNode ) -> Result {
5873 match node {
@@ -301,7 +316,8 @@ impl<'a> Writer<'a> {
301316 let children_len = self
302317 . is_short_children ( children)
303318 . map_err ( |_| std:: fmt:: Error ) ?;
304- let is_small_children = children_len. is_some ( ) ;
319+ let has_trailing_comments = self . has_trailing_comments ( children, brace) ;
320+ let is_small_children = children_len. is_some ( ) && !has_trailing_comments;
305321
306322 // if we have one long attribute and a lot of children, place the attrs on top
307323 if is_short_attr_list && !is_small_children {
@@ -310,7 +326,11 @@ impl<'a> Writer<'a> {
310326
311327 // even if the attr is long, it should be put on one line
312328 // However if we have childrne we need to just spread them out for readability
313- if !is_short_attr_list && attributes. len ( ) <= 1 && spreads. is_empty ( ) {
329+ if !is_short_attr_list
330+ && attributes. len ( ) <= 1
331+ && spreads. is_empty ( )
332+ && !has_trailing_comments
333+ {
314334 if children. is_empty ( ) {
315335 opt_level = ShortOptimization :: Oneliner ;
316336 } else {
@@ -328,7 +348,11 @@ impl<'a> Writer<'a> {
328348 }
329349
330350 // If there's nothing at all, empty optimization
331- if attributes. is_empty ( ) && children. is_empty ( ) && spreads. is_empty ( ) {
351+ if attributes. is_empty ( )
352+ && children. is_empty ( )
353+ && spreads. is_empty ( )
354+ && !has_trailing_comments
355+ {
332356 opt_level = ShortOptimization :: Empty ;
333357
334358 // Write comments if they exist
@@ -932,9 +956,8 @@ impl<'a> Writer<'a> {
932956 }
933957
934958 fn final_span_of_node ( node : & BodyNode ) -> Span {
935- // Write the trailing comments if there are any
936959 // Get the ending span of the node
937- let span = match node {
960+ match node {
938961 BodyNode :: Element ( el) => el
939962 . brace
940963 . as_ref ( )
@@ -952,8 +975,7 @@ impl<'a> Writer<'a> {
952975 Some ( b) => b. span . span ( ) ,
953976 None => i. then_brace . span . span ( ) ,
954977 } ,
955- } ;
956- span
978+ }
957979 }
958980
959981 fn final_span_of_attr ( & self , attr : & Attribute ) -> Span {
@@ -969,4 +991,45 @@ impl<'a> Writer<'a> {
969991 . unwrap_or_else ( || ex. then_value . span ( ) ) ,
970992 }
971993 }
994+
995+ fn has_trailing_comments ( & self , children : & [ BodyNode ] , brace : & Brace ) -> bool {
996+ let brace_span = brace. span . span ( ) ;
997+
998+ let Some ( last_node) = children. last ( ) else {
999+ return false ;
1000+ } ;
1001+
1002+ // Check for any comments after the last node between the last brace
1003+ let final_span = Self :: final_span_of_node ( last_node) ;
1004+ let final_span = final_span. end ( ) ;
1005+ let mut line = final_span. line ;
1006+ let mut column = final_span. column ;
1007+ loop {
1008+ let Some ( src_line) = self . src . get ( line - 1 ) else {
1009+ return false ;
1010+ } ;
1011+
1012+ // the line might contain emoji or other unicode characters - this will cause issues
1013+ let Some ( mut whitespace) = src_line. get ( column..) . map ( |s| s. trim ( ) ) else {
1014+ return false ;
1015+ } ;
1016+
1017+ let offset = 0 ;
1018+ whitespace = whitespace[ offset..] . trim ( ) ;
1019+
1020+ if whitespace. starts_with ( "//" ) {
1021+ return true ;
1022+ }
1023+
1024+ if line == brace_span. end ( ) . line {
1025+ // If we reached the end of the brace span, stop
1026+ break ;
1027+ }
1028+
1029+ line += 1 ;
1030+ column = 0 ; // reset column to the start of the next line
1031+ }
1032+
1033+ false
1034+ }
9721035}
0 commit comments