defmodule TreeSitter.QueryCursor do @doc """ Create a Stream using the given cursor and query """ def to_stream(cursor, query) do Stream.unfold(cursor, fn cursor -> case TreeSitter.Nif.query_cursor_next_match(cursor) do {:error, _} -> nil {:ok, {_, pattern_index, nodes}} -> case TreeSitter.Nif.query_capture_name_for_id(query, pattern_index) do {:ok, lbl} -> {{ to_string(lbl), nodes }, cursor} {:error, _} -> nil end end end) end end