Skip to content

Commit

Permalink
assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 22, 2024
1 parent 01731ce commit 596306b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 30 deletions.
64 changes: 34 additions & 30 deletions lib/style/comment_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,46 @@ defmodule Styler.Style.CommentDirectives do
|> Enum.reduce(zipper, fn line, zipper ->
found =
Zipper.find(zipper, fn
{_, meta, _} -> meta[:line] >= line
{_, meta, _} -> Keyword.get(meta, :line, -1) >= line
_ -> false
end)

case found do
nil ->
zipper

{{:__block__, meta, [list]}, _} when is_list(list) ->
list = Enum.sort_by(list, fn {f, _, a} -> {f, a} end)
Zipper.replace(found, {:__block__, meta, [list]})

{{:sigil_w, sm, [{:<<>>, bm, [string]}, modifiers]} = node, _} ->
dbg(node)
# ew. gotta be a better way.
# this keeps indentation for the sigil via joiner, while prepend and append are the bookending whitespace
{prepend, joiner, append} =
case Regex.run(~r|^\s+|, string) do
# oneliner like `~w|c a b|`
nil -> {"", " ", ""}
# multline like
# `"\n a\n list\n long\n of\n static\n values\n"`
# ^^^^ `prepend` ^^^^ `joiner` ^^ `append`
# note that joiner and append are the same in a multline (unsure if this is always true)
[joiner] -> {joiner, joiner, if(String.contains?(joiner, "\n"), do: "\n", else: "")}
end

string = string |> String.split() |> Enum.sort() |> Enum.join(joiner)
Zipper.replace(found, {:sigil_w, sm, [{:<<>>, bm, [prepend, string, append]}, modifiers]})

x ->
dbg(Zipper.node(x))
found
if found do
Zipper.update(found, &sort/1)
else
zipper
end
end)

{:skip, zipper, ctx}
end

defp sort({:__block__, meta, [list]}) when is_list(list) do
list = Enum.sort_by(list, fn {f, _, a} -> {f, a} end)
{:__block__, meta, [list]}
end

defp sort({:sigil_w, sm, [{:<<>>, bm, [string]}, modifiers]}) do
# ew. gotta be a better way.
# this keeps indentation for the sigil via joiner, while prepend and append are the bookending whitespace
{prepend, joiner, append} =
case Regex.run(~r|^\s+|, string) do
# oneliner like `~w|c a b|`
nil -> {"", " ", ""}
# multline like
# `"\n a\n list\n long\n of\n static\n values\n"`
# ^^^^ `prepend` ^^^^ `joiner` ^^ `append`
# note that joiner and prepend are the same in a multiline (unsure if this is always true)
#@TODO: get all 3 in one pass of a regex. probably have to turn off greedy or something...
[joiner] -> {joiner, joiner, ~r|\s+$| |> Regex.run(string) |> hd}
end

string = string |> String.split() |> Enum.sort() |> Enum.join(joiner)
{:sigil_w, sm, [{:<<>>, bm, [prepend, string, append]}, modifiers]}
end

defp sort({:=, m, [lhs, rhs]}), do: {:=, m, [lhs, sort(rhs)]}
defp sort({:@, m, [{a, am, [assignment]}]}), do: {:@, m, [{a, am, [sort(assignment)]}]}

defp sort(x), do: dbg(x)
end
60 changes: 60 additions & 0 deletions test/style/comment_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,65 @@ defmodule Styler.Style.CommentDirectivesTest do
"""
)
end

test "assignments" do
assert_style(
"""
# styler:sort
my_var =
~w(
a
long
list
of
static
values
)
""",
"""
# styler:sort
my_var =
~w(
a
list
long
of
static
values
)
"""
)

assert_style(
"""
defmodule M do
@moduledoc false
# styler:sort
@attr ~w(
a
long
list
of
static
values
)
end
""",
"""
defmodule M do
@moduledoc false
# styler:sort
@attr ~w(
a
list
long
of
static
values
)
end
"""
)
end
end
end

0 comments on commit 596306b

Please sign in to comment.