Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 18, 2024
1 parent acb7b8e commit bf65bf7
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 117 deletions.
5 changes: 3 additions & 2 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 29 additions & 28 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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}
Expand All @@ -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.
Expand Down Expand Up @@ -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])}
Expand Down
151 changes: 77 additions & 74 deletions test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -266,84 +266,87 @@ defmodule Styler.Style.ConfigsTest do
end

test "comments, more nuanced" do

Check failure on line 268 in test/style/configs_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.15.7/OTP25.1.2

test playing nice with comments comments, more nuanced (Styler.Style.ConfigsTest)

Check failure on line 268 in test/style/configs_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.16.0/OTP25.1.2

test playing nice with comments comments, more nuanced (Styler.Style.ConfigsTest)
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
27 changes: 15 additions & 12 deletions test/style/set_lines_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ defmodule Styler.Style.SetLinesTest do

test "sigh" do

Check failure on line 15 in test/style/set_lines_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.15.7/OTP25.1.2

test sigh (Styler.Style.SetLinesTest)

Check failure on line 15 in test/style/set_lines_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.16.0/OTP25.1.2

test sigh (Styler.Style.SetLinesTest)
{{_, _, 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
Expand Down
2 changes: 1 addition & 1 deletion test/support/style_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf65bf7

Please sign in to comment.