@@ -712,6 +712,14 @@ impl<'a> Writer<'a> {
712712 Ok ( ( ) )
713713 }
714714
715+ fn span_has_line_comments ( & self , span : Span ) -> bool {
716+ span. source_text ( ) . is_some_and ( |source| {
717+ source
718+ . lines ( )
719+ . any ( |line| line. trim_start ( ) . starts_with ( "//" ) )
720+ } )
721+ }
722+
715723 fn attr_value_len ( & mut self , value : & AttributeValue ) -> usize {
716724 match value {
717725 AttributeValue :: IfExpr ( if_chain) => {
@@ -734,11 +742,23 @@ impl<'a> Writer<'a> {
734742 }
735743 AttributeValue :: AttrExpr ( expr) => expr
736744 . as_expr ( )
737- . map ( |expr| self . attr_expr_len ( & expr) )
745+ . map ( |expr| {
746+ if self . span_has_line_comments ( expr. span ( ) ) {
747+ 100000
748+ } else {
749+ self . attr_expr_len ( & expr)
750+ }
751+ } )
738752 . unwrap_or ( 100000 ) ,
739753 AttributeValue :: EventTokens ( closure) => closure
740754 . as_expr ( )
741- . map ( |expr| self . attr_expr_len ( & expr) )
755+ . map ( |expr| {
756+ if self . span_has_line_comments ( expr. span ( ) ) {
757+ 100000
758+ } else {
759+ self . attr_expr_len ( & expr)
760+ }
761+ } )
742762 . unwrap_or ( 100000 ) ,
743763 }
744764 }
@@ -846,6 +866,9 @@ impl<'a> Writer<'a> {
846866
847867 let pretty = self . retrieve_formatted_expr ( & expr) . to_string ( ) ;
848868 let source = src_span. source_text ( ) . unwrap_or_default ( ) ;
869+ let source_has_line_comments = source
870+ . lines ( )
871+ . any ( |line| line. trim_start ( ) . starts_with ( "//" ) ) ;
849872 let mut src_lines = source. lines ( ) . peekable ( ) ;
850873
851874 // Comments already in pretty output (from nested rsx!) - skip these from source
@@ -940,7 +963,9 @@ impl<'a> Writer<'a> {
940963 let is_call = src_trimmed. ends_with ( '(' )
941964 || src_trimmed. ends_with ( ',' )
942965 || src_trimmed. ends_with ( '{' ) ;
943- if !is_call {
966+ let is_commented_block =
967+ source_has_line_comments && src_trimmed. ends_with ( '{' ) ;
968+ if is_commented_block || !is_call {
944969 multiline = Some ( vec ! [ * src] ) ;
945970 break ;
946971 }
@@ -1008,7 +1033,17 @@ impl<'a> Writer<'a> {
10081033 }
10091034
10101035 // Write multi-line with adjusted indentation
1011- let base_indent = ml[ 0 ] . chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( ) ;
1036+ let base_indent = if source_has_line_comments && ml[ 0 ] . trim_end ( ) . ends_with ( '{' )
1037+ {
1038+ ml. iter ( )
1039+ . skip ( 1 )
1040+ . filter ( |line| !line. trim ( ) . is_empty ( ) )
1041+ . map ( |line| line. chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( ) )
1042+ . min ( )
1043+ . unwrap_or ( 0 )
1044+ } else {
1045+ ml[ 0 ] . chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( )
1046+ } ;
10121047 let target: String = line. chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . collect ( ) ;
10131048
10141049 for ( i, src_line) in ml. iter ( ) . enumerate ( ) {
0 commit comments