@@ -6,7 +6,8 @@ defmodule Code.Formatter do
6
6
@ double_heredoc "\" \" \" "
7
7
@ single_quote "'"
8
8
@ single_heredoc "'''"
9
- @ sigil_c "~c\" "
9
+ @ sigil_c_double "~c\" "
10
+ @ sigil_c_single "~c'"
10
11
@ sigil_c_heredoc "~c\" \" \" "
11
12
@ newlines 2
12
13
@ min_line 0
@@ -299,7 +300,7 @@ defmodule Code.Formatter do
299
300
remote_to_algebra ( quoted , context , state )
300
301
301
302
meta [ :delimiter ] == ~s[ '''] ->
302
- { opener , quotes } = get_charlist_quotes ( true , state )
303
+ { opener , quotes } = get_charlist_quotes ( :heredoc , state )
303
304
304
305
{ doc , state } =
305
306
entries
@@ -309,7 +310,7 @@ defmodule Code.Formatter do
309
310
{ force_unfit ( doc ) , state }
310
311
311
312
true ->
312
- { opener , quotes } = get_charlist_quotes ( false , state )
313
+ { opener , quotes } = get_charlist_quotes ( { :regular , entries } , state )
313
314
list_interpolation_to_algebra ( entries , quotes , state , opener , quotes )
314
315
end
315
316
end
@@ -368,13 +369,14 @@ defmodule Code.Formatter do
368
369
defp quoted_to_algebra ( { :__block__ , meta , [ list ] } , _context , state ) when is_list ( list ) do
369
370
case meta [ :delimiter ] do
370
371
~s[ '''] ->
371
- { opener , quotes } = get_charlist_quotes ( true , state )
372
+ { opener , quotes } = get_charlist_quotes ( :heredoc , state )
372
373
string = list |> List . to_string ( ) |> escape_heredoc ( quotes )
373
374
{ opener |> concat ( string ) |> concat ( quotes ) |> force_unfit ( ) , state }
374
375
375
376
~s[ '] ->
376
- { opener , quotes } = get_charlist_quotes ( false , state )
377
- string = list |> List . to_string ( ) |> escape_string ( quotes )
377
+ string = list |> List . to_string ( )
378
+ { opener , quotes } = get_charlist_quotes ( { :regular , [ string ] } , state )
379
+ string = escape_string ( string , quotes )
378
380
{ opener |> concat ( string ) |> concat ( quotes ) , state }
379
381
380
382
_other ->
@@ -2410,19 +2412,23 @@ defmodule Code.Formatter do
2410
2412
{ left , right }
2411
2413
end
2412
2414
2413
- defp get_charlist_quotes ( _heredoc = false , state ) do
2415
+ defp get_charlist_quotes ( :heredoc , state ) do
2414
2416
if state . normalize_charlists_as_sigils do
2415
- { @ sigil_c , @ double_quote }
2417
+ { @ sigil_c_heredoc , @ double_heredoc }
2416
2418
else
2417
- { @ single_quote , @ single_quote }
2419
+ { @ single_heredoc , @ single_heredoc }
2418
2420
end
2419
2421
end
2420
2422
2421
- defp get_charlist_quotes ( _heredoc = true , state ) do
2422
- if state . normalize_charlists_as_sigils do
2423
- { @ sigil_c_heredoc , @ double_heredoc }
2424
- else
2425
- { @ single_heredoc , @ single_heredoc }
2423
+ defp get_charlist_quotes ( { :regular , chunks } , state ) do
2424
+ cond do
2425
+ ! state . normalize_charlists_as_sigils -> { @ single_quote , @ single_quote }
2426
+ Enum . any? ( chunks , & has_double_quote? / 1 ) -> { @ sigil_c_single , @ single_quote }
2427
+ true -> { @ sigil_c_double , @ double_quote }
2426
2428
end
2427
2429
end
2430
+
2431
+ defp has_double_quote? ( chunk ) do
2432
+ is_binary ( chunk ) and chunk =~ @ double_quote
2433
+ end
2428
2434
end
0 commit comments