-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 2 commits
d68b11b
7805e20
aaf9110
761275b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
return self.get_extension_syntax(OsStr::new(stripped_filename));
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good stuff, TIL, done. |
||
} | ||
None | ||
}) | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.bak (editor etc backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.dpkg-dist (Debian dpkg backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.dpkg-old (Debian dpkg backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.in (build system input) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.in.in (build system input, doubly replaced) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.old (editor etc backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.orig (editor, diff etc backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.orig~ (backup of an editor, diff etc backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.rpmnew (Red Hat rpm backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.rpmorig (Red Hat rpm backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo.rpmsave (Red Hat rpm backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;117;113;94m//[0m[38;2;117;113;94m foo~ (editor backup) should highlight same as foo[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[38;2;248;248;242m// foo~ for unknown foo should not highlight[0m |
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 |
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will lead to an empty string if
file_path
contains invalid UTF-8 (which is possible). Instead of directly aborting here, we would still run the fullfor
loop below. I think the behavior would still be okay, but it feels a bit strange. And could possibly lead to errors in the future if the code below is changed. I think I would prefer something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM this one too, done.
Thanks a bunch for the guidance and opportunity to learn a bit of Rust 👍