@@ -617,9 +617,12 @@ defmodule Code.Formatter do
617
617
end
618
618
619
619
doc =
620
- with_next_break_fits ( next_break_fits? ( right_arg , state ) , right , fn right ->
621
- concat ( group ( left ) , group ( nest ( glue ( op , group ( right ) ) , 2 , :break ) ) )
622
- end )
620
+ concat (
621
+ group ( left ) ,
622
+ with_next_break_fits ( next_break_fits? ( right_arg , state ) , right , fn right ->
623
+ nest ( glue ( op , right ) , 2 , :break )
624
+ end )
625
+ )
623
626
624
627
{ doc , state }
625
628
end
@@ -818,9 +821,8 @@ defmodule Code.Formatter do
818
821
819
822
{ " " <> op_string ,
820
823
with_next_break_fits ( next_break_fits? , right , fn right ->
821
- right = nest ( concat ( break ( ) , group ( right ) ) , nesting , :break )
822
- right = if eol? , do: force_unfit ( right ) , else: right
823
- group ( right )
824
+ right = nest ( concat ( break ( ) , right ) , nesting , :break )
825
+ if eol? , do: force_unfit ( right ) , else: right
824
826
end ) }
825
827
end
826
828
@@ -1262,16 +1264,15 @@ defmodule Code.Formatter do
1262
1264
args_doc =
1263
1265
if skip_parens? do
1264
1266
left_doc
1265
- |> concat ( next_break_fits ( group ( right_doc , :inherit ) , :enabled ) )
1267
+ |> concat ( group ( right_doc , :optimistic ) )
1266
1268
|> nest ( :cursor , :break )
1267
1269
else
1268
1270
right_doc =
1269
1271
right_doc
1270
1272
|> nest ( 2 , :break )
1271
1273
|> concat ( break ( "" ) )
1272
1274
|> concat ( ")" )
1273
- |> group ( :inherit )
1274
- |> next_break_fits ( :enabled )
1275
+ |> group ( :optimistic )
1275
1276
1276
1277
concat ( nest ( left_doc , 2 , :break ) , right_doc )
1277
1278
end
@@ -1314,27 +1315,24 @@ defmodule Code.Formatter do
1314
1315
|> concat ( args_doc )
1315
1316
|> nest ( 2 )
1316
1317
|> concat ( extra )
1317
- |> group ( )
1318
1318
1319
1319
skip_parens? ->
1320
1320
" "
1321
1321
|> concat ( args_doc )
1322
1322
|> concat ( extra )
1323
- |> group ( )
1324
1323
1325
1324
true ->
1326
1325
"("
1327
1326
|> concat ( break ( "" ) )
1328
1327
|> nest ( 2 , :break )
1329
1328
|> concat ( args_doc )
1330
1329
|> concat ( extra )
1331
- |> group ( )
1332
1330
end
1333
1331
1334
1332
if next_break_fits? do
1335
- { next_break_fits ( doc , :disabled ) , state }
1333
+ { group ( doc , :pessimistic ) , state }
1336
1334
else
1337
- { doc , state }
1335
+ { group ( doc ) , state }
1338
1336
end
1339
1337
end
1340
1338
@@ -1800,10 +1798,17 @@ defmodule Code.Formatter do
1800
1798
1801
1799
doc =
1802
1800
case args do
1803
- [ _ | _ ] -> concat_to_last_group ( doc , "," )
1804
- [ ] when last_arg_mode == :force_comma -> concat_to_last_group ( doc , "," )
1805
- [ ] when last_arg_mode == :next_break_fits -> next_break_fits ( doc , :enabled )
1806
- [ ] when last_arg_mode == :none -> doc
1801
+ [ _ | _ ] ->
1802
+ concat_to_last_group ( doc , "," )
1803
+
1804
+ [ ] when last_arg_mode == :force_comma ->
1805
+ concat_to_last_group ( doc , "," )
1806
+
1807
+ [ ] when last_arg_mode == :next_break_fits ->
1808
+ doc |> ungroup_if_group ( ) |> group ( :optimistic )
1809
+
1810
+ [ ] when last_arg_mode == :none ->
1811
+ doc
1807
1812
end
1808
1813
1809
1814
{ { doc , @ empty , 1 } , state }
@@ -2321,11 +2326,14 @@ defmodule Code.Formatter do
2321
2326
defp with_next_break_fits ( condition , doc , fun ) do
2322
2327
if condition do
2323
2328
doc
2324
- |> next_break_fits ( :enabled )
2329
+ |> group ( :optimistic )
2325
2330
|> fun . ( )
2326
- |> next_break_fits ( :disabled )
2331
+ |> group ( :pessimistic )
2327
2332
else
2328
- fun . ( doc )
2333
+ doc
2334
+ |> group ( )
2335
+ |> fun . ( )
2336
+ |> group ( )
2329
2337
end
2330
2338
end
2331
2339
0 commit comments