From 0994f3783f13464d0d2d78ddefa4f454cd623489 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sat, 18 Sep 2021 06:50:54 +0200 Subject: [PATCH] HighlightingAssets: Move common get_extension_syntax() code into find_syntax_by_extension() --- src/assets.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index df6943fa..f9a990fe 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -250,22 +250,18 @@ impl HighlightingAssets { .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })) } - fn find_syntax_by_extension(&self, extension: &str) -> Result> { + fn find_syntax_by_extension(&self, e: Option<&OsStr>) -> Result> { let syntax_set = self.get_syntax_set()?; + let extension = e.and_then(|x| x.to_str()).unwrap_or_default(); Ok(syntax_set .find_syntax_by_extension(extension) .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })) } fn get_extension_syntax(&self, file_name: &OsStr) -> Result> { - let mut syntax = self.find_syntax_by_extension(file_name.to_str().unwrap_or_default())?; + let mut syntax = self.find_syntax_by_extension(Some(file_name))?; if syntax.is_none() { - syntax = self.find_syntax_by_extension( - Path::new(file_name) - .extension() - .and_then(|x| x.to_str()) - .unwrap_or_default(), - )?; + syntax = self.find_syntax_by_extension(Path::new(file_name).extension())?; } if syntax.is_none() { syntax = try_with_stripped_suffix(file_name, |stripped_file_name| {