Skip to content

Commit ca49a53

Browse files
author
José Valim
committed
Consistently preserve the user choice in regards to parens, closes #7286
1 parent 1ff0e1a commit ca49a53

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,11 @@ defmodule Code.Formatter do
998998
# function(arguments)
999999
defp local_to_algebra(fun, meta, args, context, state) when is_atom(fun) do
10001000
skip_parens =
1001-
if skip_parens?(fun, meta, args, state), do: :skip_unless_many_args, else: :skip_if_do_end
1001+
cond do
1002+
not Keyword.get(meta, :no_parens, false) -> :required
1003+
local_without_parens?(fun, args, state) -> :skip_unless_many_args
1004+
true -> :skip_if_do_end
1005+
end
10021006

10031007
{{call_doc, state}, wrap_in_parens?} =
10041008
call_args_to_algebra(args, meta, context, skip_parens, true, state)
@@ -1136,10 +1140,10 @@ defmodule Code.Formatter do
11361140
{doc, state}
11371141
end
11381142

1139-
defp skip_parens?(fun, meta, args, %{locals_without_parens: locals_without_parens}) do
1143+
defp local_without_parens?(fun, args, %{locals_without_parens: locals_without_parens}) do
11401144
length = length(args)
11411145

1142-
length > 0 and Keyword.get(meta, :no_parens, false) and
1146+
length > 0 and
11431147
Enum.any?(locals_without_parens, fn {key, val} ->
11441148
key == fun and (val == :* or val == length)
11451149
end)

lib/elixir/test/elixir/code_formatter/calls_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,32 @@ defmodule Code.Formatter.CallsTest do
434434
)
435435
"""
436436

437+
assert_same """
438+
call(
439+
:hello,
440+
:foo,
441+
:bar
442+
) do
443+
1 + 2
444+
end
445+
"""
446+
437447
# Doesn't preserve this because only the beginning has a newline
438448
assert_format "call(\nfoo, bar, baz)", "call(foo, bar, baz)"
439449

450+
# Doesn't preserve because there are no args
451+
bad = """
452+
call() do
453+
1 + 2
454+
end
455+
"""
456+
457+
assert_format bad, """
458+
call do
459+
1 + 2
460+
end
461+
"""
462+
440463
# Doesn't preserve because we have a single argument with next break fits
441464
bad = """
442465
call(

0 commit comments

Comments
 (0)