summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-08-08 12:38:40 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-08-08 12:38:40 +0200
commit29cfaf8f319ce6ce6b4cd818e7bb730c8c18a3b1 (patch)
treefe2c4669e9b9c13b6a86584fb264010350cfda0f
parentd3bcc1e6d87225a11487ec81b459cfb3553a7dc4 (diff)
Make sure we always load a syntax set
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/helpers/highlight.rs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/helpers/highlight.rs b/src/helpers/highlight.rs
index 56cc372..f6ca98f 100644
--- a/src/helpers/highlight.rs
+++ b/src/helpers/highlight.rs
@@ -16,10 +16,27 @@ use syntect::parsing::SyntaxSet;
)]
pub fn highlight(s: &str, theme: &Theme, ext: Option<&str>) -> Result<cached::Return<String>> {
let syntax_set = SyntaxSet::load_defaults_newlines();
+
+ log::trace!("Finding syntax for {:?}", ext);
let syntax = match ext {
- Some(ext) => syntax_set.find_syntax_by_extension(ext).unwrap(),
- None => syntax_set.find_syntax_by_first_line(s).unwrap(),
- };
+ Some(ext) => {
+ log::trace!("Trying by extension ({:?})", ext);
+ syntax_set.find_syntax_by_extension(ext)
+ .or_else(|| {
+ log::trace!("Trying by first line...");
+ syntax_set.find_syntax_by_first_line(s)
+ })
+ },
+ None => {
+ log::trace!("Trying by first line...");
+ syntax_set.find_syntax_by_first_line(s)
+ },
+ }
+ .unwrap_or_else(|| {
+ log::trace!("Using plaintext");
+ syntax_set.find_syntax_plain_text()
+ });
+ log::trace!("Found syntax for {:?} = {:?}", ext, syntax);
Ok(Return::new(highlighted_html_for_string(s, &syntax_set, &syntax, theme)))
}