fix: build the editor without tree-sitter on wasm
This commit is contained in:
@@ -11,7 +11,6 @@ gpui-native-icons = { workspace = true, optional = true }
|
|||||||
gpui-native-theme = { workspace = true }
|
gpui-native-theme = { workspace = true }
|
||||||
gpui-native-utils = { workspace = true }
|
gpui-native-utils = { workspace = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
tree-sitter = "0.25"
|
|
||||||
similar = "2"
|
similar = "2"
|
||||||
pulldown-cmark = { version = "0.13", default-features = false, optional = true }
|
pulldown-cmark = { version = "0.13", default-features = false, optional = true }
|
||||||
tree-sitter-rust = { version = "0.24", 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-javascript = { version = "0.23", optional = true }
|
||||||
tree-sitter-python = { 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]
|
[dev-dependencies]
|
||||||
gpui = { workspace = true, features = ["test-support"] }
|
gpui = { workspace = true, features = ["test-support"] }
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,17 @@ use gpui::SharedString;
|
|||||||
use gpui_native_utils::ResultLogErr;
|
use gpui_native_utils::ResultLogErr;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use tree_sitter::StreamingIterator as _;
|
use tree_sitter::StreamingIterator as _;
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static HIGHLIGHTERS: RefCell<HashMap<Language, Option<Highlighter>>> =
|
static HIGHLIGHTERS: RefCell<HashMap<Language, Option<Highlighter>>> =
|
||||||
RefCell::new(HashMap::new());
|
RefCell::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A grammar's parser and compiled highlight query.
|
/// A grammar's parser and compiled highlight query.
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
struct Highlighter {
|
struct Highlighter {
|
||||||
parser: tree_sitter::Parser,
|
parser: tree_sitter::Parser,
|
||||||
query: tree_sitter::Query,
|
query: tree_sitter::Query,
|
||||||
@@ -78,6 +81,7 @@ pub struct HighlightedCode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Highlights `source` with `language`'s tree-sitter grammar.
|
/// Highlights `source` with `language`'s tree-sitter grammar.
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub fn highlight(source: &str, language: Language) -> HighlightedCode {
|
pub fn highlight(source: &str, language: Language) -> HighlightedCode {
|
||||||
let Some((grammar, query_source)) = grammar_for(language) else {
|
let Some((grammar, query_source)) = grammar_for(language) else {
|
||||||
return plain(source);
|
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.
|
/// Source rendered without any grammar: plain text roles.
|
||||||
pub(crate) fn plain(source: &str) -> HighlightedCode {
|
pub(crate) fn plain(source: &str) -> HighlightedCode {
|
||||||
build_lines(source, &[])
|
build_lines(source, &[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The grammar and its highlight query for a language, when compiled in.
|
/// 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)> {
|
fn grammar_for(language: Language) -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
match language {
|
match language {
|
||||||
Language::Rust => rust_grammar(),
|
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.
|
/// The Rust grammar, compiled in only when the `rust` feature is on.
|
||||||
#[cfg(feature = "rust")]
|
#[cfg(feature = "rust")]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
Some((
|
Some((
|
||||||
tree_sitter_rust::LANGUAGE.into(),
|
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.
|
/// Without the feature, Rust sources render as plain text.
|
||||||
#[cfg(not(feature = "rust"))]
|
#[cfg(not(feature = "rust"))]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The TypeScript grammar, compiled in only when the `typescript` feature is on.
|
/// The TypeScript grammar, compiled in only when the `typescript` feature is on.
|
||||||
#[cfg(feature = "typescript")]
|
#[cfg(feature = "typescript")]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
Some((
|
Some((
|
||||||
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
|
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.
|
/// Without the feature, TypeScript sources render as plain text.
|
||||||
#[cfg(not(feature = "typescript"))]
|
#[cfg(not(feature = "typescript"))]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The JavaScript grammar, compiled in only when the `javascript` feature is on.
|
/// The JavaScript grammar, compiled in only when the `javascript` feature is on.
|
||||||
#[cfg(feature = "javascript")]
|
#[cfg(feature = "javascript")]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
Some((
|
Some((
|
||||||
tree_sitter_javascript::LANGUAGE.into(),
|
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.
|
/// Without the feature, JavaScript sources render as plain text.
|
||||||
#[cfg(not(feature = "javascript"))]
|
#[cfg(not(feature = "javascript"))]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The Python grammar, compiled in only when the `python` feature is on.
|
/// The Python grammar, compiled in only when the `python` feature is on.
|
||||||
#[cfg(feature = "python")]
|
#[cfg(feature = "python")]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
Some((
|
Some((
|
||||||
tree_sitter_python::LANGUAGE.into(),
|
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.
|
/// Without the feature, Python sources render as plain text.
|
||||||
#[cfg(not(feature = "python"))]
|
#[cfg(not(feature = "python"))]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user