@@ -257,6 +257,7 @@ defmodule Code.Formatter do
257
257
force_do_end_blocks: force_do_end_blocks ,
258
258
locals_without_parens: locals_without_parens ,
259
259
operand_nesting: 2 ,
260
+ skip_eol: false ,
260
261
comments: comments
261
262
}
262
263
end
@@ -528,7 +529,7 @@ defmodule Code.Formatter do
528
529
end
529
530
530
531
defp quoted_to_algebra ( { :fn , meta , [ _ | _ ] = clauses } , _context , state ) do
531
- anon_fun_to_algebra ( clauses , line ( meta ) , closing_line ( meta ) , state , eol? ( meta ) )
532
+ anon_fun_to_algebra ( clauses , line ( meta ) , closing_line ( meta ) , state , eol? ( meta , state ) )
532
533
end
533
534
534
535
defp quoted_to_algebra ( { fun , meta , args } , context , state ) when is_atom ( fun ) and is_list ( args ) do
@@ -777,7 +778,7 @@ defmodule Code.Formatter do
777
778
concat ( concat ( group ( left ) , op_string ) , group ( right ) )
778
779
779
780
true ->
780
- eol? = eol? ( meta )
781
+ eol? = eol? ( meta , state )
781
782
782
783
next_break_fits? =
783
784
op in @ next_break_fits_operators and next_break_fits? ( right_arg , state ) and not eol?
@@ -919,7 +920,7 @@ defmodule Code.Formatter do
919
920
{ docs , comments? , state } =
920
921
quoted_to_algebra_with_comments ( operands , acc , min_line , max_line , state , fun )
921
922
922
- if comments? or eol? ( meta ) do
923
+ if comments? or eol? ( meta , state ) do
923
924
{ docs |> Enum . reduce ( & line ( & 2 , & 1 ) ) |> force_unfit ( ) , state }
924
925
else
925
926
{ docs |> Enum . reduce ( & glue ( & 2 , & 1 ) ) , state }
@@ -1155,7 +1156,7 @@ defmodule Code.Formatter do
1155
1156
end
1156
1157
1157
1158
args = if keyword? , do: left ++ right , else: left ++ [ right ]
1158
- many_eol? = match? ( [ _ , _ | _ ] , args ) and eol? ( meta )
1159
+ many_eol? = match? ( [ _ , _ | _ ] , args ) and eol? ( meta , state )
1159
1160
no_generators? = no_generators? ( args )
1160
1161
to_algebra_fun = & quoted_to_algebra ( & 1 , context , & 2 )
1161
1162
@@ -1346,8 +1347,7 @@ defmodule Code.Formatter do
1346
1347
1347
1348
defp list_interpolation_to_algebra ( [ entry | entries ] , escape , state , acc , last ) do
1348
1349
{ { :. , _ , [ Kernel , :to_string ] } , _meta , [ quoted ] } = entry
1349
- { doc , state } = block_to_algebra ( quoted , @ max_line , @ min_line , state )
1350
- doc = surround ( "\# {" , doc , "}" ) |> interpolation_to_string ( )
1350
+ { doc , state } = interpolation_to_string ( quoted , state )
1351
1351
list_interpolation_to_algebra ( entries , escape , state , concat ( acc , doc ) , last )
1352
1352
end
1353
1353
@@ -1363,15 +1363,20 @@ defmodule Code.Formatter do
1363
1363
1364
1364
defp interpolation_to_algebra ( [ entry | entries ] , escape , state , acc , last ) do
1365
1365
{ :"::" , _ , [ { { :. , _ , [ Kernel , :to_string ] } , _meta , [ quoted ] } , { :binary , _ , _ } ] } = entry
1366
- { doc , state } = block_to_algebra ( quoted , @ max_line , @ min_line , state )
1367
- doc = surround ( "\# {" , doc , "}" ) |> interpolation_to_string ( )
1366
+ { doc , state } = interpolation_to_string ( quoted , state )
1368
1367
interpolation_to_algebra ( entries , escape , state , concat ( acc , doc ) , last )
1369
1368
end
1370
1369
1371
1370
defp interpolation_to_algebra ( [ ] , _escape , state , acc , last ) do
1372
1371
{ concat ( acc , last ) , state }
1373
1372
end
1374
1373
1374
+ defp interpolation_to_string ( quoted , % { skip_eol: skip_eol } = state ) do
1375
+ { doc , state } = block_to_algebra ( quoted , @ max_line , @ min_line , % { state | skip_eol: true } )
1376
+ doc = interpolation_to_string ( surround ( "\# {" , doc , "}" ) )
1377
+ { doc , % { state | skip_eol: skip_eol } }
1378
+ end
1379
+
1375
1380
defp interpolation_to_string ( doc ) do
1376
1381
[ head | tail ] =
1377
1382
doc
@@ -1421,7 +1426,7 @@ defmodule Code.Formatter do
1421
1426
1422
1427
defp bitstring_to_algebra ( meta , args , state ) do
1423
1428
last = length ( args ) - 1
1424
- join = if eol? ( meta ) , do: :line , else: :flex_break
1429
+ join = if eol? ( meta , state ) , do: :line , else: :flex_break
1425
1430
to_algebra_fun = & bitstring_segment_to_algebra ( & 1 , & 2 , last )
1426
1431
1427
1432
{ args_doc , join , state } =
@@ -1489,7 +1494,7 @@ defmodule Code.Formatter do
1489
1494
## Literals
1490
1495
1491
1496
defp list_to_algebra ( meta , args , state ) do
1492
- join = if eol? ( meta ) , do: :line , else: :break
1497
+ join = if eol? ( meta , state ) , do: :line , else: :break
1493
1498
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1494
1499
1495
1500
{ args_doc , _join , state } =
@@ -1499,7 +1504,7 @@ defmodule Code.Formatter do
1499
1504
end
1500
1505
1501
1506
defp map_to_algebra ( meta , name_doc , [ { :| , _ , [ left , right ] } ] , state ) do
1502
- join = if eol? ( meta ) , do: :line , else: :break
1507
+ join = if eol? ( meta , state ) , do: :line , else: :break
1503
1508
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1504
1509
{ left_doc , state } = fun . ( left , state )
1505
1510
@@ -1516,7 +1521,7 @@ defmodule Code.Formatter do
1516
1521
end
1517
1522
1518
1523
defp map_to_algebra ( meta , name_doc , args , state ) do
1519
- join = if eol? ( meta ) , do: :line , else: :break
1524
+ join = if eol? ( meta , state ) , do: :line , else: :break
1520
1525
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1521
1526
1522
1527
{ args_doc , _join , state } =
@@ -1527,7 +1532,7 @@ defmodule Code.Formatter do
1527
1532
end
1528
1533
1529
1534
defp tuple_to_algebra ( meta , args , join , state ) do
1530
- join = if eol? ( meta ) , do: :line , else: join
1535
+ join = if eol? ( meta , state ) , do: :line , else: join
1531
1536
fun = & quoted_to_algebra ( & 1 , :parens_arg , & 2 )
1532
1537
1533
1538
{ args_doc , join , state } =
@@ -1709,7 +1714,7 @@ defmodule Code.Formatter do
1709
1714
|> glue ( body_doc )
1710
1715
|> nest ( 2 )
1711
1716
|> glue ( "end" )
1712
- |> maybe_force_clauses ( clauses )
1717
+ |> maybe_force_clauses ( clauses , state )
1713
1718
|> group ( )
1714
1719
1715
1720
{ doc , state }
@@ -1743,7 +1748,7 @@ defmodule Code.Formatter do
1743
1748
|> glue ( body_doc )
1744
1749
|> nest ( 2 )
1745
1750
|> glue ( "end" )
1746
- |> maybe_force_clauses ( clauses )
1751
+ |> maybe_force_clauses ( clauses , state )
1747
1752
|> group ( )
1748
1753
1749
1754
{ doc , state }
@@ -1771,7 +1776,7 @@ defmodule Code.Formatter do
1771
1776
"(() -> "
1772
1777
|> concat ( nest ( body_doc , :cursor ) )
1773
1778
|> concat ( ")" )
1774
- |> maybe_force_clauses ( clauses )
1779
+ |> maybe_force_clauses ( clauses , state )
1775
1780
|> group ( )
1776
1781
1777
1782
{ doc , state }
@@ -1792,7 +1797,7 @@ defmodule Code.Formatter do
1792
1797
|> group ( )
1793
1798
|> concat ( break ( ) |> concat ( body_doc ) |> nest ( 2 ) )
1794
1799
|> wrap_in_parens ( )
1795
- |> maybe_force_clauses ( clauses )
1800
+ |> maybe_force_clauses ( clauses , state )
1796
1801
|> group ( )
1797
1802
1798
1803
{ doc , state }
@@ -1811,8 +1816,8 @@ defmodule Code.Formatter do
1811
1816
1812
1817
## Clauses
1813
1818
1814
- defp maybe_force_clauses ( doc , clauses ) do
1815
- if Enum . any? ( clauses , fn { :-> , meta , _ } -> eol? ( meta ) end ) do
1819
+ defp maybe_force_clauses ( doc , clauses , state ) do
1820
+ if Enum . any? ( clauses , fn { :-> , meta , _ } -> eol? ( meta , state ) end ) do
1816
1821
force_unfit ( doc )
1817
1822
else
1818
1823
doc
@@ -1835,7 +1840,7 @@ defmodule Code.Formatter do
1835
1840
{ doc_acc , state_acc }
1836
1841
end )
1837
1842
1838
- { clauses_doc |> maybe_force_clauses ( [ clause | clauses ] ) |> group ( ) , state }
1843
+ { clauses_doc |> maybe_force_clauses ( [ clause | clauses ] , state ) |> group ( ) , state }
1839
1844
end
1840
1845
1841
1846
defp clauses_to_algebra ( other , min_line , max_line , state ) do
@@ -2176,8 +2181,8 @@ defmodule Code.Formatter do
2176
2181
false
2177
2182
end
2178
2183
2179
- defp eol_or_comments? ( meta , % { comments: comments } ) do
2180
- eol? ( meta ) or
2184
+ defp eol_or_comments? ( meta , % { comments: comments } = state ) do
2185
+ eol? ( meta , state ) or
2181
2186
(
2182
2187
min_line = line ( meta )
2183
2188
max_line = closing_line ( meta )
@@ -2264,9 +2269,8 @@ defmodule Code.Formatter do
2264
2269
defp keyword_key? ( _ ) ,
2265
2270
do: false
2266
2271
2267
- defp eol? ( meta ) do
2268
- Keyword . get ( meta , :newlines , 0 ) > 0
2269
- end
2272
+ defp eol? ( _meta , % { skip_eol: true } ) , do: false
2273
+ defp eol? ( meta , _state ) , do: Keyword . get ( meta , :newlines , 0 ) > 0
2270
2274
2271
2275
defp meta? ( meta , key ) do
2272
2276
is_list ( meta [ key ] )
0 commit comments