@@ -1077,7 +1077,7 @@ defmodule Code.Formatter do
1077
1077
#
1078
1078
defp call_args_to_algebra ( [ ] , meta , _context , _parens , _list_to_keyword? , state ) do
1079
1079
{ 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 } )
1081
1081
1082
1082
{ { surround ( "(" , args_doc , ")" ) , state } , false }
1083
1083
end
@@ -1125,6 +1125,7 @@ defmodule Code.Formatter do
1125
1125
call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state )
1126
1126
else
1127
1127
next_break_fits? = next_break_fits? ( right , state )
1128
+ last_arg_mode = if next_break_fits? , do: :next_break_fits , else: :none
1128
1129
force_keyword? = keyword? and force_keyword? ( right )
1129
1130
non_empty_eol? = left != [ ] and not next_break_fits? and Keyword . get ( meta , :eol , false )
1130
1131
join = if generators_count > 1 or force_keyword? or non_empty_eol? , do: :line , else: :glue
@@ -1135,7 +1136,7 @@ defmodule Code.Formatter do
1135
1136
args ,
1136
1137
meta ,
1137
1138
skip_parens? ,
1138
- next_break_fits? ,
1139
+ last_arg_mode ,
1139
1140
join ,
1140
1141
state ,
1141
1142
& quoted_to_algebra ( & 1 , context , & 2 )
@@ -1171,12 +1172,12 @@ defmodule Code.Formatter do
1171
1172
to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
1172
1173
1173
1174
{ 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 )
1175
1176
1176
1177
{ 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 )
1178
1179
1179
- right_doc = "," |> glue ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
1180
+ right_doc = break ( ) |> concat ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
1180
1181
1181
1182
doc =
1182
1183
with_next_break_fits ( true , right_doc , fn right_doc ->
@@ -1320,7 +1321,7 @@ defmodule Code.Formatter do
1320
1321
{ args_doc , join , state } =
1321
1322
args
1322
1323
|> 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 )
1324
1325
1325
1326
if join == :flex_glue do
1326
1327
{ "<<" |> concat ( args_doc ) |> nest ( 2 ) |> concat ( ">>" ) |> group ( ) , state }
@@ -1379,7 +1380,7 @@ defmodule Code.Formatter do
1379
1380
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1380
1381
1381
1382
{ 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 )
1383
1384
1384
1385
{ surround ( "[" , args_doc , "]" ) , state }
1385
1386
end
@@ -1390,7 +1391,7 @@ defmodule Code.Formatter do
1390
1391
{ left_doc , state } = fun . ( left , state )
1391
1392
1392
1393
{ 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 )
1394
1395
1395
1396
args_doc =
1396
1397
left_doc
@@ -1406,7 +1407,7 @@ defmodule Code.Formatter do
1406
1407
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1407
1408
1408
1409
{ 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 )
1410
1411
1411
1412
name_doc = "%" |> concat ( name_doc ) |> concat ( "{" )
1412
1413
{ surround ( name_doc , args_doc , "}" ) , state }
@@ -1417,7 +1418,7 @@ defmodule Code.Formatter do
1417
1418
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1418
1419
1419
1420
{ 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 )
1421
1422
1422
1423
if join == :flex_glue do
1423
1424
{ "{" |> concat ( args_doc ) |> nest ( 1 ) |> concat ( "}" ) |> group ( ) , state }
@@ -1527,18 +1528,19 @@ defmodule Code.Formatter do
1527
1528
defp heredoc_line ( [ "" , _ | _ ] ) , do: nest ( line ( ) , :reset )
1528
1529
defp heredoc_line ( _ ) , do: line ( )
1529
1530
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
1531
1532
min_line = line ( meta )
1532
1533
max_line = end_line ( meta )
1533
1534
1534
1535
arg_to_algebra = fn arg , args , newlines , state ->
1535
1536
{ doc , state } = fun . ( arg , state )
1536
1537
1537
1538
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
1542
1544
end
1543
1545
1544
1546
{ doc , @ empty , newlines , state }
@@ -1753,7 +1755,7 @@ defmodule Code.Formatter do
1753
1755
fun = & clause_args_to_algebra / 2
1754
1756
1755
1757
{ 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 )
1757
1759
1758
1760
{ args_docs , state }
1759
1761
end
@@ -2122,6 +2124,20 @@ defmodule Code.Formatter do
2122
2124
2123
2125
## Algebra helpers
2124
2126
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
+
2125
2141
defp ungroup_if_group ( { :doc_group , group , _mode } ) , do: group
2126
2142
defp ungroup_if_group ( other ) , do: other
2127
2143
0 commit comments