Skip to content

Commit

Permalink
scaffolding is getting there
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 2, 2024
1 parent 8a48d49 commit 2e26d6e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ defmodule Styler.Style do
|> Zipper.root()
end

# useful for comparing AST line numbers interfering
def without_meta(ast), do: update_all_meta(ast, fn _ -> nil end)

@doc """
Returns the current node (wrapped in a `__block__` if necessary) if it's a valid place to insert additional nodes
"""
Expand Down
5 changes: 1 addition & 4 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ defmodule Styler.Style.Blocks do
defp left_arrow?({:<-, _, _}), do: true
defp left_arrow?(_), do: false

defp nodes_equivalent?(a, b) do
# compare nodes without metadata
Style.update_all_meta(a, fn _ -> nil end) == Style.update_all_meta(b, fn _ -> nil end)
end
defp nodes_equivalent?(a, b), do: Style.without_meta(a) == Style.without_meta(b)

defp if_ast(zipper, head, {_, _, _} = do_body, {_, _, _} = else_body, ctx) do
do_ = {{:__block__, [line: nil], [:do]}, do_body}
Expand Down
23 changes: 20 additions & 3 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,30 @@ defmodule Styler.Style.Configs do
Delete the duplicative/erroneous stanza and life will be good.
"""

alias Styler.Style

def run({{:import, _, [{:__aliases__, _, [:Config]}]}, _} = zipper, %{config?: true} = ctx) do
{:skip, zipper, Map.put(ctx, :mix_config?, true)}
end

def run({{:config, _, args} = config, zm}, %{mix_config?: true} = ctx) when is_list(args) do
{configs, others} = Enum.split_while(zm.r, &match?({:config, _, [_ | _]}, &1))
[config | configs] = Enum.sort_by([config | configs], &Styler.Style.update_all_meta(&1, fn _ -> nil end), :desc)
def run({{:config, _, [_, _ | _]} = config, zm}, %{mix_config?: true} = ctx) do
{configs, others} = Enum.split_while(zm.r, &match?({:config, _, [_, _| _]}, &1))

[config | configs] =
[config | configs]
|> Enum.group_by(fn
{:config, _, [{:__block__, _, [app]} | _]} -> app
{:config, _, [arg | _]} -> Style.without_meta(arg)
end)
|> Enum.sort(:desc)
|> Enum.flat_map(fn {_app, configs} ->
configs
|> Enum.sort_by(&Style.without_meta/1, :asc)
|> Style.reset_newlines()
|> Enum.reverse()
end)
|> Style.fix_line_numbers(List.first(others))

zm = %{zm | l: configs ++ zm.l, r: others}
{:skip, {config, zm}, ctx}
end
Expand Down
28 changes: 23 additions & 5 deletions test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Styler.Style.ConfigsTest do
end
end

test "orders configs!" do
test "orders configs stanzas" do
assert_style """
config :z, :x
config :a, :b
Expand All @@ -35,22 +35,40 @@ defmodule Styler.Style.ConfigsTest do
assert_style(
"""
import Config
config :z, :x, :woof
config :a, :b, :c
config :a, :c, :d
import_config "my_config"
config :z, a: :meow
config :a, :b
config :a, :b, :x
""",
"""
import Config
config :a, :b
config :a, :b, :c
config :a, :c, :d
config :z, :x, :woof
import_config "my_config"
config :a, :b, :x
config :z, a: :meow
"""
)
end

test "ignores config when import Config wasn't called" do
test "ignores things that look like config/2,3" do
assert_style """
import Config
config :a, :b
config(a)
config :c, :d
"""
end

test "moves assignments to before configs"

Check failure on line 74 in test/style/configs_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.14.2/OTP25.1.2

test moves assignments to before configs (Styler.Style.ConfigsTest)

Check failure on line 74 in test/style/configs_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.15.7/OTP25.1.2

test moves assignments to before configs (Styler.Style.ConfigsTest)

Check failure on line 74 in test/style/configs_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.16.0/OTP25.1.2

test moves assignments to before configs (Styler.Style.ConfigsTest)
Expand Down

0 comments on commit 2e26d6e

Please sign in to comment.