summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2021-06-13 23:36:38 +0300
committerDavid Peter <sharkdp@users.noreply.github.com>2021-07-09 06:34:11 +0200
commit355e62efe9321b4e64520230fe792dc0f80a6041 (patch)
tree52d7d9b19995a22a76ba920b00993424250d16df
parentfddd11a20548883707b31dc20ec727583d49a5e5 (diff)
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.
-rw-r--r--src/assets.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/assets.rs b/src/assets.rs
index 88509788..b09ced21 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -23,6 +23,17 @@ pub struct HighlightingAssets {
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 {
pub fn default_theme() -> &'static str {
"Monokai Extended"
@@ -273,12 +284,26 @@ impl HighlightingAssets {
self.syntax_set
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
.or_else(|| {
+ let file_path = Path::new(file_name);
self.syntax_set.find_syntax_by_extension(
- Path::new(file_name)
+ file_path
.extension()
.and_then(|x| x.to_str())
.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
+ })
})
}