Packages

Tree-sitter bindings (nif) for elixir. Currently small subset of the tree-sitter api is supported, but happy to accept merge requests/patches

Current section

6 Versions

Jump to

Compare versions

7 files changed
+121 additions
-10 deletions
  @@ -9,7 +9,7 @@ The package can be installed by adding `treesitter` to your list of dependencies
9 9 ```elixir
10 10 def deps do
11 11 [
12 - {:treesitter_elixir, "~> 0.1.3"}
12 + {:treesitter_elixir, "~> 0.1.4"}
13 13 ]
14 14 end
15 15 ```
  @@ -2,15 +2,16 @@
2 2 [{<<"issues">>,<<"https://codeberg.org/edwinvanl/treesitter_elixir/issues">>},
3 3 {<<"source">>,<<"https://codeberg.org/edwinvanl/treesitter_elixir">>}]}.
4 4 {<<"name">>,<<"treesitter_elixir">>}.
5 - {<<"version">>,<<"0.1.3">>}.
5 + {<<"version">>,<<"0.1.4">>}.
6 6 {<<"description">>,
7 7 <<"Tree-sitter bindings (nif) for elixir. Currently small subset of the tree-sitter api is supported, but happy to accept merge requests/patches">>}.
8 8 {<<"elixir">>,<<"~> 1.18">>}.
9 9 {<<"app">>,<<"treesitter_elixir">>}.
10 10 {<<"files">>,
11 - [<<"lib">>,<<"lib/treesitter.ex">>,<<".formatter.exs">>,<<"mix.exs">>,
12 - <<"README.md">>,<<"LICENSE">>,<<"src">>,<<"src/treesitter_nif.c">>,
13 - <<"Makefile">>]}.
11 + [<<"lib">>,<<"lib/treesitter">>,<<"lib/treesitter/node.ex">>,
12 + <<"lib/treesitter/querycursor.ex">>,<<"lib/treesitter.ex">>,
13 + <<".formatter.exs">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>,<<"src">>,
14 + <<"src/treesitter_nif.c">>,<<"Makefile">>]}.
14 15 {<<"licenses">>,[<<"MIT">>]}.
15 16 {<<"requirements">>,
16 17 [[{<<"name">>,<<"elixir_make">>},
  @@ -127,6 +127,18 @@ defmodule TreeSitter.Nif do
127 127 def query_capture_name_for_id(_, _) do
128 128 :erlang.nif_error(:nif_not_loaded)
129 129 end
130 +
131 + def query_cursor_set_point_range(query, from, to)
132 +
133 + def query_cursor_set_point_range(_, _, _) do
134 + :erlang.nif_error(:nif_not_loaded)
135 + end
136 +
137 + def query_cursor_set_byte_range(query, from, to)
138 +
139 + def query_cursor_set_byte_range(_, _, _) do
140 + :erlang.nif_error(:nif_not_loaded)
141 + end
130 142 end
131 143
132 144 defmodule TreeSitter.Application do
  @@ -0,0 +1,9 @@
1 + defmodule TreeSitter.Node do
2 + @doc """
3 + Extract the content of the node from the given source_code/content
4 + """
5 + def content(node, content) do
6 + {s, e} = TreeSitter.Nif.node_byte_range(node)
7 + Kernel.binary_part(content, s, e - s)
8 + end
9 + end
  @@ -0,0 +1,25 @@
1 + defmodule TreeSitter.QueryCursor do
2 + @doc """
3 + Create a Stream using the given cursor and query
4 + """
5 + def to_stream(cursor, query) do
6 + Stream.unfold(cursor, fn cursor ->
7 + case TreeSitter.Nif.query_cursor_next_match(cursor) do
8 + {:error, _} ->
9 + nil
10 +
11 + {:ok, {_, pattern_index, nodes}} ->
12 + case TreeSitter.Nif.query_capture_name_for_id(query, pattern_index) do
13 + {:ok, lbl} ->
14 + {{
15 + to_string(lbl),
16 + nodes
17 + }, cursor}
18 +
19 + {:error, _} ->
20 + nil
21 + end
22 + end
23 + end)
24 + end
25 + end
Loading more files…