Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache SyntaxSet between parse_codeblock calls #147

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions i18n-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ fn heuristic_codeblock<'a>(events: &'a [(usize, Event)]) -> Vec<Group<'a>> {
/// Creates groups by parsing codeblock.
fn parse_codeblock<'a>(events: &'a [(usize, Event)]) -> Vec<Group<'a>> {
// Language detection from language identifier of codeblock.
let ss = SyntaxSet::load_defaults_newlines();
static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
let ss = SYNTAX_SET.get_or_init(SyntaxSet::load_defaults_newlines);

let syntax = if let (_, Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(x)))) = &events[0] {
ss.find_syntax_by_token(x.split(',').next().unwrap())
} else {
Expand All @@ -417,7 +419,7 @@ fn parse_codeblock<'a>(events: &'a [(usize, Event)]) -> Vec<Group<'a>> {
let mut stack = ScopeStack::new();
let mut stack_failure = false;

let Ok(ops) = ps.parse_line(text, &ss) else {
let Ok(ops) = ps.parse_line(text, ss) else {
// If parse is failed, the text event should be translated.
ret.push(Group::Translate(events[idx..idx + 1].into()));
continue;
Expand Down