fix file language detection from path

detection was broken due to Option's returning early on
file_stem() and extension()
This commit is contained in:
Jakub Panek 2024-06-12 10:12:10 +02:00
parent 890438883e
commit d4c562fcae
No known key found for this signature in database
GPG Key ID: D5E8A1F82F3BDA9D
1 changed files with 14 additions and 4 deletions

View File

@ -1532,13 +1532,23 @@ pub fn from_path(path: &Path) -> LapceLanguage {
}
fn from_path_raw(path: &Path) -> Option<LapceLanguage> {
let filename = path.file_stem()?.to_str()?.to_lowercase();
let extension = path.extension()?.to_str()?.to_lowercase();
let filename = path
.file_stem()
.and_then(|s| s.to_str().map(|s| s.to_lowercase()));
let extension = path
.extension()
.and_then(|s| s.to_str().map(|s| s.to_lowercase()));
// NOTE: This is a linear search. It is assumed that this function
// isn't called in any tight loop.
for properties in LANGUAGES {
if properties.files.contains(&filename.as_str())
|| properties.extensions.contains(&extension.as_str())
if properties
.files
.iter()
.zip(properties.extensions)
.any(|(f, e)| {
Some(*f) == filename.as_deref()
|| Some(*e) == extension.as_deref()
})
{
return Some(properties.id);
}