mirror of
https://github.com/sharkdp/bat
synced 2024-11-23 20:33:06 +00:00
HighlightingAssets: Add find_syntax_by_extension() helper
This commit is contained in:
parent
9e0ea06435
commit
6226eba52a
1 changed files with 14 additions and 14 deletions
|
@ -306,6 +306,13 @@ impl HighlightingAssets {
|
|||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
|
||||
}
|
||||
|
||||
fn find_syntax_by_extension(&self, extension: &str) -> Result<Option<SyntaxReferenceInSet>> {
|
||||
let syntax_set = self.get_syntax_set()?;
|
||||
Ok(syntax_set
|
||||
.find_syntax_by_extension(extension)
|
||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
|
||||
}
|
||||
|
||||
fn get_extension_syntax(&self, file_name: &OsStr) -> Result<Option<SyntaxReferenceInSet>> {
|
||||
let mut syntax = self.find_syntax_by_file_name(file_name)?;
|
||||
if syntax.is_none() {
|
||||
|
@ -318,26 +325,19 @@ impl HighlightingAssets {
|
|||
}
|
||||
|
||||
fn find_syntax_by_file_name(&self, file_name: &OsStr) -> Result<Option<SyntaxReferenceInSet>> {
|
||||
let syntax_set = self.get_syntax_set()?;
|
||||
Ok(syntax_set
|
||||
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
|
||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
|
||||
self.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
|
||||
}
|
||||
|
||||
fn find_syntax_by_file_name_extension(
|
||||
&self,
|
||||
file_name: &OsStr,
|
||||
) -> Result<Option<SyntaxReferenceInSet>> {
|
||||
let file_path = Path::new(file_name);
|
||||
let syntax_set = self.get_syntax_set()?;
|
||||
Ok(syntax_set
|
||||
.find_syntax_by_extension(
|
||||
file_path
|
||||
.extension()
|
||||
.and_then(|x| x.to_str())
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
|
||||
self.find_syntax_by_extension(
|
||||
Path::new(file_name)
|
||||
.extension()
|
||||
.and_then(|x| x.to_str())
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
}
|
||||
|
||||
/// If we find an ignored suffix on the file name, e.g. '~', we strip it and
|
||||
|
|
Loading…
Reference in a new issue