Skip to content

Commit

Permalink
Add highlighting timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Sep 30, 2024
1 parent d8bd2fb commit 5052344
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/syntax_highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

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};

Expand Down Expand Up @@ -194,10 +194,30 @@ pub fn format_file_inner(
};

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);
Expand Down

0 comments on commit 5052344

Please sign in to comment.