Skip to content

Commit

Permalink
add Zipper.prepend/append_siblings
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Dec 10, 2023
1 parent b82007a commit 908681b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
9 changes: 5 additions & 4 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ defmodule Styler.Style.Blocks do
cond do
Enum.any?(preroll) ->
# put the preroll before the with statement in either a block we create or the existing parent block
preroll
|> Enum.reduce(Style.ensure_block_parent(zipper), &Zipper.insert_left(&2, &1))
zipper
|> Style.ensure_block_parent()
|> Zipper.prepend_siblings(preroll)
|> run(ctx)

Enum.any?(postroll) ->
Expand Down Expand Up @@ -189,8 +190,8 @@ defmodule Styler.Style.Blocks do
Zipper.update(zipper, fn {:with, meta, _} -> {:__block__, Keyword.take(meta, [:line]), block} end)

{{:__block__, _, _}, _} ->
block
|> Enum.reduce(zipper, &Zipper.insert_left(&2, &1))
zipper
|> Zipper.prepend_siblings(block)
|> Zipper.remove()

{ast, _} ->
Expand Down
12 changes: 5 additions & 7 deletions lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,11 @@ defmodule Styler.Style.ModuleDirectives do
Zipper.update(parent, &Zipper.replace_children(&1, directives))

true ->
{last_directive, meta} =
parent
|> Zipper.update(&Zipper.replace_children(&1, directives))
|> Zipper.down()
|> Zipper.rightmost()

{last_directive, %{meta | r: nondirectives}}
parent
|> Zipper.update(&Zipper.replace_children(&1, directives))
|> Zipper.down()
|> Zipper.rightmost()
|> Zipper.append_siblings(nondirectives)
end
end

Expand Down
22 changes: 22 additions & 0 deletions lib/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ defmodule Styler.Zipper do
def insert_left({_, nil}, _), do: raise(ArgumentError, message: "Can't insert siblings at the top level.")
def insert_left({tree, meta}, child), do: {tree, %{meta | l: [child | meta.l]}}

@doc """
Inserts many siblings to the left.
Equivalent to
Enum.reduce(siblings, zipper, &Zipper.insert_left(&2, &1))
"""
@spec prepend_siblings(zipper, [tree]) :: zipper
def prepend_siblings({_, nil}, _), do: raise(ArgumentError, message: "Can't insert siblings at the top level.")
def prepend_siblings({tree, meta}, siblings), do: {tree, %{meta | l: Enum.reverse(siblings, meta.l)}}

@doc """
Inserts the item as the right sibling of the node at this zipper, without
moving. Raises an `ArgumentError` when attempting to insert a sibling at the
Expand All @@ -182,6 +193,17 @@ defmodule Styler.Zipper do
def insert_right({_, nil}, _), do: raise(ArgumentError, message: "Can't insert siblings at the top level.")
def insert_right({tree, meta}, child), do: {tree, %{meta | r: [child | meta.r]}}

@doc """
Inserts many siblings to the right.
Equivalent to
Enum.reduce(siblings, zipper, &Zipper.insert_right(&2, &1))
"""
@spec append_siblings(zipper, [tree]) :: zipper
def append_siblings({_, nil}, _), do: raise(ArgumentError, message: "Can't insert siblings at the top level.")
def append_siblings({tree, meta}, siblings), do: {tree, %{meta | r: siblings ++ meta.r}}

@doc """
Inserts the item as the leftmost child of the node at this zipper,
without moving.
Expand Down

0 comments on commit 908681b

Please sign in to comment.