Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find syntax ignoring known backup/template filename suffixes #1687

Merged
merged 4 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -273,12 +284,22 @@ 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(|| {
if let Some(file_str) = file_path.to_str() {
for suffix in IGNORED_SUFFIXES.iter() {
if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
return self.get_extension_syntax(OsStr::new(stripped_filename));
}
}
}
None
})
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.bak (editor etc backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.dpkg-dist (Debian dpkg backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.dpkg-old (Debian dpkg backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/highlighted/Ignored suffixes/test.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.in (build system input) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.in.in (build system input, doubly replaced) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.old (editor etc backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.orig (editor, diff etc backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.orig~ (backup of an editor, diff etc backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmnew (Red Hat rpm backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmorig (Red Hat rpm backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmsave (Red Hat rpm backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/highlighted/Ignored suffixes/test.rs~
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo~ (editor backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo~ for unknown foo should not highlight
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.bak (editor etc backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.dpkg-dist (Debian dpkg backup) should highlight same as foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.dpkg-old (Debian dpkg backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.in (build system input) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.in.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.in.in (build system input, doubly replaced) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.old
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.old (editor etc backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.orig (editor, diff etc backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.orig~
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.orig~ (backup of an editor, diff etc backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.rpmnew
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmnew (Red Hat rpm backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.rpmorig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmorig (Red Hat rpm backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs.rpmsave
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo.rpmsave (Red Hat rpm backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.rs~
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo~ (editor backup) should highlight same as foo
1 change: 1 addition & 0 deletions tests/syntax-tests/source/Ignored suffixes/test.unknown~
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// foo~ for unknown foo should not highlight