Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: track supertree, and add Zipper.all_the_way_up/1 #148

Merged
merged 7 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elixir 1.16.3
zachdaniel marked this conversation as resolved.
Show resolved Hide resolved
83 changes: 50 additions & 33 deletions lib/sourceror/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ defmodule Sourceror.Zipper do

alias Sourceror.Zipper, as: Z

defstruct [:node, :path]
defstruct [:node, :path, :supertree]

@type t :: %Z{
node: tree,
path: path | nil
path: path | nil,
supertree: t | nil
}

@opaque path :: %{
Expand All @@ -41,10 +42,12 @@ defmodule Sourceror.Zipper do

@type tree :: Macro.t()

@compile {:inline, new: 1, new: 2}
@compile {:inline, new: 1, new: 3}
defp new(node), do: %Z{node: node}
defp new(node, nil), do: %Z{node: node}
defp new(node, %{left: _, parent: _, right: _} = path), do: %Z{node: node, path: path}
defp new(node, nil, supertree), do: %Z{node: node, supertree: supertree}

defp new(node, %{left: _, parent: _, right: _} = path, supertree),
do: %Z{node: node, path: path, supertree: supertree}

@spec branch?(tree) :: boolean
def branch?({_, _, args}) when is_list(args), do: true
Expand Down Expand Up @@ -81,12 +84,22 @@ defmodule Sourceror.Zipper do
def zip(node), do: new(node)

@doc """
Walks the `zipper` all the way up and returns the top `zipper`.
Walks the `zipper` to the top of the current subtree and returns that `zipper`.
"""
@spec top(t) :: t
def top(%Z{path: nil} = zipper), do: zipper
def top(zipper), do: zipper |> up() |> top()

@doc """
Walks the `zipper` all the way up, breaking out of any subtrees and returns the top-most `zipper`.
"""
@spec all_the_way_up(t) :: t
def all_the_way_up(%Z{supertree: supertree} = zipper) when not is_nil(supertree) do
all_the_way_up(into(top(zipper), supertree))
end

def all_the_way_up(zipper), do: top(zipper)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can call this topmost? It feels a bit more consistent with the names rightmost and leftmost

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like topmost 👍


@doc """
Walks the `zipper` all the way up and returns the root `node`.
"""
Expand All @@ -104,12 +117,12 @@ defmodule Sourceror.Zipper do
`nil` if there's no children.
"""
@spec down(t) :: t | nil
def down(%Z{node: tree} = zipper) do
def down(%Z{node: tree, supertree: supertree} = zipper) do
case children(tree) do
nil -> nil
[] -> nil
[first] -> new(first, %{parent: zipper, left: nil, right: nil})
[first | rest] -> new(first, %{parent: zipper, left: nil, right: rest})
[first] -> new(first, %{parent: zipper, left: nil, right: nil}, supertree)
[first | rest] -> new(first, %{parent: zipper, left: nil, right: rest}, supertree)
end
end

Expand All @@ -120,10 +133,10 @@ defmodule Sourceror.Zipper do
@spec up(t) :: t | nil
def up(%Z{path: nil}), do: nil

def up(%Z{node: tree, path: path}) do
def up(%Z{node: tree, path: path, supertree: supertree}) do
children = Enum.reverse(path.left || []) ++ [tree] ++ (path.right || [])
%Z{node: parent, path: parent_path} = path.parent
new(make_node(parent, children), parent_path)
new(make_node(parent, children), parent_path, supertree)
end

@doc """
Expand All @@ -133,19 +146,19 @@ defmodule Sourceror.Zipper do
@spec left(t) :: t | nil
def left(zipper)

def left(%Z{node: tree, path: %{left: [ltree | l], right: r} = path}),
do: new(ltree, %{path | left: l, right: [tree | r || []]})
def left(%Z{node: tree, path: %{left: [ltree | l], right: r} = path, supertree: supertree}),
do: new(ltree, %{path | left: l, right: [tree | r || []]}, supertree)

def left(_), do: nil

@doc """
Returns the leftmost sibling of the `node` at this `zipper`, or itself.
"""
@spec leftmost(t) :: t
def leftmost(%Z{node: tree, path: %{left: [_ | _] = l} = path}) do
def leftmost(%Z{node: tree, path: %{left: [_ | _] = l} = path, supertree: supertree}) do
[left | rest] = Enum.reverse(l)
r = rest ++ [tree] ++ (path.right || [])
new(left, %{path | left: nil, right: r})
new(left, %{path | left: nil, right: r}, supertree)
end

def leftmost(zipper), do: zipper
Expand All @@ -157,19 +170,19 @@ defmodule Sourceror.Zipper do
@spec right(t) :: t | nil
def right(zipper)

def right(%Z{node: tree, path: %{right: [rtree | r]} = path}),
do: new(rtree, %{path | right: r, left: [tree | path.left || []]})
def right(%Z{node: tree, path: %{right: [rtree | r]} = path, supertree: supertree}),
do: new(rtree, %{path | right: r, left: [tree | path.left || []]}, supertree)

def right(_), do: nil

@doc """
Returns the rightmost sibling of the `node` at this `zipper`, or itself.
"""
@spec rightmost(t) :: t
def rightmost(%Z{node: tree, path: %{right: [_ | _] = r} = path}) do
def rightmost(%Z{node: tree, path: %{right: [_ | _] = r} = path, supertree: supertree}) do
[right | rest] = Enum.reverse(r)
l = rest ++ [tree] ++ (path.left || [])
new(right, %{path | left: l, right: nil})
new(right, %{path | left: l, right: nil}, supertree)
end

def rightmost(zipper), do: zipper
Expand Down Expand Up @@ -199,12 +212,12 @@ defmodule Sourceror.Zipper do
def remove(%Z{path: nil}),
do: raise(ArgumentError, message: "Cannot remove the top level node.")

def remove(%Z{path: path} = zipper) do
def remove(%Z{path: path, supertree: supertree} = zipper) do
case path.left do
[{:__block__, meta, [name]} = left | rest] when is_reserved_block_name(name) ->
if meta[:format] == :keyword do
left
|> new(%{path | left: rest})
|> new(%{path | left: rest}, supertree)
|> do_prev()
else
zipper
Expand All @@ -214,7 +227,7 @@ defmodule Sourceror.Zipper do

[left | rest] ->
left
|> new(%{path | left: rest})
|> new(%{path | left: rest}, supertree)
|> do_prev()

_ ->
Expand All @@ -223,7 +236,7 @@ defmodule Sourceror.Zipper do

parent
|> make_node(children)
|> new(parent_path)
|> new(parent_path, supertree)
end
end

Expand All @@ -238,8 +251,8 @@ defmodule Sourceror.Zipper do
def insert_left(%Z{path: nil}, _),
do: raise(ArgumentError, message: "Can't insert siblings at the top level.")

def insert_left(%Z{node: tree, path: path}, child) do
new(tree, %{path | left: [child | path.left || []]})
def insert_left(%Z{node: tree, path: path, supertree: supertree}, child) do
new(tree, %{path | left: [child | path.left || []]}, supertree)
end

@doc """
Expand All @@ -253,18 +266,18 @@ defmodule Sourceror.Zipper do
def insert_right(%Z{path: nil}, _),
do: raise(ArgumentError, message: "Can't insert siblings at the top level.")

def insert_right(%Z{node: tree, path: path}, child) do
new(tree, %{path | right: [child | path.right || []]})
def insert_right(%Z{node: tree, path: path, supertree: supertree}, child) do
new(tree, %{path | right: [child | path.right || []]}, supertree)
end

@doc """
Inserts the `child` as the leftmost `child` of the `node` at this `zipper`,
without moving.
"""
def insert_child(%Z{node: tree, path: path}, child) do
def insert_child(%Z{node: tree, path: path, supertree: supertree}, child) do
tree
|> do_insert_child(child)
|> new(path)
|> new(path, supertree)
end

defp do_insert_child(list, child) when is_list(list), do: [child | list]
Expand All @@ -278,10 +291,10 @@ defmodule Sourceror.Zipper do
Inserts the `child` as the rightmost `child` of the `node` at this `zipper`,
without moving.
"""
def append_child(%Z{node: tree, path: path}, child) do
def append_child(%Z{node: tree, path: path, supertree: supertree}, child) do
tree
|> do_append_child(child)
|> new(path)
|> new(path, supertree)
end

defp do_append_child(list, child) when is_list(list), do: list ++ [child]
Expand Down Expand Up @@ -453,7 +466,10 @@ defmodule Sourceror.Zipper do
end

@compile {:inline, into: 2}
defp into(%Z{path: nil} = zipper, %Z{path: path}), do: %{zipper | path: path}
defp into(zipper, nil), do: zipper

defp into(%Z{path: nil} = zipper, %Z{path: path, supertree: supertree}),
do: %{zipper | path: path, supertree: supertree}

@doc """
Returns a `zipper` to the `node` that satisfies the `predicate` function, or
Expand Down Expand Up @@ -486,7 +502,8 @@ defmodule Sourceror.Zipper do
"""
@spec subtree(t) :: t
@compile {:inline, subtree: 1}
def subtree(%Z{} = zipper), do: %{zipper | path: nil}
def subtree(%Z{} = zipper),
do: %{zipper | path: nil, supertree: zipper}

@doc """
Runs the function `fun` on the subtree of the currently focused `node` and
Expand Down
12 changes: 12 additions & 0 deletions test/zipper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ defmodule SourcerorTest.ZipperTest do
end
end

describe "all_the_way_up/1" do
test "returns the top zipper, breaking out of subtrees" do
assert Z.zip([1, [2, [3, 4]]])
|> Z.next()
|> Z.next()
|> Z.next()
|> Z.subtree()
|> Z.all_the_way_up() ==
%Z{node: [1, [2, [3, 4]]]}
end
end

describe "root/1" do
test "returns the root node" do
assert Z.zip([1, [2, [3, 4]]]) |> Z.next() |> Z.next() |> Z.next() |> Z.root() ==
Expand Down
Loading