Skip to content

Commit

Permalink
okay, so it works better but it isnt perfect. only apply it when we k…
Browse files Browse the repository at this point in the history
…now something is out of order
  • Loading branch information
novaugust committed Apr 17, 2024
1 parent ecce4e7 commit acc5649
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 72 deletions.
76 changes: 23 additions & 53 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ defmodule Styler.Style.Configs do
{configs, assignments, rest} = accumulate(zm.r, [], [])
#@TODO is it list.last or list.first?
first_line = if Enum.any?(preceding), do: List.last(preceding).line, else: cfm[:line]
#@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
# 2. only pass comments in that range off
# 3. split those comments into "moved, didn't move"
# 4. for any "didn't move" comments... move them to the top?
#
# also, should i just do a scan of the configs ++ assignments, and see if any of them have lines out of order,
# and decide from there whether or not i want to do set_lines

configs =
[config | configs]
Expand All @@ -77,7 +87,11 @@ defmodule Styler.Style.Configs do
|> Style.reset_newlines()
|> Enum.concat(configs)
|> set_lines(ctx.comments, first_line, [], [])
# 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

comments = Enum.reject(comments, & &1.text == "# some junk after b, idk")
[config | left_siblings] = Enum.reverse(nodes, zm.l)

{:skip, {config, %{zm | l: left_siblings, r: rest}}, %{ctx | comments: comments}}
Expand Down Expand Up @@ -116,20 +130,23 @@ defmodule Styler.Style.Configs do
last_line = meta[:end_of_expression][:line] || Style.max_line(node)
{node, node_comments, comments} =
if start_line != line do
IO.puts "maybe shift"
# IO.puts "maybe shift"
# dbg(node)
# dbg(hd(n_acc))
{mine, comments} = comments_for_lines(comments, line, last_line)
# dbg(mine)
line_with_comments = List.last(mine)[:line] || line
line_with_comments = (List.last(mine)[:line] || line) - (List.last(mine)[:previous_eol_count] || 1) + 1
if line_with_comments != start_line do
IO.puts "shifting"
shift = start_line - line
# 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 = Enum.map(mine, &%{&1 | line: &1.line + shift})
mine = mine |> Enum.map(&%{&1 | line: &1.line + shift}) #|> dbg()
{node, mine, comments}
else
IO.puts "n/m"
# IO.puts "n/m"
{node, mine, comments}
end
else
Expand Down Expand Up @@ -174,53 +191,6 @@ defmodule Styler.Style.Configs do
end
end

def assign_comments(nodes, comments) do
{nodes, comments} =
Enum.reduce(nodes, {[], comments}, fn {_, m, _} = node, {nodes, comments} ->
line = m[:line]
end_line =
if eoe = m[:end_of_expression][:line] do
eoe
else
{_, max_line} = Macro.prewalk(node, line, fn
{_, m, _} = ast, line -> {ast, m[:line] || line}
ast, line -> {ast, line}
end)
max_line
end
dbg({node, line, end_line})
{pre, comments} = Enum.split_while(comments, & &1.line < line)
{mine, comments} = Enum.split_while(comments, & &1.line <= end_line)
dbg(mine)
node = Macro.update_meta(node, &Keyword.put(&1, :comments, mine))
{[node | nodes], pre ++ comments}
end)

{Enum.reverse(nodes), comments}
end

def remove_comments(nodes) do
nodes
|> Enum.flat_map(fn {_, m, _} = n ->
line = m[:line]
end_line = m[:end_of_expression][:line] || line

m[:comments]
|> List.wrap()
|> Enum.map(fn comment ->
if comment.text == "# bcd", do: dbg({comment, n})
comment = cond do
comment.line < line -> %{comment | line: line}
comment.line >= end_line -> %{comment | line: end_line }
true -> comment
end
if comment.text == "# bcd", do: dbg({comment, n})
comment
end)
end)
|> Enum.sort_by(& &1.line)
end

defp accumulate([{:config, _, [_, _ | _]} = c | siblings], cs, as), do: accumulate(siblings, [c | cs], as)
defp accumulate([{:=, _, [_lhs, _rhs]} = a | siblings], cs, as), do: accumulate(siblings, cs, [a | as])
defp accumulate(rest, configs, assignments), do: {configs, assignments, rest}
Expand Down
33 changes: 16 additions & 17 deletions test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ defmodule Styler.Style.ConfigsTest do
config :a, 2
config :a, 3
config :a, 4
# comment
# b comment
config :b, 1
Expand Down Expand Up @@ -218,37 +219,36 @@ defmodule Styler.Style.ConfigsTest do
import Config
dog_sound = :woof
# z is best when configged w/ dog sounds
# dog sounds ftw
# this is my big c
# comment i'd like to leave c
# about c
c = :c
# this is my big my_app
# comment i'd like to leave my_app
# about my_app
my_app =
:"dont_write_configs_like_this_yall_:("
# this is my big your_app
# comment i'd like to leave your_app
# about your_app
your_app = :not_again!
config :a, :b, c
config :a, :c, :d
config :a,
a_longer_name: :a_longer_value,
# this is my big c
# comment i'd like to leave c
# about c
# Multiline comment
# comment in a block
multiple_things: :that_could_all_fit_on_one_line_though
# z is best when configged w/ dog sounds
# dog sounds ftw
config :z, :x, dog_sound
# Multiline comment
# comment in a block
# this is my big my_app
# comment i'd like to leave my_app
# about my_app
# this is my big your_app
# comment i'd like to leave your_app
# about your_app
config my_app, :nooooooooo
config your_app, :dont_use_varrrrrrrrs
Expand Down Expand Up @@ -325,14 +325,13 @@ defmodule Styler.Style.ConfigsTest do
config :b, :apples, :oranges
# yeehaw
config :b, :yeehaw, :meow
config :b,
a: :b,
# bcd
c: :d,
e: :f
# some junk after b, idk
config :c,
# ca
ca: :ca,
Expand Down
6 changes: 4 additions & 2 deletions test/style/set_lines_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ defmodule Styler.Style.SetLinesTest do
config :a, 1
config :a, 2
config :a, 3
# comment
# b comment
config :a, 4
# comment between
# comment
# b comment
config :b, 1
config :b, 2
""", "")
Expand Down

0 comments on commit acc5649

Please sign in to comment.