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 2 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
29 changes: 27 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,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();
Copy link
Owner

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 full for 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:

if let Some(file_str) = file_path.to_str() {
  // …
}

Copy link
Contributor Author

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 👍

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()
));
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using .ends_with(suffix), .strip_suffix(suffix) and finally .unwrap*, I think you could simply call strip_suffix and decide based on the result. Something like:

if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
    return self.get_extension_syntax(OsStr::new(stripped_filename));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff, TIL, done.

}
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