@@ -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
@@ -300,7 +301,7 @@ defmodule Code.Formatter do
300
301
remote_to_algebra ( quoted , context , state )
301
302
302
303
meta [ :delimiter ] == ~s[ '''] ->
303
- { opener , quotes } = get_charlist_quotes ( true , state )
304
+ { opener , quotes } = get_charlist_quotes ( :heredoc , state )
304
305
305
306
{ doc , state } =
306
307
entries
@@ -310,7 +311,7 @@ defmodule Code.Formatter do
310
311
{ force_unfit ( doc ) , state }
311
312
312
313
true ->
313
- { opener , quotes } = get_charlist_quotes ( false , state )
314
+ { opener , quotes } = get_charlist_quotes ( { :regular , entries } , state )
314
315
list_interpolation_to_algebra ( entries , quotes , state , opener , quotes )
315
316
end
316
317
end
@@ -369,13 +370,14 @@ defmodule Code.Formatter do
369
370
defp quoted_to_algebra ( { :__block__ , meta , [ list ] } , _context , state ) when is_list ( list ) do
370
371
case meta [ :delimiter ] do
371
372
~s[ '''] ->
372
- { opener , quotes } = get_charlist_quotes ( true , state )
373
+ { opener , quotes } = get_charlist_quotes ( :heredoc , state )
373
374
string = list |> List . to_string ( ) |> escape_heredoc ( quotes )
374
375
{ opener |> concat ( string ) |> concat ( quotes ) |> force_unfit ( ) , state }
375
376
376
377
~s[ '] ->
377
- { opener , quotes } = get_charlist_quotes ( false , state )
378
- string = list |> List . to_string ( ) |> escape_string ( quotes )
378
+ string = list |> List . to_string ( )
379
+ { opener , quotes } = get_charlist_quotes ( { :regular , [ string ] } , state )
380
+ string = escape_string ( string , quotes )
379
381
{ opener |> concat ( string ) |> concat ( quotes ) , state }
380
382
381
383
_other ->
@@ -2422,19 +2424,23 @@ defmodule Code.Formatter do
2422
2424
{ left , right }
2423
2425
end
2424
2426
2425
- defp get_charlist_quotes ( _heredoc = false , state ) do
2427
+ defp get_charlist_quotes ( :heredoc , state ) do
2426
2428
if state . normalize_charlists_as_sigils do
2427
- { @ sigil_c , @ double_quote }
2429
+ { @ sigil_c_heredoc , @ double_heredoc }
2428
2430
else
2429
- { @ single_quote , @ single_quote }
2431
+ { @ single_heredoc , @ single_heredoc }
2430
2432
end
2431
2433
end
2432
2434
2433
- defp get_charlist_quotes ( _heredoc = true , state ) do
2434
- if state . normalize_charlists_as_sigils do
2435
- { @ sigil_c_heredoc , @ double_heredoc }
2436
- else
2437
- { @ single_heredoc , @ single_heredoc }
2435
+ defp get_charlist_quotes ( { :regular , chunks } , state ) do
2436
+ cond do
2437
+ ! state . normalize_charlists_as_sigils -> { @ single_quote , @ single_quote }
2438
+ Enum . any? ( chunks , & has_double_quote? / 1 ) -> { @ sigil_c_single , @ single_quote }
2439
+ true -> { @ sigil_c_double , @ double_quote }
2438
2440
end
2439
2441
end
2442
+
2443
+ defp has_double_quote? ( chunk ) do
2444
+ is_binary ( chunk ) and chunk =~ @ double_quote
2445
+ end
2440
2446
end
0 commit comments