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

Commit ee09141

Browse files
committed
Moved native methods for TSParser to TSParser.Native class
1 parent 0856fb6 commit ee09141

File tree

7 files changed

+153
-163
lines changed

7 files changed

+153
-163
lines changed

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

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,96 @@ public TSParser(long pointer) {
1010
}
1111

1212
public TSParser() {
13-
this(TreeSitter.newParser());
13+
this(Native.newParser());
1414
}
1515

16+
/**
17+
* Set the language of the given parser.
18+
*
19+
* @param language The language to set.
20+
* @see TSLanguage
21+
* @see TSLanguages
22+
*/
1623
public void setLanguage(TSLanguage language) {
17-
TreeSitter.parserSetLanguage(pointer, language.pointer);
24+
Native.setLanguage(pointer, language.pointer);
1825
}
1926

27+
/**
28+
* Parses the given String source. Uses {@link TSInputEncoding#TSInputEncodingUTF8} as the default
29+
* encoding.
30+
*
31+
* @param source The source code to parse.
32+
* @return The parsed tree.
33+
* @throws UnsupportedEncodingException
34+
*/
2035
public TSTree parseString(String source) throws UnsupportedEncodingException {
2136
return parseString(source, TSInputEncoding.TSInputEncodingUTF8);
2237
}
2338

24-
public TSTree parseString(String source, TSInputEncoding encoding) throws UnsupportedEncodingException {
39+
/**
40+
* Parses the given String source with the given encoding.
41+
*
42+
* @param source The source code to parse.
43+
* @param encoding The encoding to of the source.
44+
* @return The parsed tree.
45+
* @throws UnsupportedEncodingException
46+
*/
47+
public TSTree parseString(String source, TSInputEncoding encoding)
48+
throws UnsupportedEncodingException {
2549
byte[] bytes = source.getBytes(encoding.getCharset());
26-
return new TSTree(TreeSitter.parserParseBytes(pointer, bytes, bytes.length, encoding.getFlag()));
50+
return new TSTree(Native.parseBytes(pointer, bytes, bytes.length, encoding.getFlag()));
2751
}
2852

53+
/**
54+
* Parses the given bytes.
55+
*
56+
* @param bytes The bytes to parse.
57+
* @param bytesLength The length of bytes to parse.
58+
* @param encodingFlag The encoding of the source.
59+
* @return The parsed tree.
60+
*/
2961
public TSTree parseBytes(byte[] bytes, int bytesLength, int encodingFlag) {
30-
return new TSTree(TreeSitter.parserParseBytes(pointer, bytes, bytesLength, encodingFlag));
62+
return new TSTree(Native.parseBytes(pointer, bytes, bytesLength, encodingFlag));
3163
}
3264

65+
/**
66+
* @see #parseString(TSTree, String, TSInputEncoding)
67+
*/
3368
public TSTree parseString(TSTree oldTree, String source) throws UnsupportedEncodingException {
3469
return parseString(oldTree, source, TSInputEncoding.TSInputEncodingUTF8);
3570
}
3671

37-
public TSTree parseString(TSTree oldTree, String source, TSInputEncoding encoding) throws UnsupportedEncodingException {
72+
/**
73+
* Parses the given string source code.
74+
*
75+
* @param oldTree If earlier version of the same document has been parsed and you intend to do an
76+
* incremental parsing, then this should be the earlier parsed syntax tree. Otherwise <code>
77+
* null</code>.
78+
* @param source The source code to parse.
79+
* @param encoding The encoding of the source code.
80+
* @return The parsed tree.
81+
* @throws UnsupportedEncodingException
82+
*/
83+
public TSTree parseString(TSTree oldTree, String source, TSInputEncoding encoding)
84+
throws UnsupportedEncodingException {
3885
byte[] bytes = source.getBytes(encoding.getCharset());
39-
return new TSTree(TreeSitter.parserIncrementalParseBytes(pointer, oldTree.getPointer(), bytes, bytes.length, encoding.getFlag()));
86+
return new TSTree(
87+
Native.incrementalParseBytes(
88+
pointer, oldTree.getPointer(), bytes, bytes.length, encoding.getFlag()));
4089
}
4190

91+
/** Closes and deletes the current parser. */
4292
@Override
4393
public void close() {
44-
TreeSitter.parserDelete(pointer);
94+
Native.delete(pointer);
95+
}
96+
97+
private static class Native {
98+
public static native long newParser();
99+
public static native void delete(long parser);
100+
public static native void setLanguage(long parser, long language);
101+
public static native long parseBytes(long parser, byte[] source, int length, int encoding);
102+
public static native long incrementalParseBytes(
103+
long parser, long old_tree, byte[] source, int length, int encoding);
45104
}
46105
}

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

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@
22

33
public class TSTree implements AutoCloseable {
44

5-
private long pointer;
6-
7-
TSTree(long pointer) {
8-
this.pointer = pointer;
9-
}
10-
11-
@Override
12-
public void close() {
13-
TreeSitter.treeDelete(pointer);
14-
}
15-
16-
public TSNode getRootNode() {
17-
return TreeSitter.treeRootNode(pointer);
18-
}
19-
20-
public TSTree copy() {
21-
return new TSTree(TreeSitter.treeCopy(pointer));
22-
}
23-
24-
public void edit(TSInputEdit edit) {
25-
TreeSitter.treeEdit(
26-
pointer,
27-
edit
28-
);
29-
}
30-
31-
public long getPointer() {
32-
return pointer;
33-
}
5+
private final long pointer;
6+
7+
TSTree(long pointer) {
8+
this.pointer = pointer;
9+
}
10+
11+
@Override
12+
public void close() {
13+
TreeSitter.treeDelete(pointer);
14+
}
15+
16+
public TSNode getRootNode() {
17+
return TreeSitter.treeRootNode(pointer);
18+
}
19+
20+
public TSTree copy() {
21+
return new TSTree(TreeSitter.treeCopy(pointer));
22+
}
23+
24+
public void edit(TSInputEdit edit) {
25+
TreeSitter.treeEdit(pointer, edit);
26+
}
27+
28+
public long getPointer() {
29+
return pointer;
30+
}
3431
}

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

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,85 +6,6 @@ public class TreeSitter {
66
// ---------- Section: TSParser --------------
77
// -------------------------------------------
88

9-
/**
10-
* Create a new parser instance.
11-
*
12-
* @return The pointer to the new parser.
13-
*/
14-
public static native long newParser();
15-
16-
/**
17-
* Delete the parser.
18-
*
19-
* @param parser The pointer to the parser to delete.
20-
*/
21-
public static native void parserDelete(long parser);
22-
23-
/**
24-
* Set the language of the given parser.
25-
*
26-
* @param parser The pointer of the parser.
27-
* @param language The language to set.
28-
* @see TSLanguages
29-
*/
30-
public static native void parserSetLanguage(long parser, long language);
31-
32-
/**
33-
* Parse the given source with the given parser.
34-
*
35-
* @param parser The pointer of the parser to use.
36-
* @param source The source code.
37-
* @param length The length of the source code.
38-
* @return The pointer to the parsed {@link TSTree}.
39-
* @see #parserParseBytes(long, byte[], int, int)
40-
*/
41-
public static long parserParseBytes(long parser, byte[] source, int length) {
42-
return parserParseBytes(parser, source, length, TSInputEncoding.TSInputEncodingUTF8.getFlag());
43-
}
44-
45-
/**
46-
* Use the parser to parse the source code and create a new syntax tree.
47-
*
48-
* @param parser The pointer to the parser.
49-
* @param old_tree The pointer to the old syntax tree.
50-
* @param source The source code.
51-
* @param length The length of the source code.
52-
* @return The pointer to the new {@link TSTree}.
53-
* @see #parserIncrementalParseBytes(long, long, byte[], int, int)
54-
* @see TSInputEncoding
55-
*/
56-
public static long parserIncrementalParseBytes(
57-
long parser, long old_tree, byte[] source, int length) {
58-
return parserIncrementalParseBytes(
59-
parser, old_tree, source, length, TSInputEncoding.TSInputEncodingUTF8.getFlag());
60-
}
61-
62-
/**
63-
* Parse the given source with the given parser.
64-
*
65-
* @param parser The pointer of the parser to use.
66-
* @param source The source code.
67-
* @param length The length of the source code.
68-
* @param encoding The encoding of the source code.
69-
* @return The pointer to the parsed {@link TSTree}.
70-
* @see TSInputEncoding
71-
*/
72-
public static native long parserParseBytes(long parser, byte[] source, int length, int encoding);
73-
74-
/**
75-
* Use the parser to parse the source code and create a new syntax tree.
76-
*
77-
* @param parser The pointer to the parser.
78-
* @param old_tree The pointer to the old syntax tree.
79-
* @param source The source code.
80-
* @param length The length of the source code.
81-
* @param encoding The encoding of the source code.
82-
* @return The pointer to the new {@link TSTree}.
83-
* @see TSInputEncoding
84-
*/
85-
public static native long parserIncrementalParseBytes(
86-
long parser, long old_tree, byte[] source, int length, int encoding);
87-
889
// -------------------------------------------
8910
// ---------- Section: TSTreeCursor ----------
9011
// -------------------------------------------

genh.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -eu
44

55
script_dir=$(realpath $(dirname $0))
66

7-
for header in TreeSitter TSLanguages.Native TSNode TSLanguage.Native
7+
for header in TreeSitter TSLanguages.Native TSLanguage.Native TSParser.Native TSNode
88
do
99
javah -d $script_dir/lib -classpath ${script_dir}/android-tree-sitter/src/main/java com.itsaky.androidide.treesitter.${header}
1010
done

lib/com_itsaky_androidide_treesitter_TSParser_Native.h

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/com_itsaky_androidide_treesitter_TreeSitter.h

Lines changed: 0 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts_parser.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
#include "com_itsaky_androidide_treesitter_TreeSitter.h"
1+
#include "com_itsaky_androidide_treesitter_TSParser_Native.h"
22
#include "ts_utils.h"
33

44
JNIEXPORT jlong JNICALL
5-
Java_com_itsaky_androidide_treesitter_TreeSitter_newParser(JNIEnv* env, jclass self) {
5+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_newParser(JNIEnv* env, jclass self) {
66
return (jlong)ts_parser_new();
77
}
88

9-
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_parserDelete(
9+
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TSParser_00024Native_delete(
1010
JNIEnv* env, jclass self, jlong parser) {
1111
ts_parser_delete((TSParser*)parser);
1212
}
1313

14-
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_parserSetLanguage(
14+
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TSParser_00024Native_setLanguage(
1515
JNIEnv* env, jclass self, jlong parser, jlong language) {
1616
ts_parser_set_language((TSParser*)parser, (TSLanguage*)language);
1717
}
1818

19-
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_parserParseBytes(
19+
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TSParser_00024Native_parseBytes(
2020
JNIEnv* env, jclass self, jlong parser, jbyteArray source_bytes,
2121
jint length, jint encodingFlag) {
2222
TSInputEncoding encoding = encodingFlag == 0 ? TSInputEncodingUTF8 : TSInputEncodingUTF16;
@@ -27,7 +27,7 @@ JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_parserP
2727
return result;
2828
}
2929

30-
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_parserIncrementalParseBytes(
30+
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TSParser_00024Native_incrementalParseBytes(
3131
JNIEnv* env, jclass self, jlong parser, jlong old_tree, jbyteArray source_bytes,
3232
jint length, jint encodingFlag) {
3333
TSInputEncoding encoding = encodingFlag == 0 ? TSInputEncodingUTF8 : TSInputEncodingUTF16;

0 commit comments

Comments
 (0)