You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 18, 2024. It is now read-only.
Android Java bindings for [tree-sitter](https://tree-sitter.github.io/tree-sitter/). Currently used in [AndroidIDE](https://github.com/AndroidIDEOfficial/AndroidIDE)
The following grammars have been published to Maven central :
22
+
20
23
- Java
21
24
- JSON
22
25
- Kotlin
@@ -30,18 +33,31 @@ The following grammars have been published to Maven central :
30
33
31
34
- Android NDK
32
35
-`JDK 11` or newer.
33
-
-`JAVA_HOME` must be set to a JDK installation containing header files in the `JAVA_HOME/include` directory. The JDK bundled with Android Studio does not contain these headers.
34
-
-`build-essential`, `cargo`, `nodejs`, `cmake` and `ninja-build` packages are required for builds. Install them with :
36
+
-`JAVA_HOME` must be set to a JDK installation containing header files in
37
+
the `JAVA_HOME/include` directory. The JDK bundled with Android Studio does not contain these
38
+
headers.
39
+
-`build-essential`, `nodejs`, `cmake` and `ninja-build` packages are required for builds.
-[`tree-sitter-cli`](https://github.com/tree-sitter/tree-sitter/tree/master/cli) - To build grammars.
39
44
40
-
As tree-sitter is already included in the source (as a submodule), the `tree-sitter-cli` is built from source and then used to build the grammars.
45
+
- You'll also need to install `rust` and `cargo` (>= v1.74.0). Follow the
46
+
instructions [here](https://www.rust-lang.org/learn/get-started) to get started with the
47
+
installation.
48
+
49
+
-[`tree-sitter-cli`](https://github.com/tree-sitter/tree-sitter/tree/master/cli) - To build
50
+
grammars.
41
51
42
-
Read [the documentation](https://github.com/tree-sitter/tree-sitter/tree/master/cli) on how to install pre-built versions of `tree-sitter`.
52
+
As tree-sitter is already included in the source (as a submodule), the `tree-sitter-cli` is built
53
+
from source and then used to build the grammars.
43
54
44
-
If you want to use a prebuilt `tree-sitter` binary (either manually installed or installed with `npm` or `cargo`), make sure it is accessible in the `PATH`, then set the `TS_CLI_BUILD_FROM_SOURCE` environment variable :
55
+
Read [the documentation](https://github.com/tree-sitter/tree-sitter/tree/master/cli) on how to
56
+
install pre-built versions of `tree-sitter`.
57
+
58
+
If you want to use a prebuilt `tree-sitter` binary (either manually installed or installed
59
+
with `npm` or `cargo`), make sure it is accessible in the `PATH`, then set
60
+
the `TS_CLI_BUILD_FROM_SOURCE` environment variable :
A normal Gradle build (`./gradlew build`) can be executed in order to build everything for Android _and_ the host OS. To build `android-tree-sitter` and the grammars _only_ for the host OS, you can execute `buildForHost` task on appropriate subprojects.
78
+
A normal Gradle build (`./gradlew build`) can be executed in order to build everything for Android
79
+
_and_ the host OS. To build `android-tree-sitter` and the grammars _only_ for the host OS, you can
80
+
execute `buildForHost` task on appropriate subprojects.
63
81
64
82
## Adding grammars
65
83
66
-
The Gradle modules for the grammars are almost identical, with only minor differences in the CMakeLists file and the Java binding class.
84
+
The Gradle modules for the grammars are almost identical, with only minor differences in the
85
+
CMakeLists file and the Java binding class.
67
86
68
-
These Gradle modules are automatically generated by the [`DynamicModulePlugin`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter/DynamicModulePlugin.kt). The generation process relies on the [`grammars.json`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/grammars/grammars.json) file. More information about the structure of this JSON file can be found in the [README](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/dev/grammars/README.md) under the `grammars` directory.
87
+
These Gradle modules are automatically generated by
88
+
the [`DynamicModulePlugin`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter/DynamicModulePlugin.kt).
89
+
The generation process relies on
90
+
the [`grammars.json`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/grammars/grammars.json)
91
+
file. More information about the structure of this JSON file can be found in
92
+
the [README](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/dev/grammars/README.md)
93
+
under the `grammars` directory.
69
94
70
-
Apart from the `DynamicModulePlugin`, there are [other Gradle plugins](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter) which are used to configure and build the grammars effectively.
95
+
Apart from the `DynamicModulePlugin`, there
96
+
are [other Gradle plugins](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter)
97
+
which are used to configure and build the grammars effectively.
71
98
72
-
The common configuration for the grammars can be found in the [`build.gradle.kts`](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/52cc0400feee5079cac25b27d1e7b673ee53f436/build.gradle.kts#L136) file. This is where you can make changes or adjustments to the module configuration that applies to all grammars.
99
+
The common configuration for the grammars can be found in
100
+
the [`build.gradle.kts`](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/52cc0400feee5079cac25b27d1e7b673ee53f436/build.gradle.kts#L136)
101
+
file. This is where you can make changes or adjustments to the module configuration that applies to
102
+
all grammars.
73
103
74
-
The generated modules are located in the `rootDir/grammar-modules` directory. This is where you can find the output of the module generation process.
104
+
The generated modules are located in the `rootDir/grammar-modules` directory. This is where you can
105
+
find the output of the module generation process.
75
106
76
107
To add a new grammar to the project, follow these steps:
77
108
78
-
1. Begin by adding the grammar source code to the `grammars` directory. To accomplish this, you can add a submodule using the following command:
2. The `language_name` should be the simple name of the language, without the `tree-sitter-` prefix. This name is used to generate both the shared library and the Gradle module. For example, if the `language_name` is `abc`:
109
+
1. Begin by adding the grammar source code to the `grammars` directory. To accomplish this, you can
2. The `language_name` should be the simple name of the language, without the `tree-sitter-` prefix.
117
+
This name is used to generate both the shared library and the Gradle module. For example, if
118
+
the `language_name` is `abc`:
83
119
- The module `tree-sitter-abc` will be automatically generated.
84
120
- The name of the resulting shared library will be `libtree-sitter-abc.so`.
85
-
3. After adding the grammar source, update the `grammars.json` file to include the newly added grammar in the project.
121
+
3. After adding the grammar source, update the `grammars.json` file to include the newly added
122
+
grammar in the project.
86
123
4. Finally, sync the project to trigger the generation of the module for the newly added grammar.
87
124
88
125
## Loading external grammars
@@ -92,16 +129,19 @@ You have two ways to load grammars that are not published along with this projec
92
129
- Package the grammar with your application.
93
130
- Load the grammar at runtime using `TSLanguage.loadLanguage`.
94
131
95
-
`TSLanguage.loadLanguage` uses `dlopen` to load the library and must be CAREFULLY used. Also, grammars that are loaded using this method must be closed when they are not used.
132
+
`TSLanguage.loadLanguage` uses `dlopen` to load the library and must be CAREFULLY used. Also,
133
+
grammars that are loaded using this method must be closed when they are not used.
96
134
97
135
>**_Prefer using the first method whenever possible._**
98
136
99
137
### Package the grammar with your application
100
138
101
-
You can package the grammar in your Android application as you would package any other shared library :
139
+
You can package the grammar in your Android application as you would package any other shared
140
+
library :
102
141
103
142
- Include the `libtree-sitter-myLang.so` file in the `jniLibs` directory of your project.
104
143
- Create a native method in a Java class which will return the pointer to the language :
final TSLanguage myLang = TSLanguage.create("myLang",MyClass.myLang());
174
+
final TSLanguage myLang=TSLanguage.create("myLang",MyClass.myLang());
134
175
135
176
// use it with TSParser
136
-
try (final var parser = TSParser.create()){
177
+
try(final var parser=TSParser.create()){
137
178
parser.setLanguage(myLang);
138
179
...
139
-
}
180
+
}
140
181
```
141
182
142
183
### Load grammars at runtime
143
184
144
185
> android-tree-sitter `v3.1.0` or newer is required for this method.
145
186
146
-
`TSLanguage` provides `loadLanguage(String, String)` method which can be used to load the grammars at runtime. This method uses `dlopen` to load the shared library, get the language instance and return its pointer. Use this method CAREFULLY.
187
+
`TSLanguage` provides `loadLanguage(String, String)` method which can be used to load the grammars
188
+
at runtime. This method uses `dlopen` to load the shared library, get the language instance and
189
+
return its pointer. Use this method CAREFULLY.
147
190
148
-
The language instances created using this method **MUST** be closed using `TSLanguage.close()`. Calling the `close` method ensures that the underlying `dlopen`'ed library handle is closed using `dlclose`.
191
+
The language instances created using this method **MUST** be closed using `TSLanguage.close()`.
192
+
Calling the `close` method ensures that the underlying `dlopen`'ed library handle is closed
193
+
using `dlclose`.
149
194
150
195
Usage :
196
+
151
197
```java
152
198
// provide the path to the shared library and the name of the language
153
199
// the name is used to cache the language instance
154
200
// further invocations of this method with the same lang name returns the
final TSLanguage cachedMyLang=TSLanguageCache.get("myLang");
178
227
```
179
228
180
229
**DO NOT FORGET** to close the language :
230
+
181
231
```java
182
232
// this closes the underlying library handle
183
233
myLang.close();
184
234
```
185
235
186
236
## Examples
187
237
188
-
For examples, see the [tests](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter).
238
+
For examples, see
239
+
the [tests](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter).
0 commit comments