mirror of
https://github.com/sharkdp/bat
synced 2024-11-24 12:53:22 +00:00
Extract common syntax functionality to helper fns
This commit is contained in:
parent
8067fd9eda
commit
83d408bab3
1 changed files with 23 additions and 24 deletions
|
@ -184,17 +184,8 @@ impl HighlightingAssets {
|
||||||
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
||||||
(None, InputFile::Ordinary(ofile)) => {
|
(None, InputFile::Ordinary(ofile)) => {
|
||||||
let path = Path::new(ofile.filename());
|
let path = Path::new(ofile.filename());
|
||||||
|
|
||||||
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||||
let extension = path.extension().and_then(|x| x.to_str()).unwrap_or("");
|
let line_syntax = self.get_first_line_syntax(reader);
|
||||||
|
|
||||||
let ext_syntax = self
|
|
||||||
.syntax_set
|
|
||||||
.find_syntax_by_extension(&file_name)
|
|
||||||
.or_else(|| self.syntax_set.find_syntax_by_extension(&extension));
|
|
||||||
let line_syntax = String::from_utf8(reader.first_line.clone())
|
|
||||||
.ok()
|
|
||||||
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l));
|
|
||||||
|
|
||||||
let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned());
|
let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned());
|
||||||
match mapping.get_syntax_for(absolute_path) {
|
match mapping.get_syntax_for(absolute_path) {
|
||||||
|
@ -204,15 +195,24 @@ impl HighlightingAssets {
|
||||||
self.syntax_set.find_syntax_by_name(syntax_name)
|
self.syntax_set.find_syntax_by_name(syntax_name)
|
||||||
}
|
}
|
||||||
Some(MappingTarget::MapToUnknown) => line_syntax,
|
Some(MappingTarget::MapToUnknown) => line_syntax,
|
||||||
None => ext_syntax.or(line_syntax),
|
None => self.get_extension_syntax(file_name).or(line_syntax),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
|
(None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
|
||||||
(None, InputFile::StdIn(Some(file_name))) => self
|
(None, InputFile::StdIn(Some(file_name))) => self
|
||||||
.syntax_set
|
.get_extension_syntax(file_name.to_str().unwrap())
|
||||||
.find_syntax_by_extension(file_name.to_str().unwrap())
|
.or(self.get_first_line_syntax(reader)),
|
||||||
|
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
|
||||||
|
};
|
||||||
|
|
||||||
|
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_extension_syntax(&self, file_name: &str) -> Option<&SyntaxReference> {
|
||||||
|
self.syntax_set
|
||||||
|
.find_syntax_by_extension(file_name)
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
self.syntax_set.find_syntax_by_extension(
|
self.syntax_set.find_syntax_by_extension(
|
||||||
Path::new(file_name)
|
Path::new(file_name)
|
||||||
|
@ -221,13 +221,12 @@ impl HighlightingAssets {
|
||||||
.unwrap_or(""),
|
.unwrap_or(""),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.or(String::from_utf8(reader.first_line.clone())
|
}
|
||||||
.ok()
|
|
||||||
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))),
|
|
||||||
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
|
|
||||||
};
|
|
||||||
|
|
||||||
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
|
fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> Option<&SyntaxReference> {
|
||||||
|
String::from_utf8(reader.first_line.clone())
|
||||||
|
.ok()
|
||||||
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue