Class: TreeSitter::QueryCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_sitter/query_cursor.rb,
ext/tree_sitter/query_cursor.c

Overview

A Cursor for Query.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execObject

Class methods

Instance Method Details

#captures(query, node, src) ⇒ Object

Iterate over all of the individual captures in the order that they appear.

This is useful if you don’t care about which pattern matched, and just want a single, ordered sequence of captures.



22
23
24
25
# File 'lib/tree_sitter/query_cursor.rb', line 22

def captures(query, node, src)
  self.exec(query, node)
  QueryCaptures.new(self, query, src)
end

#exceed_match_limit?Boolean

Manage the maximum number of in-progress matches allowed by this query cursor.

Query cursors have an optional maximum capacity for storing lists of in-progress captures. If this capacity is exceeded, then the earliest-starting match will silently be dropped to make room for further matches. This maximum capacity is optional — by default, query cursors allow any number of pending matches, dynamically allocating new space for them as needed as the query is executed.

Returns:

  • (Boolean)


86
87
88
# File 'ext/tree_sitter/query_cursor.c', line 86

static VALUE query_cursor_did_exceed_match_limit(VALUE self) {
  return ts_query_cursor_did_exceed_match_limit(SELF) ? Qtrue : Qfalse;
}

#exec(query, node) ⇒ QueryCursor

Start running a given query on a given node.

Parameters:

Returns:



68
69
70
71
72
73
# File 'ext/tree_sitter/query_cursor.c', line 68

static VALUE query_cursor_exec(VALUE self, VALUE query, VALUE node) {
  query_cursor_t *query_cursor = unwrap(self);
  ts_query_cursor_exec(query_cursor->data, value_to_query(query),
                       value_to_node(node));
  return self;
}

#matches(query, node, src) ⇒ Object

Iterate over all of the matches in the order that they were found.

Each match contains the index of the pattern that matched, and a list of captures. Because multiple patterns can match the same set of nodes, one match may contain captures that appear before some of the captures from a previous match.



12
13
14
15
# File 'lib/tree_sitter/query_cursor.rb', line 12

def matches(query, node, src)
  self.exec(query, node)
  QueryMatches.new(self, query, src)
end

#max_start_depth=(max_start_depth) ⇒ nil

Set the maximum start depth for a query cursor.

This prevents cursors from exploring children nodes at a certain depth. Note if a pattern includes many children, then they will still be checked.

The zero max start depth value can be used as a special behavior and it helps to destructure a subtree by staying on a node and using captures for interested parts. Note that the zero max start depth only limit a search depth for a pattern’s root node but other nodes that are parts of the pattern may be searched at any depth what defined by the pattern structure.

depth.

Parameters:

  • max_start_depth (Integer|nil)

    set to nil to remove the maximum start

Returns:

  • (nil)


124
125
126
127
128
129
130
131
132
# File 'ext/tree_sitter/query_cursor.c', line 124

static VALUE query_cursor_set_max_start_depth(VALUE self,
                                              VALUE max_start_depth) {
  uint32_t max = UINT32_MAX;
  if (!NIL_P(max_start_depth)) {
    max = NUM2UINT(max_start_depth);
  }
  ts_query_cursor_set_max_start_depth(SELF, max);
  return Qnil;
}

#next_captureArray<Integer|Boolean>|nil

Advance to the next capture of the currently running query.

[Integer, Boolean], otherwise return nil.

Returns:

  • (Array<Integer|Boolean>|nil)

    If there is a capture, return a tuple



147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/tree_sitter/query_cursor.c', line 147

static VALUE query_cursor_next_capture(VALUE self) {
  TSQueryMatch match;
  uint32_t index;
  if (ts_query_cursor_next_capture(SELF, &match, &index)) {
    VALUE res = rb_ary_new_capa(2);
    rb_ary_push(res, UINT2NUM(index));
    rb_ary_push(res, new_query_match(&match));
    return res;
  } else {
    return Qnil;
  }
}

#next_matchBoolean

Advance to the next match of the currently running query.

Returns:

  • (Boolean)

    Whether there’s a match.



165
166
167
168
169
170
171
172
# File 'ext/tree_sitter/query_cursor.c', line 165

static VALUE query_cursor_next_match(VALUE self) {
  TSQueryMatch match;
  if (ts_query_cursor_next_match(SELF, &match)) {
    return new_query_match(&match);
  } else {
    return Qnil;
  }
}

#remove_match(id) ⇒ Object



174
175
176
177
# File 'ext/tree_sitter/query_cursor.c', line 174

static VALUE query_cursor_remove_match(VALUE self, VALUE id) {
  ts_query_cursor_remove_match(SELF, NUM2UINT(id));
  return Qnil;
}

#set_byte_range(from, to) ⇒ nil

Parameters:

  • from (Integer)
  • to (Integer)

Returns:

  • (nil)


185
186
187
188
# File 'ext/tree_sitter/query_cursor.c', line 185

static VALUE query_cursor_set_byte_range(VALUE self, VALUE from, VALUE to) {
  ts_query_cursor_set_byte_range(SELF, NUM2UINT(from), NUM2UINT(to));
  return Qnil;
}

#set_point_range(from, to) ⇒ nil

Parameters:

Returns:

  • (nil)


196
197
198
199
200
# File 'ext/tree_sitter/query_cursor.c', line 196

static VALUE query_cursor_set_point_range(VALUE self, VALUE from, VALUE to) {
  ts_query_cursor_set_point_range(SELF, value_to_point(from),
                                  value_to_point(to));
  return Qnil;
}