mirror of
https://github.com/tree-sitter/tree-sitter.git
synced 2026-09-10 07:26:23 -04:00
fix(loader): allow filenames with dots (#5529)
(cherry picked from commit 4cb11acd46)
This commit is contained in:
parent
a4bdd941d5
commit
8850e11bd8
|
|
@ -127,6 +127,52 @@ fn detect_language_by_double_barrel_file_extension() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detect_language_with_dots_in_filename() {
|
||||
let blade_dir = tree_sitter_dir(
|
||||
r#"{
|
||||
"grammars": [
|
||||
{
|
||||
"name": "blade_dots",
|
||||
"path": ".",
|
||||
"scope": "source.blade",
|
||||
"file-types": [
|
||||
"blade.php"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "php_dots",
|
||||
"path": ".",
|
||||
"scope": "source.php",
|
||||
"file-types": [
|
||||
"php"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"version": "0.0.1"
|
||||
}
|
||||
}
|
||||
"#,
|
||||
"blade_dots",
|
||||
);
|
||||
|
||||
let mut loader = Loader::with_parser_lib_path(scratch_dir().to_path_buf());
|
||||
let config = loader
|
||||
.find_language_configurations_at_path(blade_dir.path(), false)
|
||||
.unwrap();
|
||||
|
||||
// this is just to validate that we can read the tree-sitter.json correctly
|
||||
assert_eq!(config[0].scope.as_ref().unwrap(), "source.blade");
|
||||
|
||||
let file_name = blade_dir.path().join("foo.bar.baz.blade.php");
|
||||
fs::write(&file_name, "").unwrap();
|
||||
assert_eq!(
|
||||
get_lang_scope(&loader, &file_name),
|
||||
Some("source.blade".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detect_language_without_filename() {
|
||||
let gitignore_dir = tree_sitter_dir(
|
||||
|
|
|
|||
|
|
@ -810,8 +810,11 @@ impl Loader {
|
|||
path = PathBuf::from(path.file_stem()?.to_os_string());
|
||||
}
|
||||
extensions.reverse();
|
||||
self.language_configuration_ids_by_file_type
|
||||
.get(&extensions.join("."))
|
||||
// Try longest extension suffixs first (e.g. "foo.bar.baz"->"bar.baz"->"baz"),
|
||||
// stopping at the first match.
|
||||
(0..extensions.len())
|
||||
.map(|i| extensions[i..].join("."))
|
||||
.find_map(|key| self.language_configuration_ids_by_file_type.get(&key))
|
||||
});
|
||||
|
||||
if let Some(configuration_ids) = configuration_ids {
|
||||
|
|
|
|||
Loading…
Reference in a new issue