code: build tree-sitter native highlighter on every build (ungated, Windows NDK support)
- Commit
46cda80219044b623add56319f0d666b6f5acc14- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-07 03:43 UTC (4 months ago)
- Parents
ff97368
Diff
build-logic/src/main/kotlin/RustNative.kt
diff --git a/build-logic/src/main/kotlin/RustNative.kt b/build-logic/src/main/kotlin/RustNative.ktindex 96b78daade..57c4a4e04d 100644--- a/build-logic/src/main/kotlin/RustNative.kt+++ b/build-logic/src/main/kotlin/RustNative.kt@@ -69,7 +69,12 @@ fun Project.rustNativeLib(crate: String, remapLabel: String = crate) { description = "Cross-compiles lib$crate for $abiDir." workingDir = file("src/main/rust") - val clang = "$ndkBin/$triple$androidApiLevel-clang"+ // Windows hosts use the .cmd clang wrappers and .exe tool suffixes; Unix hosts none.+ val isWindows = OperatingSystem.current().isWindows+ val exeExt = if (isWindows) ".exe" else ""+ val clangExt = if (isWindows) ".cmd" else ""+ val clang = "$ndkBin/$triple$androidApiLevel-clang$clangExt"+ val clangpp = "$ndkBin/$triple$androidApiLevel-clang++$clangExt" val linkerVar = "CARGO_TARGET_${triple.uppercase().replace('-', '_')}_LINKER" // Per-crate target (may exist from older isolated builds) and workspace root target // (current scheme since Cargo.toml workspace unified to root).@@ -94,17 +99,20 @@ fun Project.rustNativeLib(crate: String, remapLabel: String = crate) { val cargoHome = System.getenv("CARGO_HOME") ?: "${System.getProperty("user.home")}/.cargo" val rustSrc = file("src/main/rust").absolutePath - environment("PATH", "$cargoBin:${System.getenv("PATH")}")+ val pathSep = if (isWindows) ";" else ":"+ environment("PATH", "$cargoBin$pathSep${System.getenv("PATH")}") // A non-rustup rustc earlier on PATH (e.g. Homebrew's) has no // aarch64-linux-android std, so cargo fails with "can't find crate for core". // Pin the rustup shim explicitly.- environment("RUSTC", "$cargoBin/rustc")+ environment("RUSTC", "$cargoBin/rustc$exeExt") // The per-API NDK clang wrapper bakes in --target and the sysroot. environment("CC", clang)- environment("AR", "$ndkBin/llvm-ar")+ environment("CXX", clangpp)+ environment("AR", "$ndkBin/llvm-ar$exeExt") environment("SYSROOT", ndkSysroot) environment(linkerVar, clang)- environment("HOST_CC", "/usr/bin/clang")+ // Force the system clang for host-side C on Unix; on Windows let cc locate MinGW gcc.+ if (!isWindows) environment("HOST_CC", "/usr/bin/clang") // Pre-generated bindings (armv8-only): weather's om-file-format-sys no longer uses bindgen // (bindings_android.rs/host checked in), so BINDGEN_EXTRA_CLANG_ARGS is no longer needed for build. // Kept as comment for documentation if future C sys crates added.@@ -126,7 +134,7 @@ fun Project.rustNativeLib(crate: String, remapLabel: String = crate) { } environment("CARGO_INCREMENTAL", "0") - commandLine("$cargoBin/cargo", "build", "--locked", "--release", "--target", triple)+ commandLine("$cargoBin/cargo$exeExt", "build", "--locked", "--release", "--target", triple) doLast { destSo.parentFile.mkdirs()
code/build.gradle.kts
diff --git a/code/build.gradle.kts b/code/build.gradle.ktsindex d600c1b5e7..52e9bda338 100644--- a/code/build.gradle.kts+++ b/code/build.gradle.kts@@ -15,21 +15,18 @@ android { } } -// Native tree-sitter highlighter (Rust → libcode_ts.so). Opt-in, because building it needs the-// Android NDK + `rustup target add aarch64-linux-android` and a generated-// code/src/main/rust/Cargo.lock. Enable in CI/on a build machine with `-PenableTreeSitter=true`.-// When disabled (the default), no .so is produced, TreeSitterNative.isAvailable is false at runtime,-// and the editor transparently falls back to the regex highlighter. Kept opt-in so ordinary builds-// (and other agents' native modules, which share the repo-root Cargo workspace) are unaffected.-if (providers.gradleProperty("enableTreeSitter").orNull == "true") {- androidComponents {- onVariants { variant ->- val rustDir = layout.buildDirectory.dir("rustJniLibs").get().asFile.absolutePath- variant.sources.jniLibs?.addStaticSourceDirectory(rustDir)- }+// Native tree-sitter highlighter (Rust → libcode_ts.so, arm64-v8a). Built on every `code` build;+// TreeSitterNative loads it at runtime and highlights the 10 supported languages, with the regex+// tokenizer still covering the remaining languages. Requires the Android NDK ++// `rustup target add aarch64-linux-android`; the pinned grammar set is locked in+// code/src/main/rust/Cargo.lock.+androidComponents {+ onVariants { variant ->+ val rustDir = layout.buildDirectory.dir("rustJniLibs").get().asFile.absolutePath+ variant.sources.jniLibs?.addStaticSourceDirectory(rustDir) }- rustNativeLib("code_ts", "code") }+rustNativeLib("code_ts", "code") dependencies { implementation(libs.androidx.datastore.preferences)
code/src/main/rust/Cargo.lock
diff --git a/code/src/main/rust/Cargo.lock b/code/src/main/rust/Cargo.locknew file mode 100644index 0000000000..443fab609a--- /dev/null+++ b/code/src/main/rust/Cargo.lock@@ -0,0 +1,423 @@+# This file is automatically @generated by Cargo.+# It is not intended for manual editing.+version = 4++[[package]]+name = "aho-corasick"+version = "1.1.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"+dependencies = [+ "memchr",+]++[[package]]+name = "bytes"+version = "1.12.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"++[[package]]+name = "cc"+version = "1.0.106"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "066fce287b1d4eafef758e89e09d724a24808a9196fe9756b8ca90e86d0719a2"++[[package]]+name = "cesu8"+version = "1.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"++[[package]]+name = "cfg-if"+version = "1.0.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"++[[package]]+name = "code_ts"+version = "0.1.0"+dependencies = [+ "jni",+ "tree-sitter",+ "tree-sitter-c",+ "tree-sitter-cpp",+ "tree-sitter-go",+ "tree-sitter-java",+ "tree-sitter-javascript",+ "tree-sitter-json",+ "tree-sitter-kotlin",+ "tree-sitter-python",+ "tree-sitter-rust",+ "tree-sitter-typescript",+]++[[package]]+name = "combine"+version = "4.6.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd"+dependencies = [+ "bytes",+ "memchr",+]++[[package]]+name = "jni"+version = "0.21.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"+dependencies = [+ "cesu8",+ "cfg-if",+ "combine",+ "jni-sys 0.3.1",+ "log",+ "thiserror",+ "walkdir",+ "windows-sys 0.45.0",+]++[[package]]+name = "jni-sys"+version = "0.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "41a652e1f9b6e0275df1f15b32661cf0d4b78d4d87ddec5e0c3c20f097433258"+dependencies = [+ "jni-sys 0.4.1",+]++[[package]]+name = "jni-sys"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2"+dependencies = [+ "jni-sys-macros",+]++[[package]]+name = "jni-sys-macros"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264"+dependencies = [+ "quote",+ "syn",+]++[[package]]+name = "log"+version = "0.4.33"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"++[[package]]+name = "memchr"+version = "2.8.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"++[[package]]+name = "proc-macro2"+version = "1.0.107"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"+dependencies = [+ "unicode-ident",+]++[[package]]+name = "quote"+version = "1.0.47"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"+dependencies = [+ "proc-macro2",+]++[[package]]+name = "regex"+version = "1.13.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"+dependencies = [+ "aho-corasick",+ "memchr",+ "regex-automata",+ "regex-syntax",+]++[[package]]+name = "regex-automata"+version = "0.4.18"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"+dependencies = [+ "aho-corasick",+ "memchr",+ "regex-syntax",+]++[[package]]+name = "regex-syntax"+version = "0.8.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"++[[package]]+name = "same-file"+version = "1.0.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"+dependencies = [+ "winapi-util",+]++[[package]]+name = "syn"+version = "2.0.119"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"+dependencies = [+ "proc-macro2",+ "quote",+ "unicode-ident",+]++[[package]]+name = "thiserror"+version = "1.0.69"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"+dependencies = [+ "thiserror-impl",+]++[[package]]+name = "thiserror-impl"+version = "1.0.69"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"+dependencies = [+ "proc-macro2",+ "quote",+ "syn",+]++[[package]]+name = "tree-sitter"+version = "0.22.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "df7cc499ceadd4dcdf7ec6d4cbc34ece92c3fa07821e287aedecd4416c516dca"+dependencies = [+ "cc",+ "regex",+]++[[package]]+name = "tree-sitter-c"+version = "0.21.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f956d5351d62652864a4ff3ae861747e7a1940dc96c9998ae400ac0d3ce30427"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-cpp"+version = "0.22.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1d509a22a992790d38f2c291961ff8a1ff016c437c7ec6befc9220b8eec8918c"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-go"+version = "0.21.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b8d702a98d3c7e70e466456e58ff2b1ac550bf1e29b97e5770676d2fdabec00d"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-java"+version = "0.21.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "33bc21adf831a773c075d9d00107ab43965e6a6ea7607b47fd9ec6f3db4b481b"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-javascript"+version = "0.21.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8710a71bc6779e33811a8067bdda3ed08bed1733296ff915e44faf60f8c533d7"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-json"+version = "0.21.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5b737dcb73c35d74b7d64a5f3dde158113c86a012bf3cee2bfdf2150d23b05db"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-kotlin"+version = "0.3.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "54ff60aeb036f5762515ceb31404512ea4f9599764bcd3857074bb82867bdd34"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-python"+version = "0.21.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b4066c6cf678f962f8c2c4561f205945c84834cce73d981e71392624fdc390a9"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-rust"+version = "0.21.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "277690f420bf90741dea984f3da038ace46c4fe6047cba57a66822226cde1c93"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "tree-sitter-typescript"+version = "0.21.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ecb35d98a688378e56c18c9c159824fd16f730ccbea19aacf4f206e5d5438ed9"+dependencies = [+ "cc",+ "tree-sitter",+]++[[package]]+name = "unicode-ident"+version = "1.0.24"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"++[[package]]+name = "walkdir"+version = "2.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"+dependencies = [+ "same-file",+ "winapi-util",+]++[[package]]+name = "winapi-util"+version = "0.1.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"+dependencies = [+ "windows-sys 0.61.2",+]++[[package]]+name = "windows-link"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"++[[package]]+name = "windows-sys"+version = "0.45.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"+dependencies = [+ "windows-targets",+]++[[package]]+name = "windows-sys"+version = "0.61.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"+dependencies = [+ "windows-link",+]++[[package]]+name = "windows-targets"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"+dependencies = [+ "windows_aarch64_gnullvm",+ "windows_aarch64_msvc",+ "windows_i686_gnu",+ "windows_i686_msvc",+ "windows_x86_64_gnu",+ "windows_x86_64_gnullvm",+ "windows_x86_64_msvc",+]++[[package]]+name = "windows_aarch64_gnullvm"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"++[[package]]+name = "windows_aarch64_msvc"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"++[[package]]+name = "windows_i686_gnu"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"++[[package]]+name = "windows_i686_msvc"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"++[[package]]+name = "windows_x86_64_gnu"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"++[[package]]+name = "windows_x86_64_gnullvm"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"++[[package]]+name = "windows_x86_64_msvc"+version = "0.42.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
code/src/main/rust/src/lib.rs
diff --git a/code/src/main/rust/src/lib.rs b/code/src/main/rust/src/lib.rsindex ef5e18606a..b746a0bee1 100644--- a/code/src/main/rust/src/lib.rs+++ b/code/src/main/rust/src/lib.rs@@ -6,56 +6,56 @@ //! The capture-kind ordinals are aligned with the Kotlin `TsKind` enum: //! `0 keyword, 1 string, 2 number, 3 comment, 4 annotation, 5 function, 6 type, 7 property`. //!-//! Targets the tree-sitter 0.22 API (`LanguageFn`, streaming query iterators). Byte offsets are-//! returned as-is; they line up with Kotlin string indices for ASCII/BMP text (the common case for-//! source code) — full UTF-16 mapping for astral characters is a documented v1 limitation.+//! Targets the tree-sitter 0.22 API. Byte offsets are returned as-is; they line up with Kotlin+//! string indices for ASCII/BMP text (the common case for source code) — full UTF-16 mapping for+//! astral characters is a documented v1 limitation. use jni::objects::{JClass, JString}; use jni::sys::jintArray; use jni::JNIEnv;-use tree_sitter::{Language, Parser, Query, QueryCursor, StreamingIterator};+use tree_sitter::{Language, Parser, Query, QueryCursor}; /// Resolves a language id (see `TreeSitterNative.languageIdFor`) to its grammar + highlights query. fn grammar(id: &str) -> Option<(Language, &'static str)> { let entry: (Language, &'static str) = match id { "kotlin" => (- tree_sitter_kotlin::LANGUAGE.into(),+ tree_sitter_kotlin::language(), tree_sitter_kotlin::HIGHLIGHTS_QUERY, ), "java" => (- tree_sitter_java::LANGUAGE.into(),+ tree_sitter_java::language(), tree_sitter_java::HIGHLIGHTS_QUERY, ), "javascript" => (- tree_sitter_javascript::LANGUAGE.into(),+ tree_sitter_javascript::language(), tree_sitter_javascript::HIGHLIGHT_QUERY, ), "typescript" => (- tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),+ tree_sitter_typescript::language_typescript(), tree_sitter_typescript::HIGHLIGHTS_QUERY, ), "python" => (- tree_sitter_python::LANGUAGE.into(),+ tree_sitter_python::language(), tree_sitter_python::HIGHLIGHTS_QUERY, ), "rust" => (- tree_sitter_rust::LANGUAGE.into(),+ tree_sitter_rust::language(), tree_sitter_rust::HIGHLIGHTS_QUERY, ), "go" => (- tree_sitter_go::LANGUAGE.into(),+ tree_sitter_go::language(), tree_sitter_go::HIGHLIGHTS_QUERY, ), "json" => (- tree_sitter_json::LANGUAGE.into(),+ tree_sitter_json::language(), tree_sitter_json::HIGHLIGHTS_QUERY, ), "c" => (- tree_sitter_c::LANGUAGE.into(),+ tree_sitter_c::language(), tree_sitter_c::HIGHLIGHT_QUERY, ), "cpp" => (- tree_sitter_cpp::LANGUAGE.into(),+ tree_sitter_cpp::language(), tree_sitter_cpp::HIGHLIGHT_QUERY, ), _ => return None,@@ -124,8 +124,7 @@ pub extern "system" fn Java_com_vayunmathur_code_util_TreeSitterNative_highlight let mut cursor = QueryCursor::new(); let mut out: Vec<i32> = Vec::new(); - let mut matches = cursor.matches(&query, tree.root_node(), bytes);- while let Some(m) = matches.next() {+ for m in cursor.matches(&query, tree.root_node(), bytes) { for cap in m.captures { let name = names[cap.index as usize]; let kind = kind_for(name);