Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract regex check into a function and lower its precedence compared to the current grammar directory #3010

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 29 additions & 24 deletions cli/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::io::BufReader;
use std::io::{BufRead, BufReader};
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -227,6 +227,30 @@ impl Loader {
Ok(None)
}

pub fn language_configuration_for_first_line_regex(
&self,
path: &Path,
) -> Result<Option<(Language, &LanguageConfiguration)>> {
self.language_configuration_ids_by_first_line_regex
.iter()
.try_fold(None, |_, (regex, ids)| {
if let Some(regex) = Self::regex(Some(regex)) {
let file = fs::File::open(path)?;
let reader = BufReader::new(file);
let first_line = reader.lines().next().transpose()?;
if let Some(first_line) = first_line {
if regex.is_match(&first_line) && !ids.is_empty() {
let configuration = &self.language_configurations[ids[0]];
let language = self.language_for_id(configuration.language_id)?;
return Ok(Some((language, configuration)));
}
}
}

Ok(None)
})
}

pub fn language_configuration_for_file_name(
&self,
path: &Path,
Expand All @@ -243,26 +267,6 @@ impl Loader {
.and_then(|extension| {
self.language_configuration_ids_by_file_type.get(extension)
})
})
.or_else(|| {
let Ok(file) = fs::File::open(path) else {
return None;
};
let reader = BufReader::new(file);
let Some(Ok(first_line)) = std::io::BufRead::lines(reader).next() else {
return None;
};

self.language_configuration_ids_by_first_line_regex
.iter()
.find(|(regex, _)| {
if let Some(regex) = Self::regex(Some(regex)) {
regex.is_match(&first_line)
} else {
false
}
})
.map(|(_, ids)| ids)
});

if let Some(configuration_ids) = configuration_ids {
Expand Down Expand Up @@ -1006,6 +1010,8 @@ impl Loader {
.cloned()
{
Ok(lang)
} else if let Some(lang) = self.language_configuration_for_first_line_regex(path)? {
Ok(lang.0)
} else {
Err(anyhow!("No language found"))
}
Expand Down Expand Up @@ -1066,8 +1072,7 @@ impl<'a> LanguageConfiguration<'a> {
),
None => (None, None, None),
};
return self
.highlight_config
self.highlight_config
.get_or_try_init(|| {
let (highlights_query, highlight_ranges) = self.read_queries(
if highlights_filenames.is_some() {
Expand Down Expand Up @@ -1145,7 +1150,7 @@ impl<'a> LanguageConfiguration<'a> {
Ok(Some(result))
}
})
.map(Option::as_ref);
.map(Option::as_ref)
}

pub fn tags_config(&self, language: Language) -> Result<Option<&TagsConfiguration>> {
Expand Down
20 changes: 10 additions & 10 deletions cli/src/generate/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ impl Generator {
for symbol in &self.parse_table.symbols {
if *symbol != Symbol::end() {
self.symbol_order.insert(*symbol, i);
add_line!(self, "{} = {},", self.symbol_ids[&symbol], i);
add_line!(self, "{} = {},", self.symbol_ids[symbol], i);
i += 1;
}
}
for alias in &self.unique_aliases {
add_line!(self, "{} = {},", self.alias_ids[&alias], i);
add_line!(self, "{} = {},", self.alias_ids[alias], i);
i += 1;
}
dedent!(self);
Expand All @@ -370,13 +370,13 @@ impl Generator {
alias.value.as_str()
}),
);
add_line!(self, "[{}] = \"{}\",", self.symbol_ids[&symbol], name);
add_line!(self, "[{}] = \"{}\",", self.symbol_ids[symbol], name);
}
for alias in &self.unique_aliases {
add_line!(
self,
"[{}] = \"{}\",",
self.alias_ids[&alias],
self.alias_ids[alias],
self.sanitize_string(&alias.value)
);
}
Expand All @@ -401,8 +401,8 @@ impl Generator {
add_line!(
self,
"[{}] = {},",
self.alias_ids[&alias],
self.alias_ids[&alias],
self.alias_ids[alias],
self.alias_ids[alias],
);
}

Expand Down Expand Up @@ -446,7 +446,7 @@ impl Generator {
);
indent!(self);
for symbol in &self.parse_table.symbols {
add_line!(self, "[{}] = {{", self.symbol_ids[&symbol]);
add_line!(self, "[{}] = {{", self.symbol_ids[symbol]);
indent!(self);
if let Some(Alias { is_named, .. }) = self.default_aliases.get(symbol) {
add_line!(self, ".visible = true,");
Expand Down Expand Up @@ -478,7 +478,7 @@ impl Generator {
add_line!(self, "}},");
}
for alias in &self.unique_aliases {
add_line!(self, "[{}] = {{", self.alias_ids[&alias]);
add_line!(self, "[{}] = {{", self.alias_ids[alias]);
indent!(self);
add_line!(self, ".visible = true,");
add_line!(self, ".named = {},", alias.is_named);
Expand Down Expand Up @@ -510,7 +510,7 @@ impl Generator {
indent!(self);
for (j, alias) in production_info.alias_sequence.iter().enumerate() {
if let Some(alias) = alias {
add_line!(self, "[{}] = {},", j, self.alias_ids[&alias]);
add_line!(self, "[{}] = {},", j, self.alias_ids[alias]);
}
}
dedent!(self);
Expand Down Expand Up @@ -554,7 +554,7 @@ impl Generator {
indent!(self);
for (symbol, alias_ids) in alias_ids_by_symbol {
let symbol_id = &self.symbol_ids[symbol];
let public_symbol_id = &self.symbol_ids[&self.symbol_map[&symbol]];
let public_symbol_id = &self.symbol_ids[&self.symbol_map[symbol]];
add_line!(self, "{symbol_id}, {},", 1 + alias_ids.len());
indent!(self);
add_line!(self, "{public_symbol_id},");
Expand Down
14 changes: 12 additions & 2 deletions cli/src/tests/detect_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ fn tree_sitter_dir(package_json: &str, name: &str) -> tempfile::TempDir {
fn get_lang_scope(loader: &mut Loader, file_name: &Path) -> Option<String> {
loader
.language_configuration_for_file_name(file_name)
.unwrap()
.and_then(|r| r.1.scope.clone())
.ok()
.and_then(|config| {
if let Some((_, config)) = config {
config.scope.clone()
} else if let Ok(Some((_, config))) =
loader.language_configuration_for_first_line_regex(file_name)
{
config.scope.clone()
} else {
None
}
})
}
2 changes: 1 addition & 1 deletion cli/src/tests/helpers/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn get_tags_config(language_name: &str) -> TagsConfiguration {
}

pub fn get_test_language(name: &str, parser_code: &str, path: Option<&Path>) -> Language {
let src_dir = SCRATCH_DIR.join("src").join(name);
let src_dir = scratch_dir().join("src").join(name);
fs::create_dir_all(&src_dir).unwrap();

let parser_path = src_dir.join("parser.c");
Expand Down
6 changes: 3 additions & 3 deletions cli/src/tests/helpers/query_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ impl Pattern {
}
}

impl ToString for Pattern {
fn to_string(&self) -> String {
impl std::fmt::Display for Pattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut result = String::new();
self.write_to_string(&mut result, 0);
result
write!(f, "{result}")
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ svg { width: 100%; }

";

#[must_use]
pub fn lang_not_found_for_path(path: &Path, loader_config: &LoaderConfig) -> String {
let path = path.display();
format!(
Expand Down