Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 6c95b2e

Browse files
committed
Added API to get capture names from TSQuery
1 parent 9a1c0ef commit 6c95b2e

File tree

2 files changed

+21
-1
lines changed
  • android-tree-sitter/src

2 files changed

+21
-1
lines changed

android-tree-sitter/src/main/java/com/itsaky/androidide/treesitter/TSQuery.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
public class TSQuery implements AutoCloseable {
2121
final long pointer;
22-
2322
private int errorOffset;
2423
private int errorType;
2524

25+
private String[] captureNames = null;
26+
2627
/**
2728
* Create a new query from a string containing one or more S-expression patterns. The query is
2829
* associated with a particular language, and can only be run on syntax nodes parsed with that
@@ -72,6 +73,16 @@ public int getStringCount() {
7273
return Native.stringCount(this.pointer);
7374
}
7475

76+
public String[] getCaptureNames() {
77+
if (captureNames == null) {
78+
captureNames = new String[getCaptureCount()];
79+
for (int i = 0; i < getCaptureCount(); i++) {
80+
captureNames[i] = getCaptureNameForId(i);
81+
}
82+
}
83+
return captureNames;
84+
}
85+
7586
public int getStartByteForPattern(int pattern) {
7687
validatePatternIndex(pattern);
7788
return Native.startByteForPattern(this.pointer, pattern);

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/QueryTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public void queryTest() throws Exception {
5555
assertThat(match.getCaptures()).isNotNull();
5656
assertThat(match.getCaptures()).hasLength(1);
5757
assertThat(cursor.nextMatch()).isNull();
58+
59+
var captureNames = query.getCaptureNames();
60+
assertThat(captureNames).hasLength(query.getCaptureCount());
61+
captureNames = new String[query.getCaptureCount()];
62+
for (int i = 0; i < captureNames.length; i++) {
63+
captureNames[i] = query.getCaptureNameForId(0);
64+
}
65+
5866
query.close();
5967
cursor.close();
6068

@@ -66,6 +74,7 @@ public void queryTest() throws Exception {
6674
assertThat(match.getCaptures()).isNotNull();
6775
assertThat(match.getCaptures()).hasLength(1);
6876
assertThat(cursor.nextMatch()).isNull();
77+
6978
query.close();
7079
cursor.close();
7180
} catch (Throwable err) {

0 commit comments

Comments
 (0)