@@ -1077,7 +1077,7 @@ defmodule Code.Formatter do
10771077 #
10781078 defp call_args_to_algebra ( [ ] , meta , _context , _parens , _list_to_keyword? , state ) do
10791079 { args_doc , _join , state } =
1080- args_to_algebra_with_comments ( [ ] , meta , false , false , :glue , state , & { & 1 , & 2 } )
1080+ args_to_algebra_with_comments ( [ ] , meta , false , :none , :glue , state , & { & 1 , & 2 } )
10811081
10821082 { { surround ( "(" , args_doc , ")" ) , state } , false }
10831083 end
@@ -1125,6 +1125,7 @@ defmodule Code.Formatter do
11251125 call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state )
11261126 else
11271127 next_break_fits? = next_break_fits? ( right , state )
1128+ last_arg_mode = if next_break_fits? , do: :next_break_fits , else: :none
11281129 force_keyword? = keyword? and force_keyword? ( right )
11291130 non_empty_eol? = left != [ ] and not next_break_fits? and Keyword . get ( meta , :eol , false )
11301131 join = if generators_count > 1 or force_keyword? or non_empty_eol? , do: :line , else: :glue
@@ -1135,7 +1136,7 @@ defmodule Code.Formatter do
11351136 args ,
11361137 meta ,
11371138 skip_parens? ,
1138- next_break_fits? ,
1139+ last_arg_mode ,
11391140 join ,
11401141 state ,
11411142 & quoted_to_algebra ( & 1 , context , & 2 )
@@ -1171,12 +1172,12 @@ defmodule Code.Formatter do
11711172 to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
11721173
11731174 { left_doc , _join , state } =
1174- args_to_algebra_with_comments ( left , meta , true , false , :glue , state , to_algebra_fun )
1175+ args_to_algebra_with_comments ( left , meta , true , :force_comma , :glue , state , to_algebra_fun )
11751176
11761177 { right_doc , _join , state } =
1177- args_to_algebra_with_comments ( right , meta , false , false , :glue , state , to_algebra_fun )
1178+ args_to_algebra_with_comments ( right , meta , false , :none , :glue , state , to_algebra_fun )
11781179
1179- right_doc = "," |> glue ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
1180+ right_doc = break ( ) |> concat ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
11801181
11811182 doc =
11821183 with_next_break_fits ( true , right_doc , fn right_doc ->
@@ -1320,7 +1321,7 @@ defmodule Code.Formatter do
13201321 { args_doc , join , state } =
13211322 args
13221323 |> Enum . with_index ( )
1323- |> args_to_algebra_with_comments ( meta , false , false , join , state , to_algebra_fun )
1324+ |> args_to_algebra_with_comments ( meta , false , :none , join , state , to_algebra_fun )
13241325
13251326 if join == :flex_glue do
13261327 { "<<" |> concat ( args_doc ) |> nest ( 2 ) |> concat ( ">>" ) |> group ( ) , state }
@@ -1379,7 +1380,7 @@ defmodule Code.Formatter do
13791380 fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
13801381
13811382 { args_doc , _join , state } =
1382- args_to_algebra_with_comments ( args , meta , false , false , join , state , fun )
1383+ args_to_algebra_with_comments ( args , meta , false , :none , join , state , fun )
13831384
13841385 { surround ( "[" , args_doc , "]" ) , state }
13851386 end
@@ -1390,7 +1391,7 @@ defmodule Code.Formatter do
13901391 { left_doc , state } = fun . ( left , state )
13911392
13921393 { right_doc , _join , state } =
1393- args_to_algebra_with_comments ( right , meta , false , false , join , state , fun )
1394+ args_to_algebra_with_comments ( right , meta , false , :none , join , state , fun )
13941395
13951396 args_doc =
13961397 left_doc
@@ -1406,7 +1407,7 @@ defmodule Code.Formatter do
14061407 fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
14071408
14081409 { args_doc , _join , state } =
1409- args_to_algebra_with_comments ( args , meta , false , false , join , state , fun )
1410+ args_to_algebra_with_comments ( args , meta , false , :none , join , state , fun )
14101411
14111412 name_doc = "%" |> concat ( name_doc ) |> concat ( "{" )
14121413 { surround ( name_doc , args_doc , "}" ) , state }
@@ -1417,7 +1418,7 @@ defmodule Code.Formatter do
14171418 fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
14181419
14191420 { args_doc , join , state } =
1420- args_to_algebra_with_comments ( args , meta , false , false , join , state , fun )
1421+ args_to_algebra_with_comments ( args , meta , false , :none , join , state , fun )
14211422
14221423 if join == :flex_glue do
14231424 { "{" |> concat ( args_doc ) |> nest ( 1 ) |> concat ( "}" ) |> group ( ) , state }
@@ -1527,18 +1528,19 @@ defmodule Code.Formatter do
15271528 defp heredoc_line ( [ "" , _ | _ ] ) , do: nest ( line ( ) , :reset )
15281529 defp heredoc_line ( _ ) , do: line ( )
15291530
1530- defp args_to_algebra_with_comments ( args , meta , skip_parens? , next_break_fits? , join , state , fun ) do
1531+ defp args_to_algebra_with_comments ( args , meta , skip_parens? , last_arg_mode , join , state , fun ) do
15311532 min_line = line ( meta )
15321533 max_line = end_line ( meta )
15331534
15341535 arg_to_algebra = fn arg , args , newlines , state ->
15351536 { doc , state } = fun . ( arg , state )
15361537
15371538 doc =
1538- cond do
1539- args != [ ] -> concat ( doc , "," )
1540- next_break_fits? -> next_break_fits ( doc , :enabled )
1541- true -> doc
1539+ case args do
1540+ [ _ | _ ] -> concat_to_last_group ( doc , "," )
1541+ [ ] when last_arg_mode == :force_comma -> concat_to_last_group ( doc , "," )
1542+ [ ] when last_arg_mode == :next_break_fits -> next_break_fits ( doc , :enabled )
1543+ [ ] when last_arg_mode == :none -> doc
15421544 end
15431545
15441546 { doc , @ empty , newlines , state }
@@ -1753,7 +1755,7 @@ defmodule Code.Formatter do
17531755 fun = & clause_args_to_algebra / 2
17541756
17551757 { args_docs , _join , state } =
1756- args_to_algebra_with_comments ( [ args ] , meta , false , false , :glue , state , fun )
1758+ args_to_algebra_with_comments ( [ args ] , meta , false , :none , :glue , state , fun )
17571759
17581760 { args_docs , state }
17591761 end
@@ -2122,6 +2124,20 @@ defmodule Code.Formatter do
21222124
21232125 ## Algebra helpers
21242126
2127+ # Relying on the inner document is brittle and error prone.
2128+ # It would be best if we had a mechanism to apply this.
2129+ defp concat_to_last_group ( { :doc_cons , left , right } , concat ) do
2130+ { :doc_cons , left , concat_to_last_group ( right , concat ) }
2131+ end
2132+
2133+ defp concat_to_last_group ( { :doc_group , group , mode } , concat ) do
2134+ { :doc_group , { :doc_cons , group , concat } , mode }
2135+ end
2136+
2137+ defp concat_to_last_group ( other , concat ) do
2138+ { :doc_cons , other , concat }
2139+ end
2140+
21252141 defp ungroup_if_group ( { :doc_group , group , _mode } ) , do: group
21262142 defp ungroup_if_group ( other ) , do: other
21272143
0 commit comments