@@ -591,7 +591,7 @@ defmodule Code.Formatter do
591
591
end
592
592
593
593
{ args_docs , _comments? , state } =
594
- quoted_to_algebra_with_comments ( args , min_line , max_line , 2 , state , quoted_to_algebra )
594
+ quoted_to_algebra_with_comments ( args , [ ] , min_line , max_line , 2 , state , quoted_to_algebra )
595
595
596
596
case args_docs do
597
597
[ ] -> { @ empty , state }
@@ -1008,7 +1008,9 @@ defmodule Code.Formatter do
1008
1008
# * :required - never skip parens
1009
1009
#
1010
1010
defp call_args_to_algebra ( [ ] , meta , _context , _parens , _list_to_keyword? , state ) do
1011
- { args_doc , state } = args_to_algebra_with_comments ( [ ] , meta , false , false , state , & { & 1 , & 2 } )
1011
+ { args_doc , state } =
1012
+ args_to_algebra_with_comments ( [ ] , meta , false , false , false , state , & { & 1 , & 2 } )
1013
+
1012
1014
{ { surround ( "(" , args_doc , ")" ) , state } , false }
1013
1015
end
1014
1016
@@ -1055,7 +1057,6 @@ defmodule Code.Formatter do
1055
1057
call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state )
1056
1058
else
1057
1059
next_break_fits? = next_break_fits? ( right )
1058
-
1059
1060
force_keyword? = keyword? and force_keyword? ( right )
1060
1061
non_empty_eol? = left != [ ] and not next_break_fits? and Keyword . get ( meta , :eol , false )
1061
1062
force_unfit? = generators_count > 1 or force_keyword? or non_empty_eol?
@@ -1065,6 +1066,7 @@ defmodule Code.Formatter do
1065
1066
args_to_algebra_with_comments (
1066
1067
args ,
1067
1068
meta ,
1069
+ skip_parens? ,
1068
1070
next_break_fits? ,
1069
1071
force_unfit? ,
1070
1072
state ,
@@ -1097,10 +1099,10 @@ defmodule Code.Formatter do
1097
1099
to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
1098
1100
1099
1101
{ left_doc , state } =
1100
- args_to_algebra_with_comments ( left , meta , false , false , state , to_algebra_fun )
1102
+ args_to_algebra_with_comments ( left , meta , true , false , false , state , to_algebra_fun )
1101
1103
1102
1104
{ right_doc , state } =
1103
- args_to_algebra_with_comments ( right , meta , false , false , state , to_algebra_fun )
1105
+ args_to_algebra_with_comments ( right , meta , false , false , false , state , to_algebra_fun )
1104
1106
1105
1107
right_doc = "," |> glue ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
1106
1108
@@ -1245,7 +1247,7 @@ defmodule Code.Formatter do
1245
1247
{ args_doc , state } =
1246
1248
args
1247
1249
|> Enum . with_index ( )
1248
- |> args_to_algebra_with_comments ( meta , false , force_unfit? , state , to_algebra_fun )
1250
+ |> args_to_algebra_with_comments ( meta , false , false , force_unfit? , state , to_algebra_fun )
1249
1251
1250
1252
{ surround ( "<<" , args_doc , ">>" ) , state }
1251
1253
end
@@ -1300,19 +1302,18 @@ defmodule Code.Formatter do
1300
1302
force_unfit? = Keyword . get ( meta , :eol , false )
1301
1303
1302
1304
{ args_doc , state } =
1303
- args_to_algebra_with_comments ( args , meta , false , force_unfit? , state , to_algebra_fun )
1305
+ args_to_algebra_with_comments ( args , meta , false , false , force_unfit? , state , to_algebra_fun )
1304
1306
1305
1307
{ surround ( "[" , args_doc , "]" ) , state }
1306
1308
end
1307
1309
1308
1310
defp map_to_algebra ( meta , name_doc , [ { :| , _ , [ left , right ] } ] , state ) do
1309
- to_algebra_fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1311
+ fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1310
1312
force_unfit? = Keyword . get ( meta , :eol , false )
1311
-
1312
- { left_doc , state } = to_algebra_fun . ( left , state )
1313
+ { left_doc , state } = fun . ( left , state )
1313
1314
1314
1315
{ right_doc , state } =
1315
- args_to_algebra_with_comments ( right , meta , false , force_unfit? , state , to_algebra_fun )
1316
+ args_to_algebra_with_comments ( right , meta , false , false , force_unfit? , state , fun )
1316
1317
1317
1318
args_doc =
1318
1319
left_doc
@@ -1325,32 +1326,25 @@ defmodule Code.Formatter do
1325
1326
1326
1327
defp map_to_algebra ( meta , name_doc , args , state ) do
1327
1328
force_unfit? = Keyword . get ( meta , :eol , false )
1328
- to_algebra_fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1329
+ fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1329
1330
1330
1331
{ args_doc , state } =
1331
- args_to_algebra_with_comments ( args , meta , false , force_unfit? , state , to_algebra_fun )
1332
+ args_to_algebra_with_comments ( args , meta , false , false , force_unfit? , state , fun )
1332
1333
1333
1334
name_doc = "%" |> concat ( name_doc ) |> concat ( "{" )
1334
1335
{ surround ( name_doc , args_doc , "}" ) , state }
1335
1336
end
1336
1337
1337
1338
defp tuple_to_algebra ( meta , args , state ) do
1338
1339
force_unfit? = Keyword . get ( meta , :eol , false )
1339
- to_algebra_fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1340
+ fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1340
1341
1341
1342
next_break_fits? =
1342
1343
args != [ ] and next_break_fits? ( Enum . fetch! ( args , - 1 ) ) and
1343
1344
not Keyword . get ( meta , :eol , false )
1344
1345
1345
1346
{ args_doc , state } =
1346
- args_to_algebra_with_comments (
1347
- args ,
1348
- meta ,
1349
- next_break_fits? ,
1350
- force_unfit? ,
1351
- state ,
1352
- to_algebra_fun
1353
- )
1347
+ args_to_algebra_with_comments ( args , meta , false , next_break_fits? , force_unfit? , state , fun )
1354
1348
1355
1349
doc = surround ( "{" , args_doc , "}" )
1356
1350
@@ -1462,7 +1456,15 @@ defmodule Code.Formatter do
1462
1456
defp heredoc_line ( [ "" , _ | _ ] ) , do: nest ( line ( ) , :reset )
1463
1457
defp heredoc_line ( _ ) , do: line ( )
1464
1458
1465
- defp args_to_algebra_with_comments ( args , meta , next_break_fits? , force_unfit? , state , fun ) do
1459
+ defp args_to_algebra_with_comments (
1460
+ args ,
1461
+ meta ,
1462
+ skip_parens? ,
1463
+ next_break_fits? ,
1464
+ force_unfit? ,
1465
+ state ,
1466
+ fun
1467
+ ) do
1466
1468
min_line = line ( meta )
1467
1469
max_line = end_line ( meta )
1468
1470
@@ -1479,18 +1481,30 @@ defmodule Code.Formatter do
1479
1481
{ doc , @ empty , newlines , state }
1480
1482
end
1481
1483
1482
- { args_docs , comments? , new_state } =
1483
- quoted_to_algebra_with_comments ( args , min_line , max_line , 1 , state , arg_to_algebra )
1484
+ # If skipping parens, we cannot extract the comments of the first
1485
+ # argument as there is no place to move them to, so we handle it now.
1486
+ { args , acc , state } =
1487
+ case args do
1488
+ [ head | tail ] when skip_parens? ->
1489
+ { head , next_line , newlines , state } = arg_to_algebra . ( head , tail , 1 , state )
1490
+ { tail , [ { head , next_line , newlines } ] , state }
1491
+
1492
+ _ ->
1493
+ { args , [ ] , state }
1494
+ end
1495
+
1496
+ { args_docs , comments? , state } =
1497
+ quoted_to_algebra_with_comments ( args , acc , min_line , max_line , 1 , state , arg_to_algebra )
1484
1498
1485
1499
cond do
1486
1500
args_docs == [ ] ->
1487
- { @ empty , new_state }
1501
+ { @ empty , state }
1488
1502
1489
1503
force_unfit? or comments? ->
1490
- { args_docs |> Enum . reduce ( & line ( & 2 , & 1 ) ) |> force_unfit ( ) , new_state }
1504
+ { args_docs |> Enum . reduce ( & line ( & 2 , & 1 ) ) |> force_unfit ( ) , state }
1491
1505
1492
1506
true ->
1493
- { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , new_state }
1507
+ { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , state }
1494
1508
end
1495
1509
end
1496
1510
@@ -1659,7 +1673,8 @@ defmodule Code.Formatter do
1659
1673
1660
1674
defp clause_args_to_algebra ( args , min_line , state ) do
1661
1675
meta = [ line: min_line ]
1662
- args_to_algebra_with_comments ( [ args ] , meta , false , false , state , & clause_args_to_algebra / 2 )
1676
+ fun = & clause_args_to_algebra / 2
1677
+ args_to_algebra_with_comments ( [ args ] , meta , false , false , false , state , fun )
1663
1678
end
1664
1679
1665
1680
# fn a, b, c when d -> e end
@@ -1681,14 +1696,14 @@ defmodule Code.Formatter do
1681
1696
1682
1697
## Quoted helpers for comments
1683
1698
1684
- defp quoted_to_algebra_with_comments ( args , min_line , max_line , newlines , state , fun ) do
1699
+ defp quoted_to_algebra_with_comments ( args , acc , min_line , max_line , newlines , state , fun ) do
1685
1700
{ pre_comments , state } =
1686
1701
get_and_update_in ( state . comments , fn comments ->
1687
1702
Enum . split_while ( comments , fn { line , _ , _ } -> line <= min_line end )
1688
1703
end )
1689
1704
1690
1705
{ docs , comments? , state } =
1691
- each_quoted_to_algebra_with_comments ( args , [ ] , max_line , newlines , state , false , fun )
1706
+ each_quoted_to_algebra_with_comments ( args , acc , max_line , newlines , state , false , fun )
1692
1707
1693
1708
{ docs , comments? , update_in ( state . comments , & ( pre_comments ++ & 1 ) ) }
1694
1709
end
0 commit comments