mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-23 00:03:27 +01:00
HighlightingAssets: Extract get_syntax_for_path() method
To make the code easier to refactor further.
This commit is contained in:
parent
dc8225f682
commit
b69ab219d7
@ -167,6 +167,29 @@ impl HighlightingAssets {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_syntax_for_path(
|
||||||
|
&self,
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
mapping: &SyntaxMapping,
|
||||||
|
) -> Result<SyntaxReferenceInSet> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
match mapping.get_syntax_for(path) {
|
||||||
|
Some(MappingTarget::MapToUnknown) => {
|
||||||
|
Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
self.get_extension_syntax(file_name)?
|
||||||
|
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
|
pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
|
||||||
match self.get_theme_set().themes.get(theme) {
|
match self.get_theme_set().themes.get(theme) {
|
||||||
Some(theme) => theme,
|
Some(theme) => theme,
|
||||||
@ -201,23 +224,10 @@ impl HighlightingAssets {
|
|||||||
let path = input.path();
|
let path = input.path();
|
||||||
let path_syntax = if let Some(path) = path {
|
let path_syntax = if let Some(path) = path {
|
||||||
// If a path was provided, we try and detect the syntax based on extension mappings.
|
// If a path was provided, we try and detect the syntax based on extension mappings.
|
||||||
match mapping.get_syntax_for(
|
self.get_syntax_for_path(
|
||||||
PathAbs::new(path).map_or_else(|_| path.to_owned(), |p| p.as_path().to_path_buf()),
|
PathAbs::new(path).map_or_else(|_| path.to_owned(), |p| p.as_path().to_path_buf()),
|
||||||
) {
|
mapping,
|
||||||
Some(MappingTarget::MapToUnknown) => {
|
)
|
||||||
Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
self.get_extension_syntax(file_name)?
|
|
||||||
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Err(Error::UndetectedSyntax("[unknown]".into()))
|
Err(Error::UndetectedSyntax("[unknown]".into()))
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user