From 50523442c15afc9d0b459eef243dcd1630287063 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 30 Sep 2024 04:21:52 +0400 Subject: [PATCH] Add highlighting timeout --- src/syntax_highlight.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/syntax_highlight.rs b/src/syntax_highlight.rs index 139e598..065194d 100644 --- a/src/syntax_highlight.rs +++ a/src/syntax_highlight.rs @@ -8,7 +8,7 @@ }; use comrak::adapters::SyntaxHighlighterAdapter; -use tracing::debug; +use tracing::{debug, error}; use tree_sitter_grammar_repository::{Grammar, Language}; use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter}; @@ -194,10 +194,30 @@ }; HIGHLIGHTER.with_borrow_mut(|highlighter| { - let mut spans = highlighter.highlight(config, content.as_bytes(), None, |injection| { + highlighter.parser().reset(); + + let spans = highlighter.highlight(config, content.as_bytes(), None, |injection| { debug!(injection, "Highlighter switch requested"); fetch_highlighter_config_by_token(injection) - })?; + }); + + let mut spans = match spans { + Ok(v) => v, + Err(error) => { + error!( + ?error, + "Failed to run highlighter, falling back to plaintext" + ); + + for line in content.lines() { + out.push_str(line_prefix); + v_htmlescape::b_escape(line.as_bytes(), out); + out.push_str(line_suffix); + } + + return Ok(()); + } + }; let mut tag_open = true; out.push_str(line_prefix); -- rgit 0.1.3