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

Commit 5c61cff

Browse files
committed
feat(query): add support for 'ts_query_capture_quantifier_for_id'
1 parent 3e40d0b commit 5c61cff

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

android-tree-sitter/src/main/cpp/ts_query.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "utils/ts_obj_utils.h"
1919

2020
void fillQuery(JNIEnv*, jobject, uint32_t, TSQueryError);
21+
int query_quantifier_id(TSQuantifier quantifier);
2122
jint getErrorType(TSQueryError);
2223

2324
extern "C" JNIEXPORT jlong JNICALL
@@ -146,4 +147,27 @@ jint getErrorType(TSQueryError error) {
146147
case TSQueryErrorLanguage:
147148
return 6;
148149
}
150+
}
151+
extern "C"
152+
JNIEXPORT jint JNICALL
153+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_captureQuantifierForId(
154+
JNIEnv *env,
155+
jclass clazz,
156+
jlong query,
157+
jint pattern,
158+
jint capture) {
159+
160+
auto ts_query = (TSQuery*) query;
161+
auto quantifier = ts_query_capture_quantifier_for_id(ts_query, pattern, capture);
162+
return query_quantifier_id(quantifier);
163+
}
164+
165+
int query_quantifier_id(TSQuantifier quantifier) {
166+
switch (quantifier) {
167+
case TSQuantifierZero: return 0;
168+
case TSQuantifierZeroOrOne: return 1;
169+
case TSQuantifierZeroOrMore: return 2;
170+
case TSQuantifierOne: return 3;
171+
case TSQuantifierOneOrMore: return 4;
172+
}
149173
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of android-tree-sitter.
3+
*
4+
* android-tree-sitter library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* android-tree-sitter library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.treesitter;
19+
20+
/**
21+
* @author Akash Yadav
22+
*/
23+
public enum TSQuantifier {
24+
Zero(0), ZeroOrOne(1), ZeroOrMore(2), One(3), OneOrMore(4);
25+
26+
public final int id;
27+
28+
TSQuantifier(int id) {
29+
this.id = id;
30+
}
31+
32+
public static TSQuantifier forId(int id) {
33+
for (final var quantifier : values()) {
34+
if (quantifier.id == id) {
35+
return quantifier;
36+
}
37+
}
38+
throw new IllegalArgumentException("Invalid quantifier id: " + id);
39+
}
40+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ public String getStringValueForId(int id) {
160160
return Native.stringValueForId(this.pointer, id);
161161
}
162162

163+
public TSQuantifier getCaptureQuantifierForId(int pattern, int capture) {
164+
checkAccess();
165+
validatePatternIndex(pattern);
166+
return TSQuantifier.forId(Native.captureQuantifierForId(this.pointer, pattern, capture));
167+
}
168+
163169
@Override
164170
protected void closeNativeObj() {
165171
Native.delete(this.pointer);
@@ -273,5 +279,7 @@ private static class Native {
273279
public static native String captureNameForId(long query, int id);
274280

275281
public static native String stringValueForId(long query, int id);
282+
283+
public static native int captureQuantifierForId(long query, int pattern, int capture);
276284
}
277285
}

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

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public void testXmlBlocksQuery() throws Exception {
230230
@Test
231231
public void testEmptyQuery() throws Exception {
232232
try (final var query = TSQuery.EMPTY) {
233-
assertThat(query.isValid()).isFalse();
233+
assertThat(query.canAccess()).isFalse();
234234
assertThat(query.getPatternCount()).isEqualTo(0);
235235
assertThat(query.getCaptureCount()).isEqualTo(0);
236236
assertThat(query.getStringCount()).isEqualTo(0);
@@ -241,4 +241,129 @@ public void testEmptyQuery() throws Exception {
241241
public void testQueryConstructionError() throws Exception {
242242
new TSQuery(null, "not-empty").close();
243243
}
244+
245+
@Test
246+
public void testQueryQuantifierZero() {
247+
final var lang = TSLanguageJava.getInstance();
248+
try (final var parser = new TSParser()) {
249+
parser.setLanguage(lang);
250+
String javaSource = "public class Main { void a() {} void b() {} void c() {} void d() {} void e() {} }";
251+
String querySource = "(class_declaration name: (identifier) @class_name)";
252+
253+
try (final var tree = parser.parseString(javaSource); final var query = new TSQuery(lang,
254+
querySource); final var cursor = new TSQueryCursor()) {
255+
256+
assertThat(query.canAccess()).isTrue();
257+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.None);
258+
259+
cursor.exec(query, tree.getRootNode());
260+
261+
// pattern 0 -> method_declaration
262+
// capture 1 -> invalid capture id, result must be TSQuantifier.Zero
263+
final var quantifier = query.getCaptureQuantifierForId(0, 1);
264+
assertThat(quantifier).isNotNull();
265+
assertThat(quantifier).isEqualTo(TSQuantifier.Zero);
266+
}
267+
}
268+
}
269+
270+
@Test
271+
public void testQueryQuantifierZeroOrOne() {
272+
final var lang = TSLanguageJava.getInstance();
273+
try (final var parser = new TSParser()) {
274+
parser.setLanguage(lang);
275+
String javaSource = "public class Main { void a() {} void b() {} void c() {} void d() {} void e() {} }";
276+
String querySource = "(class_declaration name: (identifier)? @class_name)";
277+
278+
try (final var tree = parser.parseString(javaSource); final var query = new TSQuery(lang,
279+
querySource); final var cursor = new TSQueryCursor()) {
280+
281+
assertThat(query.canAccess()).isTrue();
282+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.None);
283+
284+
cursor.exec(query, tree.getRootNode());
285+
286+
// pattern 0 -> method_declaration
287+
// capture 0 -> @method_name
288+
final var quantifier = query.getCaptureQuantifierForId(0, 0);
289+
assertThat(quantifier).isNotNull();
290+
assertThat(quantifier).isEqualTo(TSQuantifier.ZeroOrOne);
291+
}
292+
}
293+
}
294+
295+
@Test
296+
public void testQueryQuantifierZeroOrMore() {
297+
final var lang = TSLanguageJava.getInstance();
298+
try (final var parser = new TSParser()) {
299+
parser.setLanguage(lang);
300+
String javaSource = "public class Main { void a() {} void b() {} void c() {} void d() {} void e() {} }";
301+
String querySource = "(method_declaration name: (identifier)* @method_name)";
302+
303+
try (final var tree = parser.parseString(javaSource); final var query = new TSQuery(lang,
304+
querySource); final var cursor = new TSQueryCursor()) {
305+
306+
assertThat(query.canAccess()).isTrue();
307+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.None);
308+
309+
cursor.exec(query, tree.getRootNode());
310+
311+
// pattern 0 -> method_declaration
312+
// capture 0 -> @method_name
313+
final var quantifier = query.getCaptureQuantifierForId(0, 0);
314+
assertThat(quantifier).isNotNull();
315+
assertThat(quantifier).isEqualTo(TSQuantifier.ZeroOrMore);
316+
}
317+
}
318+
}
319+
320+
@Test
321+
public void testQueryQuantifierOne() {
322+
final var lang = TSLanguageJava.getInstance();
323+
try (final var parser = new TSParser()) {
324+
parser.setLanguage(lang);
325+
String javaSource = "public class Main { void a() {} void b() {} void c() {} void d() {} void e() {} }";
326+
String querySource = "(method_declaration name: (_) @method_name)";
327+
328+
try (final var tree = parser.parseString(javaSource); final var query = new TSQuery(lang,
329+
querySource); final var cursor = new TSQueryCursor()) {
330+
331+
assertThat(query.canAccess()).isTrue();
332+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.None);
333+
334+
cursor.exec(query, tree.getRootNode());
335+
336+
// pattern 0 -> method_declaration
337+
// capture 0 -> @method_name
338+
final var quantifier = query.getCaptureQuantifierForId(0, 0);
339+
assertThat(quantifier).isNotNull();
340+
assertThat(quantifier).isEqualTo(TSQuantifier.One);
341+
}
342+
}
343+
}
344+
345+
@Test
346+
public void testQueryQuantifierOneOrMore() {
347+
final var lang = TSLanguageJava.getInstance();
348+
try (final var parser = new TSParser()) {
349+
parser.setLanguage(lang);
350+
String javaSource = "public class Main { void a() {} void b() {} void c() {} void d() {} void e() {} }";
351+
String querySource = "(method_declaration name: (identifier)+ @method_name)";
352+
353+
try (final var tree = parser.parseString(javaSource); final var query = new TSQuery(lang,
354+
querySource); final var cursor = new TSQueryCursor()) {
355+
356+
assertThat(query.canAccess()).isTrue();
357+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.None);
358+
359+
cursor.exec(query, tree.getRootNode());
360+
361+
// pattern 0 -> method_declaration
362+
// capture 0 -> @method_name
363+
final var quantifier = query.getCaptureQuantifierForId(0, 0);
364+
assertThat(quantifier).isNotNull();
365+
assertThat(quantifier).isEqualTo(TSQuantifier.OneOrMore);
366+
}
367+
}
368+
}
244369
}

0 commit comments

Comments
 (0)