Skip to content

Commit

Permalink
improve the Style.fix_line_numbers algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 5, 2024
1 parent 2e8991a commit b240fae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
34 changes: 9 additions & 25 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,33 +220,17 @@ defmodule Styler.Style do
4: # this is foo
5: def foo ...
"""
def fix_line_numbers(to_fix, acc \\ [], first_normal_line)
def fix_line_numbers(nodes, nil), do: fix_line_numbers(nodes, 999_999)
def fix_line_numbers(nodes, {_, meta, _}), do: fix_line_numbers(nodes, meta[:line])
def fix_line_numbers(nodes, max), do: nodes |> Enum.reverse() |> do_fix_lines(max, [])

def fix_line_numbers([this, next | rest], acc, first_non_directive) do
this = cap_line(this, next)
fix_line_numbers([next | rest], [this | acc], first_non_directive)
end

def fix_line_numbers([last], acc, first_non_directive) do
last = if first_non_directive, do: cap_line(last, first_non_directive), else: last
Enum.reverse([last | acc])
end
defp do_fix_lines([], _, acc), do: acc

def fix_line_numbers([], [], _), do: []
defp do_fix_lines([{_, meta, _} = node | nodes], max, acc) do
line = meta[:line]

defp cap_line({_, this_meta, _} = this, {_, next_meta, _}) do
this_line = this_meta[:line]
next_line = next_meta[:line]

if this_line > next_line do
# Subtracting 2 helps the behaviour with one-liner comments preceding the next node. It's a bit of a hack.
# TODO: look into the comments list and
# 1. move comment blocks preceding `this` up with it
# 2. find the earliest comment before `next` and set `new_line` to that value - 1
desired_line = next_line - 2
shift_line(this, desired_line - this_line)
else
this
end
if line > max,
do: do_fix_lines(nodes, max, [shift_line(node, max - line - 2) | acc]),
else: do_fix_lines(nodes, line, [node | acc])
end
end
29 changes: 29 additions & 0 deletions test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ defmodule Styler.Style.ConfigsTest do
"""
end

test "simple case" do
assert_style(
"""
import Config
config :a, 1
config :a, 4
# comment
# b comment
config :b, 1
config :b, 2
config :a, 2
config :a, 3
""",
"""
import Config
config :a, 1
config :a, 2
config :a, 3
config :a, 4
# comment
# b comment
config :b, 1
config :b, 2
"""
)
end

test "complicated comments" do
assert_style(
"""
Expand Down
14 changes: 14 additions & 0 deletions test/style_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Styler.StyleTest do

import Styler.Style, only: [displace_comments: 2, shift_comments: 3]

alias Styler.Style

@code """
# Above module
defmodule Foo do
Expand Down Expand Up @@ -115,4 +117,16 @@ defmodule Styler.StyleTest do
end
end
end

describe "fix_line_numbers" do
test "returns ast list with increasing line numbers" do
nodes = for n <- [1, 2, 999, 1000, 5, 6], do: {:node, [line: n], [n]}
fixed = Style.fix_line_numbers(nodes, 7)

Enum.scan(fixed, fn {_, [line: this_line], _} = this_node, {_, [line: previous_line], _} ->
assert this_line >= previous_line
this_node
end)
end
end
end

0 comments on commit b240fae

Please sign in to comment.