From eb1ab54ac82019fa8c01de7cb2b4d50be4d8827f Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Thu, 18 Apr 2024 15:41:54 -0400 Subject: [PATCH] cleanup --- lib/style/configs.ex | 66 +++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/lib/style/configs.ex b/lib/style/configs.ex index bdbce6b..14d0a2a 100644 --- a/lib/style/configs.ex +++ b/lib/style/configs.ex @@ -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" @@ -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}} @@ -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] @@ -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 @@ -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