Class: TreeSitter::QueryMatches

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tree_sitter/query_matches.rb

Overview

A sequence of QueryMatch associated with a given QueryCursor.

Instance Method Summary collapse

Constructor Details

#initialize(cursor, query, src) ⇒ QueryMatches

Returns a new instance of QueryMatches.



8
9
10
11
12
# File 'lib/tree_sitter/query_matches.rb', line 8

def initialize(cursor, query, src)
  @cursor = cursor
  @query = query
  @src = src
end

Instance Method Details

#each {|match| ... } ⇒ Object

Iterator over matches.

Yield Parameters:



17
18
19
20
21
22
23
24
25
# File 'lib/tree_sitter/query_matches.rb', line 17

def each(&_block)
  return enum_for __method__ if !block_given?

  while match = @cursor.next_match
    if match.satisfies_text_predicate?(@query, @src)
      yield match
    end
  end
end

#each_capture_hash {|match| ... } ⇒ Object

Iterate over all the results presented as hashes of ‘capture name => node`.

Yield Parameters:



30
31
32
33
34
35
36
37
# File 'lib/tree_sitter/query_matches.rb', line 30

def each_capture_hash(&_block)
  # TODO: should we return [Array<Hash<Symbol, TreeSitter::Node]>>] instead?
  return enum_for __method__ if !block_given?

  each do |match|
    yield match.captures.to_h { |cap| [@query.capture_name_for_id(cap.index), cap.node] }
  end
end