Skip to content

Commit

Permalink
dont move comments when shifting @derive
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Mar 11, 2024
1 parent e3349b5 commit fb73330
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
14 changes: 9 additions & 5 deletions lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ defmodule Styler.Style.ModuleDirectives do
def run({{:@, _, [{:derive, _, _}]}, _} = zipper, ctx) do
case Style.ensure_block_parent(zipper) do
{:ok, {derive, %{l: left_siblings} = z_meta}} ->
defstruct_index =
Enum.find_index(left_siblings, fn
{struct_def, _, _} when struct_def in @defstruct -> true
_ -> false
previous_defstruct =
left_siblings
|> Stream.with_index()
|> Enum.find_value(fn
{{struct_def, meta, _}, index} when struct_def in @defstruct -> {meta[:line], index}
_ -> nil
end)

if defstruct_index do
if previous_defstruct do
{defstruct_line, defstruct_index} = previous_defstruct
derive = Style.set_line(derive, defstruct_line - 1)
left_siblings = List.insert_at(left_siblings, defstruct_index + 1, derive)
{:skip, Zipper.remove({derive, %{z_meta | l: left_siblings}}), ctx}
else
Expand Down
36 changes: 21 additions & 15 deletions test/style/module_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,27 @@ defmodule Styler.Style.ModuleDirectivesTest do
end

test "@derive movements" do
assert_style """
defmodule F do
defstruct [:a]
@derive Inspect
@derive {Foo, bar: :baz}
end
""",
"""
defmodule F do
@moduledoc false
@derive Inspect
@derive {Foo, bar: :baz}
defstruct [:a]
end
"""
assert_style(
"""
defmodule F do
defstruct [:a]
# comment for foo
def foo, do: :ok
@derive Inspect
@derive {Foo, bar: :baz}
end
""",
"""
defmodule F do
@moduledoc false
@derive Inspect
@derive {Foo, bar: :baz}
defstruct [:a]
# comment for foo
def foo, do: :ok
end
"""
)

assert_style "@derive Inspect"
end
Expand Down

0 comments on commit fb73330

Please sign in to comment.