Skip to content

Commit

Permalink
Add Zipper.subtree/1 (#133)
Browse files Browse the repository at this point in the history
* Add Zipper.subtree/1

* Update test/zipper_test.exs

Co-authored-by: Zach Allaun <zach.allaun@gmail.com>

---------

Co-authored-by: Dorgan <dorgandash@gmail.com>
Co-authored-by: Zach Allaun <zach.allaun@gmail.com>
  • Loading branch information
3 people authored May 22, 2024
1 parent 9bf292b commit c900c56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/sourceror/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,14 @@ defmodule Sourceror.Zipper do

defp do_append_child(list, child) when is_list(list), do: list ++ [child]
defp do_append_child({left, right}, child), do: {:{}, [], [left, right, child]}

@doc """
Returns a new zipper that is a subtree of the currently focused node.
"""
@spec subtree(t) :: t
def subtree(%Z{} = zipper) do
zipper
|> node()
|> zip()
end
end
12 changes: 12 additions & 0 deletions test/zipper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ defmodule SourcerorTest.ZipperTest do
end
end

describe "subtree/1" do
test "returns a new zipper isolated on the focused of the parent zipper" do

Check failure on line 549 in test/zipper_test.exs

View workflow job for this annotation

GitHub Actions / ci (1.12.3, 24.3, true)

test subtree/1 returns a new zipper isolated on the focused of the parent zipper (SourcerorTest.ZipperTest)

Check failure on line 549 in test/zipper_test.exs

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 24.3)

test subtree/1 returns a new zipper isolated on the focused of the parent zipper (SourcerorTest.ZipperTest)

Check failure on line 549 in test/zipper_test.exs

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 25.0.2)

test subtree/1 returns a new zipper isolated on the focused of the parent zipper (SourcerorTest.ZipperTest)

Check failure on line 549 in test/zipper_test.exs

View workflow job for this annotation

GitHub Actions / ci (1.14.3, 25.2)

test subtree/1 returns a new zipper isolated on the focused of the parent zipper (SourcerorTest.ZipperTest)

Check failure on line 549 in test/zipper_test.exs

View workflow job for this annotation

GitHub Actions / ci (1.16.0, 26)

test subtree/1 returns a new zipper isolated on the focused of the parent zipper (SourcerorTest.ZipperTest)
zipper =
[1, [2, 3], 4, 5]
|> Z.zip()
|> Z.next()
|> Z.next()

assert Z.subtree(zipper) |> Z.root() |> Z.node() == [2, 3]
end
end

describe "Zipper.Inspect" do
test "inspect/2 defaults to using zippers: :as_ast" do
zipper = Z.zip([1, [2], 3])
Expand Down

0 comments on commit c900c56

Please sign in to comment.