@@ -10,7 +10,8 @@ use rowan::TextRange;
1010use crate :: config:: {
1111 ExpandStrategy , QuoteStyle , SimpleLambdaSingleLine , SingleArgCallParens , TrailingComma ,
1212} ;
13- use crate :: ir:: { self , AlignEntry , DocIR } ;
13+ use crate :: ir;
14+ use ir:: { AlignEntry , DocIR } ;
1415
1516use super :: FormatContext ;
1617use super :: model:: { ExprSequenceLayoutPlan , RootFormatPlan , TokenSpacingExpected } ;
@@ -121,7 +122,7 @@ fn format_binary_expr(
121122 . last_token ( )
122123 . is_some_and ( |token| token. kind ( ) == LuaTokenKind :: TkFloat . into ( ) ) ;
123124
124- if crate :: ir:: ir_has_forced_line_break ( & left_docs)
125+ if ir:: ir_has_forced_line_break ( & left_docs)
125126 && should_attach_short_binary_tail ( op_token. get_op ( ) , & right, & right_docs)
126127 {
127128 let mut docs = left_docs;
@@ -205,13 +206,13 @@ fn should_attach_short_binary_tail(
205206 right : & LuaExpr ,
206207 right_docs : & [ DocIR ] ,
207208) -> bool {
208- if crate :: ir:: ir_has_forced_line_break ( right_docs) {
209+ if ir:: ir_has_forced_line_break ( right_docs) {
209210 return false ;
210211 }
211212
212213 match op {
213214 BinaryOperator :: OpAnd | BinaryOperator :: OpOr => {
214- crate :: ir:: ir_flat_width ( right_docs) <= 24
215+ ir:: ir_flat_width ( right_docs) <= 24
215216 && matches ! (
216217 right,
217218 LuaExpr :: LiteralExpr ( _)
@@ -227,7 +228,7 @@ fn should_attach_short_binary_tail(
227228 | BinaryOperator :: OpLe
228229 | BinaryOperator :: OpGt
229230 | BinaryOperator :: OpGe => {
230- crate :: ir:: ir_flat_width ( right_docs) <= 16
231+ ir:: ir_flat_width ( right_docs) <= 16
231232 && matches ! (
232233 right,
233234 LuaExpr :: LiteralExpr ( _) | LuaExpr :: NameExpr ( _) | LuaExpr :: ParenExpr ( _)
@@ -498,7 +499,7 @@ fn format_param_list_with_comments(
498499 if let Some ( comment_docs) = entry. trailing_comment {
499500 let mut suffix = trailing_comment_prefix_for_width (
500501 ctx,
501- crate :: ir:: ir_flat_width ( & line_content) ,
502+ ir:: ir_flat_width ( & line_content) ,
502503 trailing_widths[ index] ,
503504 ) ;
504505 suffix. extend ( comment_docs) ;
@@ -553,7 +554,7 @@ fn format_call_expr(ctx: &FormatContext, plan: &RootFormatPlan, expr: &LuaCallEx
553554 return format_expr_with_bridge_tail ( ctx, docs, arg_docs, bridge, false ) ;
554555 }
555556
556- if prefix_is_multiline && crate :: ir:: ir_has_forced_line_break ( & arg_docs) {
557+ if prefix_is_multiline && ir:: ir_has_forced_line_break ( & arg_docs) {
557558 docs. push ( ir:: indent ( arg_docs) ) ;
558559 } else {
559560 docs. extend ( arg_docs) ;
@@ -664,6 +665,10 @@ fn format_call_arg_list(
664665 }
665666
666667 let preserve_multiline_args = args_list. syntax ( ) . text ( ) . contains_char ( '\n' ) ;
668+ let first_arg_is_multiline_table = matches ! (
669+ args. first( ) ,
670+ Some ( LuaExpr :: TableExpr ( table) ) if table. syntax( ) . text( ) . contains_char( '\n' )
671+ ) ;
667672 let attach_first_arg = preserve_multiline_args && should_attach_first_call_arg ( & args) ;
668673 let arg_docs: Vec < Vec < DocIR > > = args
669674 . iter ( )
@@ -686,6 +691,8 @@ fn format_call_arg_list(
686691 if attach_first_arg {
687692 return format_call_args_with_attached_first_arg (
688693 ctx,
694+ plan,
695+ first_arg_is_multiline_table,
689696 layout_plan,
690697 arg_docs,
691698 token_or_kind_doc ( open. as_ref ( ) , LuaTokenKind :: TkLeftParen ) ,
@@ -721,7 +728,7 @@ fn format_call_arg_list(
721728 if arg_docs
722729 . iter ( )
723730 . skip ( 1 )
724- . any ( |docs| crate :: ir:: ir_has_forced_line_break ( docs) )
731+ . any ( |docs| ir:: ir_has_forced_line_break ( docs) )
725732 {
726733 return format_call_args_with_block_items (
727734 ctx,
@@ -837,7 +844,7 @@ fn format_call_args_with_block_items(
837844
838845 for ( index, item_docs) in arg_docs. iter ( ) . enumerate ( ) {
839846 let is_last = index + 1 == arg_docs. len ( ) ;
840- let item_is_multiline = crate :: ir:: ir_has_forced_line_break ( item_docs) ;
847+ let item_is_multiline = ir:: ir_has_forced_line_break ( item_docs) ;
841848 let mut item_with_trailing = item_docs. clone ( ) ;
842849 if !is_last {
843850 item_with_trailing. extend ( comma_token_docs ( comma) ) ;
@@ -950,6 +957,8 @@ fn format_call_arg_value_ir(
950957
951958fn format_call_args_with_attached_first_arg (
952959 ctx : & FormatContext ,
960+ plan : & RootFormatPlan ,
961+ allow_inline_tail_after_first_arg : bool ,
953962 layout_plan : ExprSequenceLayoutPlan ,
954963 arg_docs : Vec < Vec < DocIR > > ,
955964 open : DocIR ,
@@ -969,6 +978,22 @@ fn format_call_args_with_attached_first_arg(
969978
970979 let first_arg = arg_docs[ 0 ] . clone ( ) ;
971980 let tail_items = arg_docs[ 1 ..] . to_vec ( ) ;
981+ let tail_is_single_line = allow_inline_tail_after_first_arg
982+ && tail_items
983+ . iter ( )
984+ . all ( |item_docs| !ir:: ir_has_forced_line_break ( item_docs) ) ;
985+
986+ let flat = tail_is_single_line. then ( || {
987+ let mut docs = vec ! [ open. clone( ) ] ;
988+ docs. extend ( first_arg. clone ( ) ) ;
989+ docs. extend ( comma_flat_separator ( plan, comma) ) ;
990+ docs. extend ( ir:: intersperse (
991+ tail_items. clone ( ) ,
992+ comma_flat_separator ( plan, comma) ,
993+ ) ) ;
994+ docs. push ( close. clone ( ) ) ;
995+ docs
996+ } ) ;
972997
973998 let mut fill_docs = vec ! [ open. clone( ) ] ;
974999 fill_docs. extend ( first_arg. clone ( ) ) ;
@@ -997,6 +1022,7 @@ fn format_call_args_with_attached_first_arg(
9971022 choose_sequence_layout (
9981023 ctx,
9991024 SequenceLayoutCandidates {
1025+ flat,
10001026 fill : Some ( vec ! [ ir:: group( fill_docs) ] ) ,
10011027 one_per_line : Some ( vec ! [ ir:: group_break( one_per_line_docs) ] ) ,
10021028 ..Default :: default ( )
@@ -1152,7 +1178,7 @@ fn format_call_arg_list_with_comments(
11521178 if let Some ( comment_docs) = entry. trailing_comment {
11531179 let mut suffix = trailing_comment_prefix_for_width (
11541180 ctx,
1155- crate :: ir:: ir_flat_width ( & line_content) ,
1181+ ir:: ir_flat_width ( & line_content) ,
11561182 trailing_widths[ index] ,
11571183 ) ;
11581184 suffix. extend ( comment_docs) ;
@@ -1241,7 +1267,7 @@ fn format_table_expr(
12411267 . collect ( ) ;
12421268 let has_multiline_field = field_docs
12431269 . iter ( )
1244- . any ( |docs| crate :: ir:: ir_has_forced_line_break ( docs) ) ;
1270+ . any ( |docs| ir:: ir_has_forced_line_break ( docs) ) ;
12451271 let prefer_declaration_expand = should_prefer_expanded_declaration_table ( expr) ;
12461272 let ( open, close) = brace_tokens ( expr. syntax ( ) ) ;
12471273 let comma = first_direct_token ( expr. syntax ( ) , LuaTokenKind :: TkComma ) ;
@@ -1302,7 +1328,7 @@ fn format_table_expr(
13021328 let mut flat_layout = layout;
13031329 flat_layout. strategy = ExpandStrategy :: Never ;
13041330 let flat_docs = format_delimited_sequence ( ctx, flat_layout) ;
1305- if crate :: ir:: ir_flat_width ( & flat_docs) + source_line_prefix_width ( expr. syntax ( ) )
1331+ if ir:: ir_flat_width ( & flat_docs) + source_line_prefix_width ( expr. syntax ( ) )
13061332 <= ctx. config . layout . max_line_width
13071333 {
13081334 flat_docs
@@ -1347,7 +1373,7 @@ fn format_table_expr(
13471373 let mut flat_layout = layout. clone ( ) ;
13481374 flat_layout. strategy = ExpandStrategy :: Never ;
13491375 let flat_docs = format_delimited_sequence ( ctx, flat_layout) ;
1350- if crate :: ir:: ir_flat_width ( & flat_docs) + source_line_prefix_width ( expr. syntax ( ) )
1376+ if ir:: ir_flat_width ( & flat_docs) + source_line_prefix_width ( expr. syntax ( ) )
13511377 <= ctx. config . layout . max_line_width
13521378 {
13531379 return flat_docs;
@@ -1961,7 +1987,7 @@ fn table_entry_comment_alignment_width(
19611987 1
19621988 } ;
19631989
1964- crate :: ir:: ir_flat_width ( entry_docs) + separator_width
1990+ ir:: ir_flat_width ( entry_docs) + separator_width
19651991}
19661992
19671993fn aligned_table_comment_widths (
@@ -2001,7 +2027,7 @@ fn aligned_table_comment_widths(
20012027 } else {
20022028 content. push ( ir:: syntax_token ( LuaTokenKind :: TkComma ) ) ;
20032029 }
2004- Some ( crate :: ir:: ir_flat_width ( & content) )
2030+ Some ( ir:: ir_flat_width ( & content) )
20052031 } )
20062032 . max ( )
20072033 . unwrap_or ( 0 ) ;
@@ -2019,7 +2045,7 @@ fn aligned_table_comment_widths(
20192045 }
20202046 widths[ index - group_start] = Some ( trailing_comment_padding_for_config (
20212047 ctx,
2022- crate :: ir:: ir_flat_width ( & content) ,
2048+ ir:: ir_flat_width ( & content) ,
20232049 max_content_width,
20242050 ) ) ;
20252051 }
@@ -2093,7 +2119,7 @@ fn try_format_simple_inline_closure_expr(
20932119 {
20942120 return None ;
20952121 }
2096- if crate :: ir:: ir_has_forced_line_break ( & shell_plan. params ) {
2122+ if ir:: ir_has_forced_line_break ( & shell_plan. params ) {
20972123 return None ;
20982124 }
20992125
@@ -2117,7 +2143,7 @@ fn try_format_simple_inline_closure_expr(
21172143 }
21182144
21192145 let returned_docs = format_expr ( ctx, plan, & returned_expr) ;
2120- if crate :: ir:: ir_has_forced_line_break ( & returned_docs) {
2146+ if ir:: ir_has_forced_line_break ( & returned_docs) {
21212147 return None ;
21222148 }
21232149
@@ -2134,7 +2160,7 @@ fn try_format_simple_inline_closure_expr(
21342160 docs. push ( ir:: space ( ) ) ;
21352161 docs. push ( ir:: syntax_token ( LuaTokenKind :: TkEnd ) ) ;
21362162
2137- ( crate :: ir:: ir_flat_width ( & docs) + source_line_prefix_width ( expr. syntax ( ) )
2163+ ( ir:: ir_flat_width ( & docs) + source_line_prefix_width ( expr. syntax ( ) )
21382164 <= ctx. config . layout . max_line_width )
21392165 . then_some ( docs)
21402166}
@@ -2319,7 +2345,7 @@ fn try_format_chain_expr(
23192345
23202346 if segments
23212347 . iter ( )
2322- . any ( |docs| crate :: ir:: ir_has_forced_line_break ( docs) )
2348+ . any ( |docs| ir:: ir_has_forced_line_break ( docs) )
23232349 {
23242350 return Some ( flat) ;
23252351 }
@@ -2426,7 +2452,7 @@ fn format_call_suffix_ir(
24262452 && arg. syntax ( ) . text ( ) . contains_char ( '\n' )
24272453 } ) ;
24282454
2429- if crate :: ir:: ir_has_forced_line_break ( & docs)
2455+ if ir:: ir_has_forced_line_break ( & docs)
24302456 && !is_single_multiline_table_payload
24312457 && !has_multiline_block_payload
24322458 {
@@ -2450,7 +2476,7 @@ fn format_compact_call_arg_list(
24502476 let arg_docs: Vec < Vec < DocIR > > = args. iter ( ) . map ( |arg| format_expr ( ctx, plan, arg) ) . collect ( ) ;
24512477 if arg_docs
24522478 . iter ( )
2453- . any ( |docs| crate :: ir:: ir_has_forced_line_break ( docs) )
2479+ . any ( |docs| ir:: ir_has_forced_line_break ( docs) )
24542480 {
24552481 return None ;
24562482 }
@@ -2876,7 +2902,7 @@ where
28762902 let max_width = entries
28772903 . iter ( )
28782904 . filter ( |( _, has_comment) | * has_comment)
2879- . map ( |( docs, _) | crate :: ir:: ir_flat_width ( docs) )
2905+ . map ( |( docs, _) | ir:: ir_flat_width ( docs) )
28802906 . max ( ) ;
28812907
28822908 entries
0 commit comments