{"id":15282563,"url":"https://github.com/androidideofficial/android-tree-sitter","last_synced_at":"2025-03-29T01:31:04.197Z","repository":{"id":46215911,"uuid":"515109940","full_name":"AndroidIDEOfficial/android-tree-sitter","owner":"AndroidIDEOfficial","description":"Tree Sitter for Android","archived":true,"fork":false,"pushed_at":"2024-10-18T06:12:59.000Z","size":16829,"stargazers_count":52,"open_issues_count":11,"forks_count":22,"subscribers_count":5,"default_branch":"dev","last_synced_at":"2025-03-28T14:51:44.242Z","etag":null,"topics":["android","android-ide","androidide","hacktoberfest","java","jni","tree-sitter"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AndroidIDEOfficial.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-18T09:07:29.000Z","updated_at":"2025-03-21T12:58:10.000Z","dependencies_parsed_at":"2023-10-11T04:58:47.094Z","dependency_job_id":"80a58887-bb79-4f81-9435-0687b36414d0","html_url":"https://github.com/AndroidIDEOfficial/android-tree-sitter","commit_stats":{"total_commits":449,"total_committers":4,"mean_commits":112.25,"dds":0.09576837416481065,"last_synced_commit":"9334675451f048ff1fda36547efb929e6fe23dc3"},"previous_names":["itsaky/android-tree-sitter"],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndroidIDEOfficial%2Fandroid-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndroidIDEOfficial%2Fandroid-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndroidIDEOfficial%2Fandroid-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndroidIDEOfficial%2Fandroid-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndroidIDEOfficial","download_url":"https://codeload.github.com/AndroidIDEOfficial/android-tree-sitter/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246125515,"owners_count":20727437,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-ide","androidide","hacktoberfest","java","jni","tree-sitter"],"created_at":"2024-09-30T14:26:56.913Z","updated_at":"2025-03-29T01:31:03.732Z","avatar_url":"https://github.com/AndroidIDEOfficial.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e [!WARNING]\n\u003e \n\u003e THIS PROJECT IS NOT MAINTAINED ANYMORE. \n\n# android-tree-sitter\n\n\u003ca href=\"https://github.com/itsaky/AndroidIDE\"\u003e\u003cimg src=\"https://androidide.com/github/img/androidide.php?part\u0026for-the-badge\"/\u003e\u003c/a\u003e\u003cbr\u003e\u003cbr\u003e\nAndroid Java bindings for [tree-sitter](https://tree-sitter.github.io/tree-sitter/). Currently used\nin [AndroidIDE](https://github.com/AndroidIDEOfficial/AndroidIDE)\nand [sora-editor](https://github.com/Rosemoe/sora-editor).\n\n## Add to your project\n\n![Latest maven release](https://img.shields.io/maven-central/v/com.itsaky.androidide.treesitter/android-tree-sitter)\n\n```gradle\n// main library\nimplementation 'com.itsaky.androidide.treesitter:android-tree-sitter:\u003cversion\u003e'\n\n// grammar libraries\n// \u003clanguage\u003e is the name of the language grammar. e.g. 'java', 'python', 'xml', etc.\nimplementation 'com.itsaky.androidide.treesitter:tree-sitter-\u003clanguage\u003e:\u003cversion\u003e'\n```\n\nBefore using tree sitter, load the native library when appropriate (e.g. in the `onCreate()` method of your `Application` class, or in your activity) :\n\n```java\nTreeSitter.loadLibrary();\n```\n\nThe following grammars have been published to Maven central :\n\n- Java\n- JSON\n- Kotlin\n- Logs (for highlighting logs in AndroidIDE)\n- Python\n- XML\n\n## Building\n\n### Prerequisites\n\n- Android NDK\n- `JDK 11` or newer.\n    - `JAVA_HOME` must be set to a JDK installation containing header files in\n      the `JAVA_HOME/include` directory. The JDK bundled with Android Studio does not contain these\n      headers.\n- `build-essential`, `nodejs`, `cmake` and `ninja-build` packages are required for builds.\n  Install them with :\n  ```\n  sudo apt install build-essential nodejs cmake ninja-build\n  ```\n\n- You'll also need to install `rust` and `cargo` (\u003e= v1.74.0). Follow the\n  instructions [here](https://www.rust-lang.org/learn/get-started) to get started with the\n  installation.\n\n- [`tree-sitter-cli`](https://github.com/tree-sitter/tree-sitter/tree/master/cli) - To build\n  grammars.\n\nAs tree-sitter is already included in the source (as a submodule), the `tree-sitter-cli` is built\nfrom source and then used to build the grammars.\n\nRead [the documentation](https://github.com/tree-sitter/tree-sitter/tree/master/cli) on how to\ninstall pre-built versions of `tree-sitter`.\n\nIf you want to use a prebuilt `tree-sitter` binary (either manually installed or installed\nwith `npm` or `cargo`), make sure it is accessible in the `PATH`, then set\nthe `TS_CLI_BUILD_FROM_SOURCE` environment variable :\n\n```bash\nexport TS_CLI_BUILD_FROM_SOURCE=false\n```\n\n\u003e _IMPORTANT: Building on a Linux machine is recommended._\n\n### Get source\n\nClone this repo with :\n\n```bash\ngit clone --recurse-submodules https://github.com/AndroidIDEOfficial/android-tree-sitter\n```\n\n### Install grammar dependencies\n\nYou might need to install the Node packages required by the grammars. For example, the `cpp` grammar\nrequires you to install the packages:\n\n```bash\n# from the root directory of this project\ncd grammars/cpp \u0026\u0026 npm install \u0026\u0026 cd -\n```\n\n### Build\n\nA normal Gradle build (`./gradlew build`) can be executed in order to build everything for Android\n_and_ the host OS. To build `android-tree-sitter` and the grammars _only_ for the host OS, you can\nexecute `buildForHost` task on appropriate subprojects.\n\n## Adding grammars\n\nThe Gradle modules for the grammars are almost identical, with only minor differences in the\nCMakeLists file and the Java binding class.\n\nThese Gradle modules are automatically generated by\nthe [`DynamicModulePlugin`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter/DynamicModulePlugin.kt).\nThe generation process relies on\nthe [`grammars.json`](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/grammars/grammars.json)\nfile. More information about the structure of this JSON file can be found in\nthe [README](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/dev/grammars/README.md)\nunder the `grammars` directory.\n\nApart from the `DynamicModulePlugin`, there\nare [other Gradle plugins](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/build-logic/ats/src/main/java/com/itsaky/androidide/treesitter)\nwhich are used to configure and build the grammars effectively.\n\nThe common configuration for the grammars can be found in\nthe [`build.gradle.kts`](https://github.com/AndroidIDEOfficial/android-tree-sitter/blob/52cc0400feee5079cac25b27d1e7b673ee53f436/build.gradle.kts#L136)\nfile. This is where you can make changes or adjustments to the module configuration that applies to\nall grammars.\n\nThe generated modules are located in the `rootDir/grammar-modules` directory. This is where you can\nfind the output of the module generation process.\n\nTo add a new grammar to the project, follow these steps:\n\n1. Begin by adding the grammar source code to the `grammars` directory. To accomplish this, you can\n   add a submodule using the following command:\n\n    ```bash\n    git submodule add \u003cremote_url\u003e grammars/\u003clanguage_name\u003e\n    ```\n\n2. The `language_name` should be the simple name of the language, without the `tree-sitter-` prefix.\n   This name is used to generate both the shared library and the Gradle module. For example, if\n   the `language_name` is `abc`:\n    - The module `tree-sitter-abc` will be automatically generated.\n    - The name of the resulting shared library will be `libtree-sitter-abc.so`.\n3. After adding the grammar source, update the `grammars.json` file to include the newly added\n   grammar in the project.\n4. Finally, sync the project to trigger the generation of the module for the newly added grammar.\n\n## Loading external grammars\n\nYou have two ways to load grammars that are not published along with this project :\n\n- Package the grammar with your application.\n- Load the grammar at runtime using `TSLanguage.loadLanguage`.\n\n`TSLanguage.loadLanguage` uses `dlopen` to load the library and must be CAREFULLY used. Also,\ngrammars that are loaded using this method must be closed when they are not used.\n\n\u003e **_Prefer using the first method whenever possible._**\n\n### Package the grammar with your application\n\nYou can package the grammar in your Android application as you would package any other shared\nlibrary :\n\n- Include the `libtree-sitter-myLang.so` file in the `jniLibs` directory of your project.\n- Create a native method in a Java class which will return the pointer to the language :\n\n```java\npackage com.my.app;\n\npublic class MyClass {\n\n  static {\n    System.loadLibrary(\"tree-sitter-myLang\");\n  }\n\n  public static native long myLang();\n}\n```\n\n- Write the C/C++ implementation for the method :\n\n```c++\nextern \"C\" TSLanguage *tree_sitter_myLang();\n\nextern \"C\"\nJNIEXPORT jlong JNICALL\nJava_com_my_app_MyClass_myLang(JNIEnv *env, jclass clazz) {\n  // simply cast the language pointer to jlong\n  return (jlong) tree_sitter_myLang();\n}\n```\n\n- Create and use the `TSLanguage` instance :\n\n```java\nfinal TSLanguage myLang=TSLanguage.create(\"myLang\",MyClass.myLang());\n\n// use it with TSParser\n  try(final var parser=TSParser.create()){\n  parser.setLanguage(myLang);\n  ...\n  }\n```\n\n### Load grammars at runtime\n\n\u003e android-tree-sitter `v3.1.0` or newer is required for this method.\n\n`TSLanguage` provides `loadLanguage(String, String)` method which can be used to load the grammars\nat runtime. This method uses `dlopen` to load the shared library, get the language instance and\nreturn its pointer. Use this method CAREFULLY.\n\nThe language instances created using this method **MUST** be closed using `TSLanguage.close()`.\nCalling the `close` method ensures that the underlying `dlopen`'ed library handle is closed\nusing `dlclose`.\n\nUsage :\n\n```java\n// provide the path to the shared library and the name of the language\n// the name is used to cache the language instance\n// further invocations of this method with the same lang name returns the\n// cached language instance\nfinal TSLanguage myLang=TSLanguage.loadLanguage(\"/path/to/libtree-sitter-myLang.so\",\"myLang\");\n\n  if(myLang!=null){\n  // loaded successfully\n  }else{\n  // failed to load the language\n  // see logcat for details\n  }\n```\n\nUse this language :\n\n```java\ntry(final var parser=TSParser.create()){\n  parser.setLanguage(myLang);\n  ...\n  }\n```\n\nYou don't have to keep a reference to `myLang`. Once loaded, the language can be accessed\nusing `TSLanguageCache` :\n\n```java\n// returns the 'myLang' instance i.e. both are same\nfinal TSLanguage cachedMyLang=TSLanguageCache.get(\"myLang\");\n```\n\n**DO NOT FORGET** to close the language :\n\n```java\n// this closes the underlying library handle\nmyLang.close();\n```\n\n## Examples\n\nFor examples, see\nthe [tests](https://github.com/AndroidIDEOfficial/android-tree-sitter/tree/dev/android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter).\n\n## License\n\n```\nandroid-tree-sitter library is free software; you can redistribute it and/or\nmodify it under the terms of the GNU Lesser General Public\nLicense as published by the Free Software Foundation; either\nversion 2.1 of the License, or (at your option) any later version.\n\nandroid-tree-sitter library is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\nLesser General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with android-tree-sitter.  If not, see \u003chttps://www.gnu.org/licenses/\u003e.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandroidideofficial%2Fandroid-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandroidideofficial%2Fandroid-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandroidideofficial%2Fandroid-tree-sitter/lists"}