Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 17, 2024
1 parent a80bade commit ecce4e7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
58 changes: 32 additions & 26 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,12 @@ defmodule Styler.Style.Configs do
# 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
config_line = cfm[:line]
{preceding, comments} = comments_for_node(config, ctx.comments)
{preceding, _} = comments_for_node(config, ctx.comments)
# all of these list are reversed due to the reduce
{configs, assignments, rest} = accumulate(zm.r, [], [])
first_line = if Enum.any?(preceding), do: List.last(preceding).line, else: config_line
# last_line =
# if Enum.any?(rest),
# do: rest |> hd() |> elem(1) |> Keyword.get_lazy(:line, fn -> Style.max_line(configs ++ assignments) end),
# else: Style.max_line(configs ++ assignments)
#@TODO is it list.last or list.first?
first_line = if Enum.any?(preceding), do: List.last(preceding).line, else: cfm[:line]

# {configs, comments} =
configs =
[config | configs]
|> Enum.group_by(fn
Expand All @@ -76,15 +71,16 @@ defmodule Styler.Style.Configs do
|> Style.reset_newlines()
end)

[config | left_siblings] =
{nodes, comments} =
assignments
|> Enum.reverse()
|> Style.reset_newlines()
|> Stream.concat(configs)
|> set_lines(comments, first_line, [], [])
|> Enum.reverse(zm.l)
|> Enum.concat(configs)
|> set_lines(ctx.comments, first_line, [], [])

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

{:skip, {config, %{zm | l: left_siblings, r: rest}}, ctx}
{:skip, {config, %{zm | l: left_siblings, r: rest}}, %{ctx | comments: comments}}
end

def run(zipper, %{config?: true} = ctx) do
Expand Down Expand Up @@ -120,23 +116,33 @@ 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
# start at 7
# at 8
# shift by -1
{preceding, comments} = comments_for_lines(comments, line, last_line)
shift = start_line - line
node = Style.shift_line(node, shift)
#@TODO comments
# comments should be pulled out of the input comment generator and put into a shifted comment list
# that'll keep me from moving a comment two times by mistake
# then when we're done, sort the comments by line number
{node, preceding, comments}
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
if line_with_comments != start_line do
IO.puts "shifting"
shift = start_line - line
node = Style.shift_line(node, shift)
mine = Enum.map(mine, &%{&1 | line: &1.line + shift})
{node, mine, comments}
else
IO.puts "n/m"
{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
# @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.
# i think that's accomplishable by doing a final call above that finds all comments in the comments list that weren't moved
# and which are in the range of start..finish and sets their lines to finish!
last_line = meta[:end_of_expression][:line] || Style.max_line(node)
last_line = (meta[:end_of_expression][:newlines] || 1) + last_line
set_lines(nodes, comments, last_line, [node | n_acc], node_comments ++ c_acc)
Expand Down Expand Up @@ -164,7 +170,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, acc)}
true -> {Enum.reverse(match), Enum.reverse(rev_comments, [comment | acc])}
end
end

Expand Down
40 changes: 17 additions & 23 deletions test/style/set_lines_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,28 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

defmodule Styler.Style.ConfigsTest do
defmodule Styler.Style.SetLinesTest do
@moduledoc false
use Styler.StyleCase, async: true, filename: "config/config.exs"

test "sigh" do
{ast, comments} = Style.string_to_quoted_with_comments("""
line = meta[:line]
last_line = meta[:end_of_expression][:line] || Style.max_line(node)
{node, node_comments, comments} =
if start_line != line do
# start at 7
# at 8
# shift by -1
{precedng, comments} = comments_for_lines(comments, line, last_line)
shift = start_line - line
node = Style.shift_line(node, shift)
#@TODO comments
# comments should be pulled out of the input comment generator and put into a shifted comment list
# that'll keep me from moving a comment two times by mistake
# then when we're done, sort the comments by line number
{node, preceding, comments}
else
{node, [], comments}
end
{{_, _, ast}, comments} =
Styler.string_to_quoted_with_comments("""
import Config
{_, meta, _} = node
""")
jumbled = Macro.prewalk(ast, fn)
config :a, 1
config :a, 2
config :a, 3
# comment
# b comment
config :a, 4
config :b, 1
config :b, 2
""", "")

assert {sast, scomments} = Styler.Style.Configs.set_lines(ast, comments, 1, [], [])
assert comments == scomments
assert ast == sast
end
end

0 comments on commit ecce4e7

Please sign in to comment.