fix: build the editor without tree-sitter on wasm

This commit is contained in:
2026-11-10 21:02:24 +07:00
parent 0192c50157
commit 21646952ce
2 changed files with 22 additions and 1 deletions
+3 -1
View File
@@ -11,7 +11,6 @@ gpui-native-icons = { workspace = true, optional = true }
gpui-native-theme = { workspace = true }
gpui-native-utils = { workspace = true }
log = "0.4"
tree-sitter = "0.25"
similar = "2"
pulldown-cmark = { version = "0.13", default-features = false, optional = true }
tree-sitter-rust = { version = "0.24", optional = true }
@@ -19,6 +18,9 @@ tree-sitter-typescript = { version = "0.23", optional = true }
tree-sitter-javascript = { version = "0.23", optional = true }
tree-sitter-python = { version = "0.23", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tree-sitter = "0.25"
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
+19
View File
@@ -6,14 +6,17 @@ use gpui::SharedString;
use gpui_native_utils::ResultLogErr;
use std::cell::RefCell;
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use tree_sitter::StreamingIterator as _;
#[cfg(not(target_arch = "wasm32"))]
thread_local! {
static HIGHLIGHTERS: RefCell<HashMap<Language, Option<Highlighter>>> =
RefCell::new(HashMap::new());
}
/// A grammar's parser and compiled highlight query.
#[cfg(not(target_arch = "wasm32"))]
struct Highlighter {
parser: tree_sitter::Parser,
query: tree_sitter::Query,
@@ -78,6 +81,7 @@ pub struct HighlightedCode {
}
/// Highlights `source` with `language`'s tree-sitter grammar.
#[cfg(not(target_arch = "wasm32"))]
pub fn highlight(source: &str, language: Language) -> HighlightedCode {
let Some((grammar, query_source)) = grammar_for(language) else {
return plain(source);
@@ -140,12 +144,19 @@ pub fn highlight(source: &str, language: Language) -> HighlightedCode {
})
}
/// The web target has no tree-sitter parsers.
#[cfg(target_arch = "wasm32")]
pub fn highlight(source: &str, _language: Language) -> HighlightedCode {
plain(source)
}
/// Source rendered without any grammar: plain text roles.
pub(crate) fn plain(source: &str) -> HighlightedCode {
build_lines(source, &[])
}
/// The grammar and its highlight query for a language, when compiled in.
#[cfg(not(target_arch = "wasm32"))]
fn grammar_for(language: Language) -> Option<(tree_sitter::Language, &'static str)> {
match language {
Language::Rust => rust_grammar(),
@@ -157,6 +168,7 @@ fn grammar_for(language: Language) -> Option<(tree_sitter::Language, &'static st
/// The Rust grammar, compiled in only when the `rust` feature is on.
#[cfg(feature = "rust")]
#[cfg(not(target_arch = "wasm32"))]
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
Some((
tree_sitter_rust::LANGUAGE.into(),
@@ -166,12 +178,14 @@ fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
/// Without the feature, Rust sources render as plain text.
#[cfg(not(feature = "rust"))]
#[cfg(not(target_arch = "wasm32"))]
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
None
}
/// The TypeScript grammar, compiled in only when the `typescript` feature is on.
#[cfg(feature = "typescript")]
#[cfg(not(target_arch = "wasm32"))]
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
Some((
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
@@ -181,12 +195,14 @@ fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
/// Without the feature, TypeScript sources render as plain text.
#[cfg(not(feature = "typescript"))]
#[cfg(not(target_arch = "wasm32"))]
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
None
}
/// The JavaScript grammar, compiled in only when the `javascript` feature is on.
#[cfg(feature = "javascript")]
#[cfg(not(target_arch = "wasm32"))]
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
Some((
tree_sitter_javascript::LANGUAGE.into(),
@@ -196,12 +212,14 @@ fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
/// Without the feature, JavaScript sources render as plain text.
#[cfg(not(feature = "javascript"))]
#[cfg(not(target_arch = "wasm32"))]
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
None
}
/// The Python grammar, compiled in only when the `python` feature is on.
#[cfg(feature = "python")]
#[cfg(not(target_arch = "wasm32"))]
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
Some((
tree_sitter_python::LANGUAGE.into(),
@@ -211,6 +229,7 @@ fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
/// Without the feature, Python sources render as plain text.
#[cfg(not(feature = "python"))]
#[cfg(not(target_arch = "wasm32"))]
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
None
}