@@ -420,11 +420,11 @@ defmodule Code.Formatter do
420
420
# {}
421
421
# {1, 2}
422
422
defp quoted_to_algebra ( { :{} , meta , args } , _context , state ) do
423
- tuple_to_algebra ( meta , args , :flex_glue , state )
423
+ tuple_to_algebra ( meta , args , :flex_break , state )
424
424
end
425
425
426
426
defp quoted_to_algebra ( { :__block__ , meta , [ { left , right } ] } , _context , state ) do
427
- tuple_to_algebra ( meta , [ left , right ] , :flex_glue , state )
427
+ tuple_to_algebra ( meta , [ left , right ] , :flex_break , state )
428
428
end
429
429
430
430
defp quoted_to_algebra ( { :__block__ , meta , [ list ] } , _context , state ) when is_list ( list ) do
@@ -960,7 +960,7 @@ defmodule Code.Formatter do
960
960
# expression.{arguments}
961
961
defp remote_to_algebra ( { { :. , _ , [ target , :{} ] } , meta , args } , _context , state ) do
962
962
{ target_doc , state } = remote_target_to_algebra ( target , state )
963
- { call_doc , state } = tuple_to_algebra ( meta , args , :glue , state )
963
+ { call_doc , state } = tuple_to_algebra ( meta , args , :break , state )
964
964
{ concat ( concat ( target_doc , "." ) , call_doc ) , state }
965
965
end
966
966
@@ -1087,7 +1087,7 @@ defmodule Code.Formatter do
1087
1087
#
1088
1088
defp call_args_to_algebra ( [ ] , meta , _context , _parens , _list_to_keyword? , state ) do
1089
1089
{ args_doc , _join , state } =
1090
- args_to_algebra_with_comments ( [ ] , meta , false , :none , :glue , state , & { & 1 , & 2 } )
1090
+ args_to_algebra_with_comments ( [ ] , meta , false , :none , :break , state , & { & 1 , & 2 } )
1091
1091
1092
1092
{ { surround ( "(" , args_doc , ")" ) , state } , false }
1093
1093
end
@@ -1121,7 +1121,6 @@ defmodule Code.Formatter do
1121
1121
1122
1122
defp call_args_to_algebra_no_blocks ( meta , args , skip_parens? , list_to_keyword? , extra , state ) do
1123
1123
{ left , right } = split_last ( args )
1124
- generators_count = count_generators ( args )
1125
1124
{ keyword? , right } = last_arg_to_keyword ( right , list_to_keyword? )
1126
1125
1127
1126
context =
@@ -1131,15 +1130,14 @@ defmodule Code.Formatter do
1131
1130
if skip_parens? , do: :no_parens_arg , else: :parens_arg
1132
1131
end
1133
1132
1134
- if left != [ ] and keyword? and skip_parens? and generators_count == 0 do
1133
+ if left != [ ] and keyword? and skip_parens? and no_generators? ( args ) do
1135
1134
call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state )
1136
1135
else
1137
- force_keyword? = keyword? and force_keyword? ( right )
1138
- non_empty_eol? = left != [ ] and Keyword . get ( meta , :eol , false )
1139
- join = if generators_count > 1 or force_keyword? or non_empty_eol? , do: :line , else: :glue
1140
1136
args = if keyword? , do: left ++ right , else: left ++ [ right ]
1137
+ many_eol? = match? ( [ _ , _ | _ ] , args ) and Keyword . get ( meta , :eol , false )
1138
+ join = if force_args? ( args ) or many_eol? , do: :line , else: :break
1141
1139
1142
- next_break_fits? = join == :glue and next_break_fits? ( right , state )
1140
+ next_break_fits? = join == :break and next_break_fits? ( right , state )
1143
1141
last_arg_mode = if next_break_fits? , do: :next_break_fits , else: :none
1144
1142
1145
1143
{ args_doc , _join , state } =
@@ -1181,14 +1179,17 @@ defmodule Code.Formatter do
1181
1179
1182
1180
defp call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state ) do
1183
1181
to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
1182
+ join = if force_args? ( left ) , do: :line , else: :break
1184
1183
1185
1184
{ left_doc , _join , state } =
1186
- args_to_algebra_with_comments ( left , meta , true , :force_comma , :glue , state , to_algebra_fun )
1185
+ args_to_algebra_with_comments ( left , meta , true , :force_comma , join , state , to_algebra_fun )
1186
+
1187
+ join = if force_args? ( right ) or force_args? ( left ++ right ) , do: :line , else: :break
1187
1188
1188
1189
{ right_doc , _join , state } =
1189
- args_to_algebra_with_comments ( right , meta , false , :none , :glue , state , to_algebra_fun )
1190
+ args_to_algebra_with_comments ( right , meta , false , :none , join , state , to_algebra_fun )
1190
1191
1191
- right_doc = break ( ) |> concat ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
1192
+ right_doc = apply ( Inspect.Algebra , join , [ ] ) |> concat ( right_doc ) |> group ( :inherit )
1192
1193
1193
1194
doc =
1194
1195
with_next_break_fits ( true , right_doc , fn right_doc ->
@@ -1213,8 +1214,8 @@ defmodule Code.Formatter do
1213
1214
end )
1214
1215
end
1215
1216
1216
- defp count_generators ( args ) do
1217
- Enum . count ( args , & match? ( { :<- , _ , [ _ , _ ] } , & 1 ) )
1217
+ defp no_generators? ( args ) do
1218
+ not Enum . any? ( args , & match? ( { :<- , _ , [ _ , _ ] } , & 1 ) )
1218
1219
end
1219
1220
1220
1221
defp do_end_blocks ( [ { { :__block__ , meta , [ :do ] } , _ } | _ ] = blocks ) do
@@ -1326,15 +1327,15 @@ defmodule Code.Formatter do
1326
1327
1327
1328
defp bitstring_to_algebra ( meta , args , state ) do
1328
1329
last = length ( args ) - 1
1329
- join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :flex_glue
1330
+ join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :flex_break
1330
1331
to_algebra_fun = & bitstring_segment_to_algebra ( & 1 , & 2 , last )
1331
1332
1332
1333
{ args_doc , join , state } =
1333
1334
args
1334
1335
|> Enum . with_index ( )
1335
1336
|> args_to_algebra_with_comments ( meta , false , :none , join , state , to_algebra_fun )
1336
1337
1337
- if join == :flex_glue do
1338
+ if join == :flex_break do
1338
1339
{ "<<" |> concat ( args_doc ) |> nest ( 2 ) |> concat ( ">>" ) |> group ( ) , state }
1339
1340
else
1340
1341
{ surround ( "<<" , args_doc , ">>" ) , state }
@@ -1387,7 +1388,7 @@ defmodule Code.Formatter do
1387
1388
## Literals
1388
1389
1389
1390
defp list_to_algebra ( meta , args , state ) do
1390
- join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :glue
1391
+ join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :break
1391
1392
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1392
1393
1393
1394
{ args_doc , _join , state } =
@@ -1397,7 +1398,7 @@ defmodule Code.Formatter do
1397
1398
end
1398
1399
1399
1400
defp map_to_algebra ( meta , name_doc , [ { :| , _ , [ left , right ] } ] , state ) do
1400
- join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :glue
1401
+ join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :break
1401
1402
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1402
1403
{ left_doc , state } = fun . ( left , state )
1403
1404
@@ -1414,7 +1415,7 @@ defmodule Code.Formatter do
1414
1415
end
1415
1416
1416
1417
defp map_to_algebra ( meta , name_doc , args , state ) do
1417
- join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :glue
1418
+ join = if Keyword . get ( meta , :eol , false ) , do: :line , else: :break
1418
1419
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1419
1420
1420
1421
{ args_doc , _join , state } =
@@ -1431,7 +1432,7 @@ defmodule Code.Formatter do
1431
1432
{ args_doc , join , state } =
1432
1433
args_to_algebra_with_comments ( args , meta , false , :none , join , state , fun )
1433
1434
1434
- if join == :flex_glue do
1435
+ if join == :flex_break do
1435
1436
{ "{" |> concat ( args_doc ) |> nest ( 1 ) |> concat ( "}" ) |> group ( ) , state }
1436
1437
else
1437
1438
{ surround ( "{" , args_doc , "}" ) , state }
@@ -1579,11 +1580,11 @@ defmodule Code.Formatter do
1579
1580
join == :line or comments? ->
1580
1581
{ args_docs |> Enum . reduce ( & line ( & 2 , & 1 ) ) |> force_unfit ( ) , :line , state }
1581
1582
1582
- join == :glue ->
1583
- { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , :glue , state }
1583
+ join == :break ->
1584
+ { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , :break , state }
1584
1585
1585
- join == :flex_glue ->
1586
- { args_docs |> Enum . reduce ( & flex_glue ( & 2 , & 1 ) ) , :flex_glue , state }
1586
+ join == :flex_break ->
1587
+ { args_docs |> Enum . reduce ( & flex_glue ( & 2 , & 1 ) ) , :flex_break , state }
1587
1588
end
1588
1589
end
1589
1590
@@ -1766,7 +1767,7 @@ defmodule Code.Formatter do
1766
1767
fun = & clause_args_to_algebra / 2
1767
1768
1768
1769
{ args_docs , _join , state } =
1769
- args_to_algebra_with_comments ( [ args ] , meta , false , :none , :glue , state , fun )
1770
+ args_to_algebra_with_comments ( [ args ] , meta , false , :none , :break , state , fun )
1770
1771
1771
1772
{ args_docs , state }
1772
1773
end
@@ -2083,26 +2084,32 @@ defmodule Code.Formatter do
2083
2084
{ false , arg }
2084
2085
end
2085
2086
2086
- defp force_keyword? ( keyword ) do
2087
- match? ( [ { _ , _ } , _ | _ ] , keyword ) and force_keyword? ( keyword , MapSet . new ( ) )
2087
+ defp force_args? ( args ) do
2088
+ match? ( [ _ , _ | _ ] , args ) and force_args? ( args , MapSet . new ( ) )
2089
+ end
2090
+
2091
+ defp force_args? ( [ [ arg | _ ] | args ] , lines ) do
2092
+ force_args? ( [ arg | args ] , lines )
2088
2093
end
2089
2094
2090
- defp force_keyword? ( [ { { _ , meta , _ } , _ } | keyword ] , lines ) do
2091
- line = line ( meta )
2095
+ defp force_args? ( [ arg | args ] , lines ) do
2096
+ line =
2097
+ case arg do
2098
+ { { _ , meta , _ } , _ } -> line ( meta )
2099
+ { _ , meta , _ } -> line ( meta )
2100
+ end
2092
2101
2093
2102
if MapSet . member? ( lines , line ) do
2094
2103
false
2095
2104
else
2096
- force_keyword? ( keyword , MapSet . put ( lines , line ) )
2105
+ force_args? ( args , MapSet . put ( lines , line ) )
2097
2106
end
2098
2107
end
2099
2108
2100
- defp force_keyword? ( [ ] , _lines ) do
2101
- true
2102
- end
2109
+ defp force_args? ( [ ] , _lines ) , do: true
2103
2110
2104
2111
defp force_keyword ( doc , arg ) do
2105
- if force_keyword ?( arg ) , do: force_unfit ( doc ) , else: doc
2112
+ if force_args ?( arg ) , do: force_unfit ( doc ) , else: doc
2106
2113
end
2107
2114
2108
2115
defp keyword? ( [ { key , _ } | list ] ) do
0 commit comments