Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 18, 2024
1 parent bf65bf7 commit eb1ab54
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,11 @@ defmodule Styler.Style.Configs do
end

def run({{:config, cfm, [_, _ | _]} = config, zm}, %{mix_config?: true, comments: comments} = ctx) do
# after running, this block should take up the same # of lines that it did before
# the first node of `rest` is greater than the highest line in configs, assignments
# config line is the first line to be used as part of this block
# that will change when we consider preceding comments
{node_comments, _} = comments_for_node(config, comments)
config_line = cfm[:line]
# all of these list are reversed due to the reduce
{configs, assignments, rest} = accumulate(zm.r, [], [])
# @TODO is it list.last or list.first?
first_line = min(List.last(node_comments)[:line] || config_line, config_line)
# @TODO
# okay so comments between nodes that we moved.......
# lets just push them out of the way. so
# lets just push them out of the way (???). so
# 1. figure out first/last possible lines we're talking about here
# 2. only pass comments in that range off
# 3. split those comments into "moved, didn't move"
Expand Down Expand Up @@ -94,15 +86,22 @@ defmodule Styler.Style.Configs do
# moving >=3 nodes hints that this is an initial run, where `set_lines` definitely outperforms.
{nodes, comments} =
case change_count(nodes) do
0 -> {nodes, comments}
n when n < 3 -> {Style.fix_line_numbers(nodes, List.last(rest)), comments}
_ -> set_lines(nodes, comments, first_line, [], [])
0 ->
{nodes, comments}

n when n < 3 ->
{Style.fix_line_numbers(nodes, List.last(rest)), comments}

_ ->
# after running, this block should take up the same # of lines that it did before
# the first node of `rest` is greater than the highest line in configs, assignments
# config line is the first line to be used as part of this block
# that will change when we consider preceding comments
{node_comments, _} = comments_for_node(config, comments)
first_line = min(List.last(node_comments)[:line] || cfm[:line], cfm[:line])
set_lines(nodes, comments, first_line)
end

# lol so the test with this line, demonstrates a bug where
# comments that aren't part of a node get a node comment shifted onto them
# i think that fixing 1-4 above will handle this error - we just push this stuff to the top, sorry gang, and call it a day

[config | left_siblings] = Enum.reverse(nodes, zm.l)

{:skip, {config, %{zm | l: left_siblings, r: rest}}, %{ctx | comments: comments}}
Expand Down Expand Up @@ -130,21 +129,14 @@ defmodule Styler.Style.Configs do

defp change_count(_, n), do: n

# merged =
# Enum.sort_by(nodes ++ comments, fn
# %{line: line} -> {line, 0}
# {_, m, _} -> {m[:line], 1}
# end)

# loop thru nodes
# looking at the previous last line
# - should take into account newlines...
# calculate how big of a shift this node gets when moving to the next line
# - take into account preceding comments
# - take into account newlines on previous node
defp set_lines(nodes, comments, first_line) do
{nodes, comments, node_comments} = set_lines(nodes, comments, first_line, [], [])
# @TODO if there are dangling comments between the nodes min/max, push them somewhere?
# likewise deal with conflicting line comments?
{nodes, Enum.sort_by(comments ++ node_comments, & &1.line)}
end

def set_lines([], comments, _, n_acc, []), do: {Enum.reverse(n_acc), comments}
def set_lines([], comments, _, n_acc, c_acc), do: {Enum.reverse(n_acc), Enum.sort_by(comments ++ c_acc, & &1.line)}
def set_lines([], comments, _, node_acc, c_acc), do: {Enum.reverse(node_acc), comments, c_acc}

def set_lines([{_, meta, _} = node | nodes], comments, start_line, n_acc, c_acc) do
line = meta[:line]
Expand All @@ -153,24 +145,16 @@ defmodule Styler.Style.Configs do
{node, node_comments, comments} =
if start_line == line do
{node, [], comments}
# IO.puts "maybe shift"
# dbg(node)
# dbg(hd(n_acc))
# dbg(mine)
# IO.puts "n/m"
# dbg({start_line, line, line_with_comments})
# dbg(node)
# dbg(hd(n_acc))
else
{mine, comments} = comments_for_lines(comments, line, last_line)
line_with_comments = (List.last(mine)[:line] || line) - (List.last(mine)[:previous_eol_count] || 1) + 1
line_with_comments = (List.first(mine)[:line] || line) - (List.first(mine)[:previous_eol_count] || 1) + 1

if line_with_comments == start_line do
{node, mine, comments}
else
shift = start_line - line_with_comments
node = Style.shift_line(node, shift)
# |> dbg()

mine = Enum.map(mine, &%{&1 | line: &1.line + shift})
{node, mine, comments}
end
Expand Down Expand Up @@ -208,7 +192,7 @@ defmodule Styler.Style.Configs do
# @TODO bug: match line looks like `x = :foo # comment for x`
# could account for that by pre-running the formatter on config files :/
line == start - 1 -> comments_for_lines(rev_comments, start - 1, last, [comment | match], acc)
true -> {Enum.reverse(match), Enum.reverse(rev_comments, [comment | acc])}
true -> {match, Enum.reverse(rev_comments, [comment | acc])}
end
end

Expand Down

0 comments on commit eb1ab54

Please sign in to comment.