From f07e6973fedb2fed4ff7f0e1a9d166bea7bedcab Mon Sep 17 00:00:00 2001 From: Nik Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Wed, 14 May 2025 19:41:18 +0100 Subject: [PATCH] fix: do not consider languages that do not have comment tokens --- Cargo.lock | 4 ---- Cargo.toml | 2 +- helix-core/src/comment.rs | 32 ++++++++++++++++++++------------ helix-core/src/syntax.rs | 6 +++++- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a52e24df..214bc1ee4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2801,8 +2801,6 @@ checksum = "bfb942dfe1d8e29a7ee7fcbde5bd2b9a25fb89aa70caea2eba3bee836ff41076" [[package]] name = "tree-house" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "803311306ba3279e87699f7fa16ea18fbcc8889d0ff0c20dc0652317f8b58117" dependencies = [ "arc-swap", "hashbrown 0.15.2", @@ -2818,8 +2816,6 @@ dependencies = [ [[package]] name = "tree-house-bindings" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2f6894df414648c56f1f5b129830447140ff1017867773694ba882d093aa140" dependencies = [ "cc", "libloading", diff --git a/Cargo.toml b/Cargo.toml index 1daf1b03c..113946c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ package.helix-tui.opt-level = 2 package.helix-term.opt-level = 2 [workspace.dependencies] -tree-house = { version = "0.1.0", default-features = false } +tree-house = { path = "../../projects/tree-house/highlighter", default-features = false } nucleo = "0.5.0" slotmap = "1.0.7" thiserror = "2.0" diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index ee5751473..0c0c374aa 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -54,19 +54,27 @@ pub fn get_injected_tokens( ) -> (Option>, Option>) { // Find the injection with the most tightly encompassing range. syntax - .map(|syntax| { - let config = loader - .language( - syntax - .layer(syntax.layer_for_byte_range(start, end)) - .language, - ) - .config(); + .and_then(|syntax| { + syntax + .layers_for_byte_range(start, end) + .into_iter() + .rev() + .find_map(|layer| { + let lang_config = loader.language(syntax.layer(layer).language).config(); - ( - config.comment_tokens.clone(), - config.block_comment_tokens.clone(), - ) + let has_any_comment_tokens = lang_config.comment_tokens.is_some() + || lang_config.block_comment_tokens.is_some(); + + // if the language does not have any comment tokens, it does not make + // any sense to consider it. + // + // This includes languages such as comment, jsdoc and regex: These + // languages are injected and never found in files by themselves + has_any_comment_tokens.then_some(( + lang_config.comment_tokens.clone(), + lang_config.block_comment_tokens.clone(), + )) + }) }) .unwrap_or_default() } diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index e232ee69b..da408cd82 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -412,7 +412,7 @@ impl FileTypeGlobMatcher { #[derive(Debug)] pub struct Syntax { - inner: tree_house::Syntax, + pub inner: tree_house::Syntax, } const PARSE_TIMEOUT: Duration = Duration::from_millis(500); // half a second is pretty generous @@ -450,6 +450,10 @@ impl Syntax { self.inner.layer_for_byte_range(start, end) } + pub fn layers_for_byte_range(&self, start: u32, end: u32) -> Vec { + self.inner.layers_for_byte_range(start, end) + } + pub fn root_language(&self) -> Language { self.layer(self.root_layer()).language }