Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatter): fix small issue where there was only one newline added #13867

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/elixir/lib/code/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ defmodule Code.Formatter do
end

defp block_args_to_algebra(args, min_line, max_line, state) do
quoted_to_algebra = fn {kind, meta, _} = arg, _args, state ->
newlines = meta[:end_of_expression][:newlines] || 1
quoted_to_algebra = fn {kind, meta, _} = arg, args, state ->
newlines = maybe_add_newlines({kind, args}, meta)
{doc, state} = quoted_to_algebra(arg, :block, state)
{{doc, block_next_line(kind), newlines}, state}
end
Expand All @@ -656,6 +656,14 @@ defmodule Code.Formatter do
defp block_next_line(:@), do: @empty
defp block_next_line(_), do: break("")

defp maybe_add_newlines({:def, [{:@, _, _} = _next | _]}, _meta) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not look at def, because we can have def, defp, defmacro, defmacrop... and projects can define defn, defnp, etc. We do have some code today that skips adding new lines if they are module attributes but perhaps the code should only avoid adding lines below, not lines up.

@newlines
end

defp maybe_add_newlines(_, meta) do
meta[:end_of_expression][:newlines] || 1
end

## Operators

defp maybe_unary_op_to_algebra(fun, meta, args, context, state) do
Expand Down
50 changes: 50 additions & 0 deletions lib/elixir/test/elixir/code_formatter/general_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,56 @@ defmodule Code.Formatter.GeneralTest do
assert_format bad, good
end

test "with def followed by module attribute" do
bad = """
defmodule Example do
@impl FooA
def a(), do: :something
@impl FooB
def b() do
:something_else
end
end
"""

good = """
defmodule Example do
@impl FooA
def a(), do: :something

@impl FooB
def b() do
:something_else
end
end
"""

assert_format bad, good
end

test "with def followed by def" do
bad = """
defmodule Example do
def a(), do: :something
def b() do
:something_else
end
end
"""

good = """
defmodule Example do
def a(), do: :something

def b() do
:something_else
end
end
"""

assert_format bad, good
end

test "with multiple defs" do
assert_same """
def foo(:one), do: 1
Expand Down