diff --git a/lib/style.ex b/lib/style.ex index 15e6d53..386fe98 100644 --- a/lib/style.ex +++ b/lib/style.ex @@ -230,9 +230,10 @@ defmodule Styler.Style do else: do_fix_lines(nodes, line, [node | acc]) end - #@TODO can i shortcut and just return end_of_expression[:line] when it's available? + # @TODO can i shortcut and just return end_of_expression[:line] when it's available? def max_line(ast) do - {_, max_line} = Macro.prewalk(ast, 0, fn + {_, max_line} = + Macro.prewalk(ast, 0, fn {_, meta, _} = ast, max -> {ast, max(meta[:line] || max, max)} ast, max -> {ast, max} end) diff --git a/lib/style/configs.ex b/lib/style/configs.ex index 153ec61..bdbce6b 100644 --- a/lib/style/configs.ex +++ b/lib/style/configs.ex @@ -56,9 +56,9 @@ defmodule Styler.Style.Configs do 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? + # @TODO is it list.last or list.first? first_line = min(List.last(node_comments)[:line] || config_line, config_line) - #@TODO + # @TODO # okay so comments between nodes that we moved....... # lets just push them out of the way. so # 1. figure out first/last possible lines we're talking about here @@ -99,9 +99,9 @@ defmodule Styler.Style.Configs do _ -> 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 + # 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) @@ -114,7 +114,7 @@ defmodule Styler.Style.Configs do def run(zipper, %{file: file} = ctx) do if file =~ ~r|config/.*\.exs| or file =~ ~r|rel/overlays/.*\.exs| do - #@TODO have this run forward to `import Config`, then run forward from there until we find `config` itself. no need for multi function head + # @TODO have this run forward to `import Config`, then run forward from there until we find `config` itself. no need for multi function head {:cont, zipper, Map.put(ctx, :config?, true)} else {:halt, zipper, ctx} @@ -136,45 +136,46 @@ defmodule Styler.Style.Configs do # {_, 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 + # 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 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([{_, meta, _} = node | nodes], comments, start_line, n_acc, c_acc) do line = meta[:line] last_line = meta[:end_of_expression][:line] || Style.max_line(node) + # IO.puts "shifting by #{shift}" {node, node_comments, comments} = - if start_line != line do + if start_line == line do + {node, [], comments} # IO.puts "maybe shift" # dbg(node) # dbg(hd(n_acc)) - {mine, comments} = comments_for_lines(comments, line, last_line) # 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 - if line_with_comments != start_line do - # dbg({start_line, line, line_with_comments}) - # dbg(node) - # dbg(hd(n_acc)) - shift = start_line - line_with_comments - # IO.puts "shifting by #{shift}" - node = Style.shift_line(node, shift) - mine = mine |> Enum.map(&%{&1 | line: &1.line + shift}) #|> dbg() + + if line_with_comments == start_line do {node, mine, comments} else - # IO.puts "n/m" + 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 - else - {node, [], comments} end - - {_, meta, _} = node # @TODO what about comments that were free floating between blocks? i'm just ignoring them and maybe always will... # kind of just want to shove them to the end though, so that they don't interrupt existing stanzas. @@ -204,7 +205,7 @@ defmodule Styler.Style.Configs do cond do line > last -> comments_for_lines(rev_comments, start, last, match, [comment | acc]) line >= start -> comments_for_lines(rev_comments, start, last, [comment | match], acc) - #@TODO bug: match line looks like `x = :foo # comment for x` + # @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])} diff --git a/test/style/configs_test.exs b/test/style/configs_test.exs index 3ea289a..9a09c5a 100644 --- a/test/style/configs_test.exs +++ b/test/style/configs_test.exs @@ -266,84 +266,87 @@ defmodule Styler.Style.ConfigsTest do end test "comments, more nuanced" do - assert_style(""" - # start config - # import - import Config + assert_style( + """ + # start config + # import + import Config - # random noise - - config :c, - # ca - ca: :ca, - # cb 1 - # cb 2 - cb: :cb, - cc: :cc, - # cd - cd: :cd - - config :b, :apples, :oranges - # yeehaw - config :b, :yeehaw, :meow - config :b, - a: :b, - # bcd - c: :d, - e: :f - - # some junk after b, idk + # random noise + + config :c, + # ca + ca: :ca, + # cb 1 + # cb 2 + cb: :cb, + cc: :cc, + # cd + cd: :cd + + config :b, :apples, :oranges + # yeehaw + config :b, :yeehaw, :meow + config :b, + a: :b, + # bcd + c: :d, + e: :f + + # some junk after b, idk - config :a, - # aa - aa: :aa, - # ab 1 - # ab 2 - ab: :ab, - ac: :ac, - # ad - ad: :cd - - # end of config - """,""" - # start config - # import - import Config + config :a, + # aa + aa: :aa, + # ab 1 + # ab 2 + ab: :ab, + ac: :ac, + # ad + ad: :cd + + # end of config + """, + """ + # start config + # import + import Config - # random noise + # random noise - config :a, - # aa - aa: :aa, - # ab 1 - # ab 2 - ab: :ab, - ac: :ac, - # ad - ad: :cd - - config :b, :apples, :oranges - # yeehaw - config :b, :yeehaw, :meow - - config :b, - a: :b, - # bcd - c: :d, - e: :f - - config :c, - # ca - ca: :ca, - # cb 1 - # cb 2 - cb: :cb, - cc: :cc, - # cd - cd: :cd - - # end of config - """) + config :a, + # aa + aa: :aa, + # ab 1 + # ab 2 + ab: :ab, + ac: :ac, + # ad + ad: :cd + + config :b, :apples, :oranges + # yeehaw + config :b, :yeehaw, :meow + + config :b, + a: :b, + # bcd + c: :d, + e: :f + + config :c, + # ca + ca: :ca, + # cb 1 + # cb 2 + cb: :cb, + cc: :cc, + # cd + cd: :cd + + # end of config + """ + ) end end end diff --git a/test/style/set_lines_test.exs b/test/style/set_lines_test.exs index bee0048..283251e 100644 --- a/test/style/set_lines_test.exs +++ b/test/style/set_lines_test.exs @@ -14,21 +14,24 @@ defmodule Styler.Style.SetLinesTest do test "sigh" do {{_, _, ast}, comments} = - Styler.string_to_quoted_with_comments(""" - import Config + Styler.string_to_quoted_with_comments( + """ + import Config - config :a, 1 - config :a, 2 - config :a, 3 - config :a, 4 + config :a, 1 + config :a, 2 + config :a, 3 + config :a, 4 - # comment between + # comment between - # comment - # b comment - config :b, 1 - config :b, 2 - """, "") + # comment + # b comment + config :b, 1 + config :b, 2 + """, + "" + ) assert {sast, scomments} = Styler.Style.Configs.set_lines(ast, comments, 1, [], []) assert comments == scomments diff --git a/test/support/style_case.ex b/test/support/style_case.ex index fe48b69..b88d35c 100644 --- a/test/support/style_case.ex +++ b/test/support/style_case.ex @@ -51,7 +51,7 @@ defmodule Styler.StyleCase do dbg(styled_ast) dbg(styled_comments) IO.puts("========================\n") - IO.puts "wtf?" + IO.puts("wtf?") end if expected == styled do