Package io.github.treesitter.jtreesitter
Class Query
java.lang.Object
io.github.treesitter.jtreesitter.Query
- All Implemented Interfaces:
AutoCloseable
A class that represents a set of patterns which match
nodes in a syntax tree.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
disableCapture
(String name) Disable a certain capture within a query.void
disablePattern
(int index) Disable a certain pattern within a query.int
endByteForPattern
(int index) Get the byte offset where the given pattern ends in the query's source.int
Deprecated.Get the names of the captures used in the query.getPatternAssertions
(int index, boolean positive) Get the property assertions for the given pattern index.int
Get the number of patterns in the query.getPatternSettings
(int index) Get the property settings for the given pattern index.Get the string literals used in the query.boolean
isPatternGuaranteedAtStep
(int offset) Check if a pattern is guaranteed to match once a given byte offset is reached.boolean
isPatternNonLocal
(int index) Check if the pattern with the given index is "non-local".boolean
isPatternRooted
(int index) Check if the pattern with the given index has a single root node.int
startByteForPattern
(int index) Get the byte offset where the given pattern starts in the query's source.toString()
-
Constructor Details
-
Query
Create a new query from a string containing one or more S-expression patterns.- Throws:
QueryError
- If an error occurred while creating the query.
-
-
Method Details
-
getPatternCount
Get the number of patterns in the query. -
getCaptureCount
Deprecated.UsegetCaptureNames().size()
instead.Get the number of captures in the query. -
getCaptureNames
-
getStringValues
-
disablePattern
Disable a certain pattern within a query.- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.- API Note:
- This prevents the pattern from matching and removes most of the overhead associated with the pattern. Currently, there is no way to undo this.
-
disableCapture
Disable a certain capture within a query.- Throws:
NoSuchElementException
- If the capture does not exist.- API Note:
- This prevents the capture from being returned in matches, and also avoids most resource usage associated with recording the capture. Currently, there is no way to undo this.
-
startByteForPattern
Get the byte offset where the given pattern starts in the query's source.- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.
-
endByteForPattern
Get the byte offset where the given pattern ends in the query's source.- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.- Since:
- 0.23.0
-
isPatternRooted
Check if the pattern with the given index has a single root node.- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.
-
isPatternNonLocal
Check if the pattern with the given index is "non-local".A non-local pattern has multiple root nodes and can match within a repeating sequence of nodes, as specified by the grammar. Non-local patterns disable certain optimizations that would otherwise be possible when executing a query on a specific range of a syntax tree.
- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.
-
isPatternGuaranteedAtStep
Check if a pattern is guaranteed to match once a given byte offset is reached.- Throws:
IndexOutOfBoundsException
- If the offset exceeds the source length.
-
getPatternSettings
public Map<String,Optional<String>> getPatternSettings(@Unsigned int index) throws IndexOutOfBoundsException Get the property settings for the given pattern index.Properties are set using the
#set!
directive.- Parameters:
index
- The index of a pattern within the query.- Returns:
- A map of property keys with optional values.
- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.
-
getPatternAssertions
public Map<String,Optional<String>> getPatternAssertions(@Unsigned int index, boolean positive) throws IndexOutOfBoundsException Get the property assertions for the given pattern index.Assertions are performed using the
#is?
(positive) and#is-not?
(negative) predicates.- Parameters:
index
- The index of a pattern within the query.positive
- Indicates whether to include positive or negative assertions.- Returns:
- A map of property keys with optional values.
- Throws:
IndexOutOfBoundsException
- If the index exceeds the pattern count.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
RuntimeException
-
toString
-
getCaptureNames().size()
instead.