Skip to content

Commit

Permalink
spike on sorting comment directive. closes #167
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 22, 2024
1 parent 4ec6ba7 commit da4f9e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
42 changes: 42 additions & 0 deletions lib/style/comment_directives.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

defmodule Styler.Style.CommentDirectives do
@moduledoc "TODO"

@behaviour Styler.Style

alias Styler.Zipper

def run(zipper, ctx) do
zipper =
ctx.comments
|> Enum.filter(& &1.text == "# styler:sort")
|> Enum.map(& &1.line)
|> Enum.reduce(zipper, fn line, zipper ->
found =
Zipper.find(zipper, fn
{_, meta, _} -> meta[:line] >= line
_ -> false
end)

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

{:skip, zipper, ctx}
end
end
3 changes: 2 additions & 1 deletion lib/styler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Styler do
Styler.Style.Defs,
Styler.Style.Blocks,
Styler.Style.Deprecations,
Styler.Style.Configs
Styler.Style.Configs,
Styler.Style.CommentDirectives
]

@doc false
Expand Down
24 changes: 24 additions & 0 deletions test/style/comment_directives_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

defmodule Styler.Style.CommentDirectivesTest do
@moduledoc false
use Styler.StyleCase, async: true

describe "sort" do
test "we dont just sort by accident" do
assert_style "[:c, :b, :a]"
end

test "sorts lists" do
assert_style "# styler:sort\n[:c, :b, :a]", "# styler:sort\n[:a, :b, :c]"
end
end
end

0 comments on commit da4f9e2

Please sign in to comment.