Skip to content

Commit

Permalink
hmm
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 5, 2024
1 parent a62eaa1 commit f26d2d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
3 changes: 1 addition & 2 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"{config,lib,test}/**/*.{ex,exs}"
],
locals_without_parens: [
assert_style: 1,
assert_style: 2
assert_style: 1
],
plugins: [Styler],
line_length: 122
Expand Down
15 changes: 8 additions & 7 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,26 @@ defmodule Styler.Style.Configs do
other, {configs, assignments, rest} -> {configs, assignments, [other | rest]}
end)

[config | configs] =
assignments = assignments |> Enum.reverse() |> Style.reset_newlines()

[config | left_siblings] =
# in order: from L to R
[config | configs]
|> Enum.group_by(fn
{:config, _, [{:__block__, _, [app]} | _]} -> app
{:config, _, [arg | _]} -> Style.without_meta(arg)
end)
|> Enum.sort(:desc)
|> Enum.sort()
|> Enum.flat_map(fn {_app, configs} ->
configs
|> Enum.sort_by(&Style.without_meta/1, :asc)
|> Enum.sort_by(&Style.without_meta/1)
|> Style.reset_newlines()
|> Enum.reverse()
end)
|> Style.fix_line_numbers(List.first(rest))
|> dbg()
|> Enum.reverse(Enum.reverse(assignments, zm.l))

assignments = assignments |> Enum.reverse() |> Style.reset_newlines()

zm = %{zm | l: configs ++ Enum.reverse(assignments, zm.l), r: Enum.reverse(rest)}
zm = %{zm | l: left_siblings, r: Enum.reverse(rest)}
{:skip, {config, zm}, ctx}
end

Expand Down
55 changes: 25 additions & 30 deletions test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,51 @@ defmodule Styler.Style.ConfigsTest do
end
end

test "orders configs stanzas" do
# doesn't order when we haven't seen `import Config`, so this is something else that we don't understand
test "doesn't sort when no import config" do
assert_style """
config :z, :x
config :a, :b
config :z, :x, :c
config :a, :b, :c
"""
end

# 1. orders `config/2,3` relative to each other
# 2. lifts assignments above config blocks
# 3. non assignment/config separate "config" blocks
test "simple case" do
assert_style """
import Config
config :z, :x, :c
config :a, :b, :c
config :y, :x, :z
config :a, :c, :d
""",
"""
import Config
config :a, :b, :c
config :a, :c, :d
config :y, :x, :z
config :z, :x, :c
"""
end

test "more complicated" do
assert_style(
"""
import Config
dog_sound = :woof
# z is best when configged w/ dog sounds
# dog sounds ftw
config :z, :x, dog_sound
# this is my big c
# comment i'd like to leave c
# about c
c = :c
config :a, :b, c
config :a, :c, :d
config :a,
a_longer_name: :a_longer_value,
multiple_things: :that_could_all_fit_on_one_line_though
# this is my big my_app
# comment i'd like to leave my_app
# about my_app
my_app =
:"dont_write_configs_like_this_yall_:("
# this is my big your_app
# comment i'd like to leave your_app
# about your_app
your_app = :not_again!
config your_app, :dont_use_varrrrrrrrs
config my_app, :nooooooooo
Expand All @@ -79,23 +86,11 @@ defmodule Styler.Style.ConfigsTest do
import Config
dog_sound = :woof
# z is best when configged w/ dog sounds
# dog sounds ftw
# this is my big c
# comment i'd like to leave c
# about c
c = :c
# this is my big my_app
# comment i'd like to leave my_app
# about my_app
my_app =
:"dont_write_configs_like_this_yall_:("
# this is my big your_app
# comment i'd like to leave your_app
# about your_app
your_app = :not_again!
config :a, :b, c
Expand Down
2 changes: 1 addition & 1 deletion test/support/style_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ defmodule Styler.StyleCase do
assert true
else
flunk(
format_diff(restyled, styled, "expected styling to be idempotent, but a second pass resulted in more changes.")
format_diff(styled, restyled, "expected styling to be idempotent, but a second pass resulted in more changes.")
)
end
end
Expand Down

0 comments on commit f26d2d7

Please sign in to comment.