Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CapturesIterator(

@Override
public boolean tryAdvance(Consumer<? super SimpleImmutableEntry<Integer, QueryMatch>> action) {
var hasNoText = tree.getText() == null;
var hasNoText = !tree.hasText();
MemorySegment match = allocator.allocate(TSQueryMatch.layout());
MemorySegment index = allocator.allocate(C_INT);
var captureNames = query.getCaptureNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MatchesIterator(

@Override
public boolean tryAdvance(Consumer<? super QueryMatch> action) {
var hasNoText = tree.getText() == null;
var hasNoText = !tree.hasText();
MemorySegment match = allocator.allocate(TSQueryMatch.layout());
var captureNames = query.getCaptureNames();
while (ts_query_cursor_next_match(cursor, match)) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/github/treesitter/jtreesitter/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ MemorySegment segment() {
return self;
}

boolean hasText() {
return charset != null;
}

@Nullable
String getRegion(@Unsigned int start, @Unsigned int end) {
var length = Math.min(end, source.length) - start;
Expand All @@ -57,7 +61,7 @@ public Language getLanguage() {

/** Get the source code of the syntax tree, if available. */
public @Nullable String getText() {
return charset != null ? new String(source, charset) : null;
return hasText() ? new String(source, charset) : null;
}

/** Get the root node of the syntax tree. */
Expand Down Expand Up @@ -161,6 +165,6 @@ public void close() throws RuntimeException {

@Override
public String toString() {
return "Tree{language=%s, source=%s}".formatted(language, source);
return "Tree{language=%s, source=%s}".formatted(language, getText());
}
}
5 changes: 3 additions & 2 deletions src/test/java/io/github/treesitter/jtreesitter/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ void getPatternAssertions() {

@Test
void queryWithTwoPredicates() {
var source = """
var source =
"""
((identifier) @foo
(#eq? @foo "foo")
(#not-eq? @foo "bar"))
"""
.stripIndent();
.stripIndent();
assertQuery(source, query -> {
assertEquals(1, query.getPatternCount());
assertIterableEquals(List.of("foo"), query.getCaptureNames());
Expand Down
Loading