@@ -591,7 +591,7 @@ defmodule Code.Formatter do
591591 end
592592
593593 { 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 )
595595
596596 case args_docs do
597597 [ ] -> { @ empty , state }
@@ -1008,7 +1008,9 @@ defmodule Code.Formatter do
10081008 # * :required - never skip parens
10091009 #
10101010 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+
10121014 { { surround ( "(" , args_doc , ")" ) , state } , false }
10131015 end
10141016
@@ -1055,7 +1057,6 @@ defmodule Code.Formatter do
10551057 call_args_to_algebra_with_no_parens_keywords ( meta , left , right , context , extra , state )
10561058 else
10571059 next_break_fits? = next_break_fits? ( right )
1058-
10591060 force_keyword? = keyword? and force_keyword? ( right )
10601061 non_empty_eol? = left != [ ] and not next_break_fits? and Keyword . get ( meta , :eol , false )
10611062 force_unfit? = generators_count > 1 or force_keyword? or non_empty_eol?
@@ -1065,6 +1066,7 @@ defmodule Code.Formatter do
10651066 args_to_algebra_with_comments (
10661067 args ,
10671068 meta ,
1069+ skip_parens? ,
10681070 next_break_fits? ,
10691071 force_unfit? ,
10701072 state ,
@@ -1097,10 +1099,10 @@ defmodule Code.Formatter do
10971099 to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
10981100
10991101 { 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 )
11011103
11021104 { 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 )
11041106
11051107 right_doc = "," |> glue ( right_doc ) |> force_keyword ( right ) |> group ( :inherit )
11061108
@@ -1245,7 +1247,7 @@ defmodule Code.Formatter do
12451247 { args_doc , state } =
12461248 args
12471249 |> 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 )
12491251
12501252 { surround ( "<<" , args_doc , ">>" ) , state }
12511253 end
@@ -1300,19 +1302,18 @@ defmodule Code.Formatter do
13001302 force_unfit? = Keyword . get ( meta , :eol , false )
13011303
13021304 { 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 )
13041306
13051307 { surround ( "[" , args_doc , "]" ) , state }
13061308 end
13071309
13081310 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 )
13101312 force_unfit? = Keyword . get ( meta , :eol , false )
1311-
1312- { left_doc , state } = to_algebra_fun . ( left , state )
1313+ { left_doc , state } = fun . ( left , state )
13131314
13141315 { 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 )
13161317
13171318 args_doc =
13181319 left_doc
@@ -1325,32 +1326,25 @@ defmodule Code.Formatter do
13251326
13261327 defp map_to_algebra ( meta , name_doc , args , state ) do
13271328 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 )
13291330
13301331 { 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 )
13321333
13331334 name_doc = "%" |> concat ( name_doc ) |> concat ( "{" )
13341335 { surround ( name_doc , args_doc , "}" ) , state }
13351336 end
13361337
13371338 defp tuple_to_algebra ( meta , args , state ) do
13381339 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 )
13401341
13411342 next_break_fits? =
13421343 args != [ ] and next_break_fits? ( Enum . fetch! ( args , - 1 ) ) and
13431344 not Keyword . get ( meta , :eol , false )
13441345
13451346 { 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 )
13541348
13551349 doc = surround ( "{" , args_doc , "}" )
13561350
@@ -1462,7 +1456,15 @@ defmodule Code.Formatter do
14621456 defp heredoc_line ( [ "" , _ | _ ] ) , do: nest ( line ( ) , :reset )
14631457 defp heredoc_line ( _ ) , do: line ( )
14641458
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
14661468 min_line = line ( meta )
14671469 max_line = end_line ( meta )
14681470
@@ -1479,18 +1481,30 @@ defmodule Code.Formatter do
14791481 { doc , @ empty , newlines , state }
14801482 end
14811483
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 )
14841498
14851499 cond do
14861500 args_docs == [ ] ->
1487- { @ empty , new_state }
1501+ { @ empty , state }
14881502
14891503 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 }
14911505
14921506 true ->
1493- { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , new_state }
1507+ { args_docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , state }
14941508 end
14951509 end
14961510
@@ -1659,7 +1673,8 @@ defmodule Code.Formatter do
16591673
16601674 defp clause_args_to_algebra ( args , min_line , state ) do
16611675 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 )
16631678 end
16641679
16651680 # fn a, b, c when d -> e end
@@ -1681,14 +1696,14 @@ defmodule Code.Formatter do
16811696
16821697 ## Quoted helpers for comments
16831698
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
16851700 { pre_comments , state } =
16861701 get_and_update_in ( state . comments , fn comments ->
16871702 Enum . split_while ( comments , fn { line , _ , _ } -> line <= min_line end )
16881703 end )
16891704
16901705 { 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 )
16921707
16931708 { docs , comments? , update_in ( state . comments , & ( pre_comments ++ & 1 ) ) }
16941709 end
0 commit comments