ts::Query class

A query is a "pre-compiled" string of S-expression patterns.

Features not included (because we currently don't use them):

  • Predicates (evaluation is not handled by tree-sitter anyway):
    • ts_query_predicates_for_pattern
    • ts_query_step_is_definite

    Can't be copied because the underlying TSQuery can't be copied.

Constructors, destructors, conversion operators

Query(const Language&, std::string_view)
Create query from the given query string.
Query(Query&&) defaulted noexcept
Move constructor.

Public functions

auto operator=(Query&&) -> Query& defaulted noexcept
Move assignment operator.
auto raw() const -> const TSQuery*
Returns the raw Tree-Sitter query.
auto raw() -> TSQuery*
Returns the raw Tree-Sitter query.
auto pattern_count() const -> std::uint32_t
The number of patterns in the query.
auto capture_count() const -> std::uint32_t
The number of captures in the query.
auto string_count() const -> std::uint32_t
The number of string literals in the query.
auto start_byte_for_pattern(std::uint32_t) const -> std::uint32_t
The byte offset where the pattern starts in the query source.
auto capture_name_for_id(std::uint32_t id) const -> std::string_view
The name of one of the query's captures.
auto string_value_for_id(std::uint32_t id) const -> std::string_view
Get one of the query's string literals.
void disable_capture(std::string_view)
Disable a capture within a query.
void disable_pattern(std::uint32_t id)
Disable a pattern within a query.

Friends

void swap(Query& self, Query& other) noexcept
Swap function.

Function documentation

ts::Query::Query(const Language&, std::string_view)

Create query from the given query string.

const TSQuery* ts::Query::raw() const

Returns the raw Tree-Sitter query.

TSQuery* ts::Query::raw()

Returns the raw Tree-Sitter query.

std::uint32_t ts::Query::start_byte_for_pattern(std::uint32_t) const

The byte offset where the pattern starts in the query source.

Can be useful when combining queries.

std::string_view ts::Query::capture_name_for_id(std::uint32_t id) const

The name of one of the query's captures.

Each capture is associated with a numeric id based on the order that it appeared in the query's source.

std::string_view ts::Query::string_value_for_id(std::uint32_t id) const

Get one of the query's string literals.

Each string literal is associated with a numeric id based on the order that it appeared in the query's source.

void ts::Query::disable_capture(std::string_view)

Disable a capture within a query.

This prevents the capture from being returned in matches and avoid ressource usage.

void ts::Query::disable_pattern(std::uint32_t id)

Disable a pattern within a query.

This prevents the pattern from matching and removes most of the overhead.