mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-22 12:58:26 +00:00
Find syntax ignoring known backup/template filename suffixes
For example, fall back to `foo.extension` for `foo.extension~`, `foo.extension.orig`, `foo.extension.in.in` etc.
This commit is contained in:
parent
fddd11a205
commit
355e62efe9
@ -23,6 +23,17 @@ pub struct HighlightingAssets {
|
|||||||
fallback_theme: Option<&'static str>,
|
fallback_theme: Option<&'static str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const IGNORED_SUFFIXES: [&str; 10] = [
|
||||||
|
// Editor etc backups
|
||||||
|
"~", ".bak", ".old", ".orig",
|
||||||
|
// Debian and derivatives apt/dpkg backups
|
||||||
|
".dpkg-dist", ".dpkg-old",
|
||||||
|
// Red Hat and derivatives rpm backups
|
||||||
|
".rpmnew", ".rpmorig", ".rpmsave",
|
||||||
|
// Build system input/template files
|
||||||
|
".in",
|
||||||
|
];
|
||||||
|
|
||||||
impl HighlightingAssets {
|
impl HighlightingAssets {
|
||||||
pub fn default_theme() -> &'static str {
|
pub fn default_theme() -> &'static str {
|
||||||
"Monokai Extended"
|
"Monokai Extended"
|
||||||
@ -273,12 +284,26 @@ impl HighlightingAssets {
|
|||||||
self.syntax_set
|
self.syntax_set
|
||||||
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
|
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
|
let file_path = Path::new(file_name);
|
||||||
self.syntax_set.find_syntax_by_extension(
|
self.syntax_set.find_syntax_by_extension(
|
||||||
Path::new(file_name)
|
file_path
|
||||||
.extension()
|
.extension()
|
||||||
.and_then(|x| x.to_str())
|
.and_then(|x| x.to_str())
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
)
|
).or_else(|| {
|
||||||
|
let file_str = file_path.to_str().unwrap_or_default();
|
||||||
|
for suffix in IGNORED_SUFFIXES.iter() {
|
||||||
|
if file_str.ends_with(suffix) {
|
||||||
|
return self.get_extension_syntax(
|
||||||
|
OsStr::new(
|
||||||
|
file_str
|
||||||
|
.strip_suffix(suffix)
|
||||||
|
.unwrap_or_default()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user