summaryrefslogtreecommitdiffstats
path: root/src/assets.rs
diff options
context:
space:
mode:
authorMartin Nordholts <enselic@gmail.com>2021-09-16 17:01:12 +0200
committerGitHub <noreply@github.com>2021-09-16 17:01:12 +0200
commit9ed9a6fc3d3bc985a41d613e93848a50f103e8e3 (patch)
tree765181e3ae28857e7bae4aa076d65cc9cc398fd1 /src/assets.rs
parente84b702309471e31621cf0bc06f8754511a5cbbc (diff)
Simplify HighlightingAssets::get_syntax() first_line logic (#1852)
And make self.get_first_line_syntax() be called lazily.
Diffstat (limited to 'src/assets.rs')
-rw-r--r--src/assets.rs22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/assets.rs b/src/assets.rs
index 3f844bc4..dfdb8843 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -198,8 +198,6 @@ impl HighlightingAssets {
.ok_or_else(|| Error::UnknownSyntax(language.to_owned()));
}
- let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
-
// Get the path of the file:
// If this was set by the metadata, that will take priority.
// If it wasn't, it will use the real file path (if available).
@@ -212,7 +210,7 @@ impl HighlightingAssets {
_ => None,
});
- if let Some(path_str) = path_str {
+ let path_syntax = if let Some(path_str) = path_str {
// If a path was provided, we try and detect the syntax based on extension mappings.
let path = Path::new(path_str);
let absolute_path = PathAbs::new(path)
@@ -221,8 +219,9 @@ impl HighlightingAssets {
.unwrap_or_else(|| path.to_owned());
match mapping.get_syntax_for(absolute_path) {
- Some(MappingTarget::MapToUnknown) => line_syntax
- .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
+ Some(MappingTarget::MapToUnknown) => {
+ Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
+ }
Some(MappingTarget::MapTo(syntax_name)) => self
.find_syntax_by_name(syntax_name)?
@@ -231,13 +230,20 @@ impl HighlightingAssets {
None => {
let file_name = path.file_name().unwrap_or_default();
self.get_extension_syntax(file_name)?
- .or(line_syntax)
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
}
}
} else {
- // If a path wasn't provided, we fall back to the detect first-line syntax.
- line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
+ Err(Error::UndetectedSyntax("[unknown]".into()))
+ };
+
+ match path_syntax {
+ // If a path wasn't provided, or if path based syntax detection
+ // above failed, we fall back to first-line syntax detection.
+ Err(Error::UndetectedSyntax(path)) => self
+ .get_first_line_syntax(&mut input.reader)?
+ .ok_or(Error::UndetectedSyntax(path)),
+ _ => path_syntax,
}
}