Skip to content

Commit

Permalink
HighlightingAssets: Simplify get_extension_syntax()
Browse files Browse the repository at this point in the history
To make it easier to make further enhancements. See e.g. sharkdp#1687.
  • Loading branch information
Enselic committed Jun 28, 2021
1 parent 073b996 commit 311f561
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,17 @@ impl HighlightingAssets {
}

fn get_extension_syntax(&self, file_name: &OsStr) -> Option<&SyntaxReference> {
self.syntax_set
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
.or_else(|| {
self.syntax_set.find_syntax_by_extension(
Path::new(file_name)
.extension()
.and_then(|x| x.to_str())
.unwrap_or_default(),
)
})
// file_name can be e.g. '.bashrc' in which case .extension() will
// return the empty string, so we need to check both variants
let extensions = vec![
file_name,
Path::new(file_name).extension().unwrap_or_default(),
];

extensions.iter().find_map(|ext| {
self.syntax_set
.find_syntax_by_extension(ext.to_str().unwrap_or_default())
})
}

fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> {
Expand Down

0 comments on commit 311f561

Please sign in to comment.