Skip to content

Commit

Permalink
Fix incorrectly anchored matches on highlighter injection
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Sep 30, 2024
1 parent 5052344 commit 2f4b3fa
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tree-sitter-grammar-repository/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ fn build_language_registry(

let mut injection_regex = Vec::new();
let mut injection_regex_str_len = Vec::new();
let mut regex_to_camel = Vec::new();

for language in &language_definition {
if BLACKLISTED_MODULES.contains(&language.name.as_str()) {
Expand All @@ -150,8 +151,9 @@ fn build_language_registry(
}

if let Some(regex) = language.injection_regex.as_deref() {
injection_regex.push(regex);
injection_regex.push(format!("^{regex}$"));
injection_regex_str_len.push(regex.len());
regex_to_camel.push(camel_cased_name.clone());
}
}

Expand Down Expand Up @@ -208,6 +210,7 @@ fn build_language_registry(

pub fn from_injection(name: &str) -> Option<Self> {
const LENGTHS: [usize; #injection_regex_len] = [#(#injection_regex_str_len),*];
const REGEX_TO_VARIANT: [Language; #injection_regex_len] = [#(Language::#regex_to_camel),*];

thread_local! {
static REGEX: ::std::cell::LazyCell<::regex::RegexSet> = ::std::cell::LazyCell::new(|| {
Expand All @@ -227,7 +230,7 @@ fn build_language_registry(

if curr_length < max {
max = curr_length;
curr = Some(Self::VARIANTS[m]);
curr = Some(REGEX_TO_VARIANT[m]);
}
}
});
Expand Down

0 comments on commit 2f4b3fa

Please sign in to comment.