From 9e0ea06435f6d5f0e2faacea121626019faa9b32 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Mon, 13 Sep 2021 17:37:05 +0200 Subject: [PATCH] HighlightingAssets: Add find_syntax_by_name() helper --- src/assets.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index d731fc3f..544acaf2 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -220,12 +220,7 @@ impl HighlightingAssets { let file_name = file_name.as_ref(); Ok(match mapping.get_syntax_for(file_name) { Some(MappingTarget::MapToUnknown) => None, - Some(MappingTarget::MapTo(syntax_name)) => { - let syntax_set = self.get_syntax_set()?; - syntax_set - .find_syntax_by_name(syntax_name) - .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }) - } + Some(MappingTarget::MapTo(syntax_name)) => self.find_syntax_by_name(syntax_name)?, None => self.get_extension_syntax(file_name.as_os_str())?, }) } @@ -287,13 +282,9 @@ impl HighlightingAssets { Some(MappingTarget::MapToUnknown) => line_syntax .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())), - Some(MappingTarget::MapTo(syntax_name)) => { - let syntax_set = self.get_syntax_set()?; - syntax_set - .find_syntax_by_name(syntax_name) - .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }) - .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned())) - } + Some(MappingTarget::MapTo(syntax_name)) => self + .find_syntax_by_name(syntax_name)? + .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned())), None => { let file_name = path.file_name().unwrap_or_default(); @@ -308,6 +299,13 @@ impl HighlightingAssets { } } + fn find_syntax_by_name(&self, syntax_name: &str) -> Result> { + let syntax_set = self.get_syntax_set()?; + Ok(syntax_set + .find_syntax_by_name(syntax_name) + .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })) + } + fn get_extension_syntax(&self, file_name: &OsStr) -> Result> { let mut syntax = self.find_syntax_by_file_name(file_name)?; if syntax.is_none() {