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

Commit 75a8a06

Browse files
committed
docs: add instructions about Rust setup and version requirements
1 parent 4045f21 commit 75a8a06

File tree

1 file changed

+88
-37
lines changed

1 file changed

+88
-37
lines changed

README.md

Lines changed: 88 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# android-tree-sitter
2-
<a href="https://github.com/itsaky/AndroidIDE"><img src="https://androidide.com/github/img/androidide.php?part&for-the-badge"/></a><br><br>
3-
Android Java bindings for [tree-sitter](https://tree-sitter.github.io/tree-sitter/). Currently used in [AndroidIDE](https://github.com/AndroidIDEOfficial/AndroidIDE)
2+
3+
<a href="https://github.com/itsaky/AndroidIDE"><img src="https://androidide.com/github/img/androidide.php?part&for-the-badge"/></a><br><br>
4+
Android Java bindings for [tree-sitter](https://tree-sitter.github.io/tree-sitter/). Currently used
5+
in [AndroidIDE](https://github.com/AndroidIDEOfficial/AndroidIDE)
46
and [sora-editor](https://github.com/Rosemoe/sora-editor).
57

68
## Add to your project
@@ -17,6 +19,7 @@ implementation 'com.itsaky.androidide.treesitter:tree-sitter-<language>:<version
1719
```
1820

1921
The following grammars have been published to Maven central :
22+
2023
- Java
2124
- JSON
2225
- Kotlin
@@ -30,18 +33,31 @@ The following grammars have been published to Maven central :
3033

3134
- Android NDK
3235
- `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.
40+
Install them with :
3541
```
36-
sudo apt install build-essential cargo nodejs cmake ninja-build
42+
sudo apt install build-essential nodejs cmake ninja-build
3743
```
38-
- [`tree-sitter-cli`](https://github.com/tree-sitter/tree-sitter/tree/master/cli) - To build grammars.
3944

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.
4151

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.
4354

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 :
4561

4662
```bash
4763
export TS_CLI_BUILD_FROM_SOURCE=false
@@ -59,30 +75,51 @@ git clone --recurse-submodules https://github.com/AndroidIDEOfficial/android-tre
5975

6076
### Build
6177

62-
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.
6381

6482
## Adding grammars
6583

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.
6786

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.
6994

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.
7198

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.
73103

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.
75106

76107
To add a new grammar to the project, follow these steps:
77108

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:
79-
```bash
80-
git submodule add <remote_url> grammars/<language_name>
81-
```
82-
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
110+
add a submodule using the following command:
111+
112+
```bash
113+
git submodule add <remote_url> grammars/<language_name>
114+
```
115+
116+
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`:
83119
- The module `tree-sitter-abc` will be automatically generated.
84120
- 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.
86123
4. Finally, sync the project to trigger the generation of the module for the newly added grammar.
87124

88125
## Loading external grammars
@@ -92,16 +129,19 @@ You have two ways to load grammars that are not published along with this projec
92129
- Package the grammar with your application.
93130
- Load the grammar at runtime using `TSLanguage.loadLanguage`.
94131

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.
96134

97135
> **_Prefer using the first method whenever possible._**
98136

99137
### Package the grammar with your application
100138

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 :
102141

103142
- Include the `libtree-sitter-myLang.so` file in the `jniLibs` directory of your project.
104143
- Create a native method in a Java class which will return the pointer to the language :
144+
105145
```java
106146
package com.my.app;
107147
@@ -116,6 +156,7 @@ public class MyClass {
116156
```
117157

118158
- Write the C/C++ implementation for the method :
159+
119160
```c++
120161
extern "C" TSLanguage *tree_sitter_myLang();
121162
@@ -130,62 +171,72 @@ Java_com_my_app_MyClass_myLang(JNIEnv *env, jclass clazz) {
130171
- Create and use the `TSLanguage` instance :
131172

132173
```java
133-
final TSLanguage myLang = TSLanguage.create("myLang", MyClass.myLang());
174+
final TSLanguage myLang=TSLanguage.create("myLang",MyClass.myLang());
134175
135176
// use it with TSParser
136-
try (final var parser = TSParser.create()) {
177+
try(final var parser=TSParser.create()){
137178
parser.setLanguage(myLang);
138179
...
139-
}
180+
}
140181
```
141182

142183
### Load grammars at runtime
143184

144185
> android-tree-sitter `v3.1.0` or newer is required for this method.
145186

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.
147190

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`.
149194
150195
Usage :
196+
151197
```java
152198
// provide the path to the shared library and the name of the language
153199
// the name is used to cache the language instance
154200
// further invocations of this method with the same lang name returns the
155201
// cached language instance
156-
final TSLanguage myLang = TSLanguage.loadLanguage("/path/to/libtree-sitter-myLang.so", "myLang");
202+
final TSLanguage myLang=TSLanguage.loadLanguage("/path/to/libtree-sitter-myLang.so","myLang");
157203
158-
if (myLang != null) {
204+
if(myLang!=null){
159205
// loaded successfully
160-
} else {
206+
}else{
161207
// failed to load the language
162208
// see logcat for details
163-
}
209+
}
164210
```
165211
166212
Use this language :
213+
167214
```java
168-
try (final var parser = TSParser.create()) {
215+
try(final var parser=TSParser.create()){
169216
parser.setLanguage(myLang);
170217
...
171-
}
218+
}
172219
```
173220
174-
You don't have to keep a reference to `myLang`. Once loaded, the language can be accessed using `TSLanguageCache` :
221+
You don't have to keep a reference to `myLang`. Once loaded, the language can be accessed
222+
using `TSLanguageCache` :
223+
175224
```java
176225
// returns the 'myLang' instance i.e. both are same
177-
final TSLanguage cachedMyLang = TSLanguageCache.get("myLang");
226+
final TSLanguage cachedMyLang=TSLanguageCache.get("myLang");
178227
```
179228
180229
**DO NOT FORGET** to close the language :
230+
181231
```java
182232
// this closes the underlying library handle
183233
myLang.close();
184234
```
185235
186236
## Examples
187237
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).
189240
190241
## License
191242

0 commit comments

Comments
 (0)