1use iced::Color;
12use std::rc::Rc;
13use std::sync::{Mutex, OnceLock};
14use syntect::easy::HighlightLines;
15use syntect::highlighting::{
16 HighlightState, Highlighter, RangedHighlightIterator, Style, ThemeSet,
17};
18use syntect::parsing::{
19 ParseState, ScopeStack, SyntaxDefinition, SyntaxReference, SyntaxSet,
20 SyntaxSetBuilder,
21};
22
23#[cfg(not(target_arch = "wasm32"))]
24use std::time::Instant;
25#[cfg(target_arch = "wasm32")]
26use web_time::Instant;
27
28use super::token_colors::{TokenColorizer, token_color_override};
29use crate::canvas_editor::{
30 CodeEditor, HIGHLIGHT_CLOCK_CHECK_INTERVAL, HighlightCache, ResolvedSyntax,
31};
32
33static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
36
37static EXTRA_SYNTAXES: Mutex<Vec<SyntaxDefinition>> = Mutex::new(Vec::new());
40
41static EXTRA_SET: OnceLock<SyntaxSet> = OnceLock::new();
45
46#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum SyntaxRegistrationError {
49 Parse {
52 index: usize,
54 message: String,
56 },
57 AlreadyBuilt,
60}
61
62impl std::fmt::Display for SyntaxRegistrationError {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 match self {
65 Self::Parse { index, message } => {
66 write!(f, "syntax definition {index}: {message}")
67 }
68 Self::AlreadyBuilt => write!(
69 f,
70 "the syntax set is already built; register syntaxes before \
71 the first editor highlights text"
72 ),
73 }
74 }
75}
76
77impl std::error::Error for SyntaxRegistrationError {}
78
79pub fn register_syntaxes<'a>(
118 definitions: impl IntoIterator<Item = &'a str>,
119) -> Result<(), SyntaxRegistrationError> {
120 register_into(&EXTRA_SET, &EXTRA_SYNTAXES, definitions)
121}
122
123fn register_into<'a>(
126 set: &OnceLock<SyntaxSet>,
127 queue: &Mutex<Vec<SyntaxDefinition>>,
128 definitions: impl IntoIterator<Item = &'a str>,
129) -> Result<(), SyntaxRegistrationError> {
130 let parsed = definitions
131 .into_iter()
132 .enumerate()
133 .map(|(index, text)| {
134 SyntaxDefinition::load_from_str(text, true, None).map_err(|e| {
135 SyntaxRegistrationError::Parse { index, message: e.to_string() }
136 })
137 })
138 .collect::<Result<Vec<_>, _>>()?;
139 let mut queue = queue.lock().unwrap_or_else(|e| e.into_inner());
141 if set.get().is_some() {
142 return Err(SyntaxRegistrationError::AlreadyBuilt);
143 }
144 queue.extend(parsed);
145 Ok(())
146}
147
148fn bundled_syntax_set() -> SyntaxSet {
150 #[cfg(feature = "two-face")]
151 {
152 two_face::syntax::extra_newlines()
153 }
154 #[cfg(not(feature = "two-face"))]
155 {
156 SyntaxSet::load_defaults_newlines()
157 }
158}
159
160fn build_extra_set(queue: &Mutex<Vec<SyntaxDefinition>>) -> SyntaxSet {
162 let mut builder = SyntaxSetBuilder::new();
163 let definitions =
164 std::mem::take(&mut *queue.lock().unwrap_or_else(|e| e.into_inner()));
165 for definition in definitions {
166 builder.add(definition);
167 }
168 builder.build()
169}
170
171fn find_registered<'a>(
174 set: &'a SyntaxSet,
175 name: &str,
176) -> Option<&'a SyntaxReference> {
177 set.syntaxes().iter().rev().find(|syntax| {
178 syntax.file_extensions.iter().any(|e| e.eq_ignore_ascii_case(name))
179 })
180}
181
182static THEME_SET: OnceLock<ThemeSet> = OnceLock::new();
184
185fn color_from_style(style: Style) -> Color {
193 color_from_syntect(style.foreground)
194}
195
196fn color_from_syntect(color: syntect::highlighting::Color) -> Color {
199 Color::from_rgb(
200 f32::from(color.r) / 255.0,
201 f32::from(color.g) / 255.0,
202 f32::from(color.b) / 255.0,
203 )
204}
205
206fn luminance(color: Color) -> f32 {
208 color.r * 0.299 + color.g * 0.587 + color.b * 0.114
209}
210
211const MIN_CONTRAST: f32 = 0.12;
213
214fn contrasts_with(color: Color, background: Color) -> bool {
216 (luminance(color) - luminance(background)).abs() >= MIN_CONTRAST
217}
218
219fn readable_color(style: Style, background: Color, fallback: Color) -> Color {
231 let foreground = color_from_style(style);
232 if contrasts_with(foreground, background) {
233 return foreground;
234 }
235 let highlight = color_from_syntect(style.background);
236 if contrasts_with(highlight, background) {
237 return highlight;
238 }
239 fallback
240}
241
242pub fn highlight_line_spans(
261 line: &str,
262 syntax: &syntect::parsing::SyntaxReference,
263 theme: &syntect::highlighting::Theme,
264 syntax_set: &SyntaxSet,
265) -> Vec<(Color, String)> {
266 let mut highlighter = HighlightLines::new(syntax, theme);
267 let ranges = highlighter
268 .highlight_line(line, syntax_set)
269 .unwrap_or_else(|_| vec![(Style::default(), line)]);
270
271 let background =
274 theme.settings.background.map_or(Color::BLACK, color_from_syntect);
275 let fallback =
276 theme.settings.foreground.map_or(Color::WHITE, color_from_syntect);
277
278 ranges
279 .into_iter()
280 .map(|(style, text)| {
281 (readable_color(style, background, fallback), text.to_string())
282 })
283 .collect()
284}
285
286const BRACKET_PAIR_COLORS_DARK: [Color; 3] = [
291 Color { r: 1.0, g: 0.843, b: 0.0, a: 1.0 }, Color { r: 0.855, g: 0.439, b: 0.839, a: 1.0 }, Color { r: 0.529, g: 0.808, b: 0.980, a: 1.0 }, ];
295
296const BRACKET_PAIR_COLORS_LIGHT: [Color; 3] = [
302 Color { r: 0.016, g: 0.192, b: 0.980, a: 1.0 }, Color { r: 0.192, g: 0.576, b: 0.192, a: 1.0 }, Color { r: 0.482, g: 0.220, b: 0.078, a: 1.0 }, ];
306
307impl CodeEditor {
308 pub(super) fn bracket_pair_colors(&self) -> &'static [Color; 3] {
318 if crate::theme::is_dark_background(self.style.background) {
319 &BRACKET_PAIR_COLORS_DARK
320 } else {
321 &BRACKET_PAIR_COLORS_LIGHT
322 }
323 }
324
325 pub(crate) fn resolve_syntax(
345 &self,
346 ) -> (
347 &'static SyntaxSet,
348 Option<&'static SyntaxReference>,
349 Option<&'static syntect::highlighting::Theme>,
350 ) {
351 let bundled = SYNTAX_SET.get_or_init(bundled_syntax_set);
352 let registered =
353 EXTRA_SET.get_or_init(|| build_extra_set(&EXTRA_SYNTAXES));
354 let owner = |syntax: Option<&'static SyntaxReference>| {
356 let is_registered = syntax.is_some_and(|s| {
357 registered.syntaxes().iter().any(|r| std::ptr::eq(r, s))
358 });
359 if is_registered { registered } else { bundled }
360 };
361
362 let dark_background =
365 crate::theme::is_dark_background(self.style.background);
366
367 let requested_theme = self.style.syntax_theme_name;
368
369 let mut memo = self.resolved_syntax.borrow_mut();
370 if let Some((syntax, theme)) = memo.as_ref().and_then(|resolved| {
371 resolved.get(&self.syntax, dark_background, requested_theme)
372 }) {
373 return (owner(syntax), syntax, theme);
374 }
375
376 let theme_set = THEME_SET.get_or_init(ThemeSet::load_defaults);
377 let automatic_theme = if dark_background {
378 "base16-ocean.dark"
379 } else {
380 "base16-ocean.light"
381 };
382 let theme = requested_theme
385 .and_then(|name| theme_set.themes.get(name))
386 .or_else(|| theme_set.themes.get(automatic_theme))
387 .or_else(|| theme_set.themes.values().next());
388
389 let syntax_set = bundled;
392 let syntax = find_registered(registered, &self.syntax)
393 .or_else(|| match self.syntax.as_str() {
394 "python" => syntax_set.find_syntax_by_extension("py"),
395 "rust" => syntax_set.find_syntax_by_extension("rs"),
396 "javascript" => syntax_set.find_syntax_by_extension("js"),
397 "htm" => syntax_set.find_syntax_by_extension("html"),
398 "svg" => syntax_set.find_syntax_by_extension("xml"),
399 "markdown" => syntax_set.find_syntax_by_extension("md"),
400 "text" => Some(syntax_set.find_syntax_plain_text()),
401 _ => syntax_set.find_syntax_by_extension(self.syntax.as_str()),
402 })
403 .or(Some(syntax_set.find_syntax_plain_text()));
404
405 *memo = Some(ResolvedSyntax::new(
406 &self.syntax,
407 dark_background,
408 requested_theme,
409 syntax,
410 theme,
411 ));
412
413 (owner(syntax), syntax, theme)
414 }
415
416 pub fn scope_color(&self, scope: &str) -> Option<Color> {
435 let scope = syntect::parsing::Scope::new(scope).ok()?;
436 if let Some(color) =
437 token_color_override(&self.style.token_colors, &[scope])
438 {
439 return Some(color);
440 }
441 let (_, _, theme) = self.resolve_syntax();
442 let foreground =
443 Highlighter::new(theme?).style_mod_for_stack(&[scope]).foreground?;
444 let color = color_from_syntect(foreground);
445 contrasts_with(color, self.style.background).then_some(color)
446 }
447
448 pub(crate) fn highlighted_line_cached(
471 &self,
472 logical_line: usize,
473 syntax: &syntect::parsing::SyntaxReference,
474 theme: &syntect::highlighting::Theme,
475 syntax_set: &SyntaxSet,
476 ) -> Rc<Vec<(Color, String)>> {
477 let mut guard = self.highlight_cache.borrow_mut();
478
479 let needs_reset =
481 guard.as_ref().is_none_or(|cache| cache.syntax() != self.syntax);
482 if needs_reset {
483 *guard = Some(HighlightCache::new(self.syntax.clone()));
484 }
485
486 let Some(cache) = guard.as_mut() else {
487 return Rc::new(highlight_line_spans(
490 self.buffer.line(logical_line),
491 syntax,
492 theme,
493 syntax_set,
494 ));
495 };
496
497 if let Some(spans) = cache.spans(logical_line) {
498 return spans;
499 }
500
501 let highlighter = Highlighter::new(theme);
504 let (mut parse_state, mut highlight_state) =
505 cache.resume_state().unwrap_or_else(|| {
506 (
507 ParseState::new(syntax),
508 HighlightState::new(&highlighter, ScopeStack::new()),
509 )
510 });
511
512 let line_count = self.buffer.line_count();
513 let target = logical_line.min(line_count.saturating_sub(1));
514 let missing_lines =
515 target.saturating_add(1).saturating_sub(cache.valid_len());
516 let lines_to_parse =
517 missing_lines.min(self.highlight_lines_remaining.get());
518 let parse_end = cache
519 .valid_len()
520 .saturating_add(lines_to_parse)
521 .saturating_sub(1)
522 .min(target);
523 let mut result = None;
524 let background = self.style.background;
525 let fallback = self.style.text_color;
526 let deadline = self.highlight_deadline.get();
527 let mut parsed = 0usize;
528 if lines_to_parse > 0 {
529 for index in cache.valid_len()..=parse_end {
530 if parsed > 0
533 && parsed.is_multiple_of(HIGHLIGHT_CLOCK_CHECK_INTERVAL)
534 && deadline
535 .is_some_and(|deadline| Instant::now() >= deadline)
536 {
537 break;
538 }
539 parsed += 1;
540 let mut line = self.buffer.line(index).to_string();
543 line.push('\n');
544
545 let ops = parse_state
546 .parse_line(&line, syntax_set)
547 .unwrap_or_default();
548 let mut token_colors = TokenColorizer::new(
550 self.style.token_colors,
551 highlight_state.path.clone(),
552 &ops,
553 );
554 let spans: Vec<(Color, String)> = RangedHighlightIterator::new(
555 &mut highlight_state,
556 &ops,
557 &line,
558 &highlighter,
559 )
560 .filter_map(|(style, text, range)| {
561 let text = text.strip_suffix('\n').unwrap_or(text);
562 if text.is_empty() {
563 return None;
564 }
565 let color = token_colors
566 .as_mut()
567 .and_then(|colors| colors.color_at(range.start))
568 .unwrap_or_else(|| {
569 readable_color(style, background, fallback)
570 });
571 Some((color, text.to_string()))
572 })
573 .collect();
574
575 let spans = Rc::new(spans);
576 cache.push_line(
577 Rc::clone(&spans),
578 parse_state.clone(),
579 highlight_state.clone(),
580 );
581 if index == logical_line {
582 result = Some(spans);
583 }
584 }
585 }
586 self.highlight_lines_remaining
589 .set(self.highlight_lines_remaining.get().saturating_sub(parsed));
590 if cache.valid_len() <= target {
594 self.highlight_catch_up_pending.set(true);
595 }
596
597 result.or_else(|| cache.spans(logical_line)).unwrap_or_else(|| {
598 Rc::new(vec![(
599 self.style.text_color,
600 self.buffer.line(logical_line).to_string(),
601 )])
602 })
603 }
604}
605
606#[cfg(test)]
607mod tests {
608 use std::rc::Rc;
609
610 use syntect::parsing::SyntaxSet;
611
612 use super::*;
613
614 #[test]
615 fn test_bracket_pair_colors_follow_the_editor_theme() {
616 let mut editor = CodeEditor::new("(a)", "rs");
617
618 editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Dark));
619 let dark = editor.bracket_pair_colors();
620
621 editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Light));
622 let light = editor.bracket_pair_colors();
623
624 assert_ne!(
625 dark[0], light[0],
626 "a light theme must not reuse the dark rainbow palette"
627 );
628 }
629
630 #[test]
631 fn test_bracket_pair_colors_contrast_with_their_background() {
632 for color in BRACKET_PAIR_COLORS_DARK {
636 assert!(
637 !crate::theme::is_dark_background(color),
638 "{color:?} is too dark for a dark background"
639 );
640 }
641 for color in BRACKET_PAIR_COLORS_LIGHT {
642 assert!(
643 crate::theme::is_dark_background(color),
644 "{color:?} is too light for a light background"
645 );
646 }
647 }
648
649 #[test]
650 fn test_highlight_line_spans_covers_full_line() {
651 let syntax_set = SyntaxSet::load_defaults_newlines();
652 let syntax = syntax_set.find_syntax_plain_text();
653 let theme = syntect::highlighting::Theme::default();
654
655 let line = "fn main() {}";
656 let spans = highlight_line_spans(line, syntax, &theme, &syntax_set);
657
658 assert!(!spans.is_empty(), "expected at least one span");
659 let combined: String =
660 spans.iter().map(|(_, text)| text.as_str()).collect();
661 assert_eq!(combined, line, "spans must cover the entire line");
662 }
663
664 #[test]
665 fn test_highlighted_line_cached_reuses_until_invalidated() {
666 let editor = CodeEditor::new("fn main() {}\nlet x = 1;", "rs");
667 let syntax_set = SyntaxSet::load_defaults_newlines();
668 let syntax = syntax_set.find_syntax_plain_text();
669 let theme = syntect::highlighting::Theme::default();
670
671 let first =
672 editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
673 let second =
674 editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
675 assert!(
676 Rc::ptr_eq(&first, &second),
677 "a cached line should be reused as the same Rc"
678 );
679
680 editor.invalidate_highlight_from(0);
681 let third =
682 editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
683 assert!(
684 !Rc::ptr_eq(&first, &third),
685 "invalidation should force the line to be recomputed"
686 );
687 }
688
689 #[test]
690 fn test_highlight_budget_uses_plain_fallback_without_scanning_to_target() {
691 let editor = CodeEditor::new("zero\none\ntwo\nthree\nfour", "txt");
692 let syntax_set = SyntaxSet::load_defaults_newlines();
693 let syntax = syntax_set.find_syntax_plain_text();
694 let theme = syntect::highlighting::Theme::default();
695 editor.highlight_lines_remaining.set(2);
696
697 let spans =
698 editor.highlighted_line_cached(4, syntax, &theme, &syntax_set);
699 let combined: String =
700 spans.iter().map(|(_, text)| text.as_str()).collect();
701
702 assert_eq!(combined, "four");
703 assert_eq!(
704 editor
705 .highlight_cache
706 .borrow()
707 .as_ref()
708 .map(super::HighlightCache::valid_len),
709 Some(2)
710 );
711 assert_eq!(editor.highlight_lines_remaining.get(), 0);
712 }
713
714 #[test]
715 fn test_expired_time_budget_stops_parsing_early() {
716 let content = vec!["{\"k\": 1}"; 1_000].join("\n");
717 let editor = CodeEditor::new(&content, "json");
718 let (syntax_set, syntax, theme) = editor.resolve_syntax();
719 let (Some(syntax), Some(theme)) = (syntax, theme) else {
720 return;
721 };
722 editor.highlight_lines_remaining.set(10_000);
724 editor.highlight_deadline.set(Some(Instant::now()));
725
726 let _ = editor.highlighted_line_cached(999, syntax, theme, syntax_set);
727
728 let parsed = editor
729 .highlight_cache
730 .borrow()
731 .as_ref()
732 .map(super::HighlightCache::valid_len);
733 assert_eq!(
734 parsed,
735 Some(HIGHLIGHT_CLOCK_CHECK_INTERVAL),
736 "parsing stops at the first clock check past the deadline"
737 );
738 assert_eq!(
739 editor.highlight_lines_remaining.get(),
740 10_000 - HIGHLIGHT_CLOCK_CHECK_INTERVAL,
741 "only the lines actually parsed are charged"
742 );
743 assert!(editor.highlight_catch_up_pending.get());
744 }
745
746 #[test]
747 fn test_no_deadline_parses_up_to_the_target() {
748 let content = vec!["{\"k\": 1}"; 1_000].join("\n");
749 let editor = CodeEditor::new(&content, "json");
750 let (syntax_set, syntax, theme) = editor.resolve_syntax();
751 let (Some(syntax), Some(theme)) = (syntax, theme) else {
752 return;
753 };
754
755 let _ = editor.highlighted_line_cached(999, syntax, theme, syntax_set);
756
757 assert!(!editor.highlight_catch_up_pending.get());
758 assert_eq!(
759 editor
760 .highlight_cache
761 .borrow()
762 .as_ref()
763 .map(super::HighlightCache::valid_len),
764 Some(1_000)
765 );
766 }
767
768 fn test_luminance(color: Color) -> f32 {
771 color.r * 0.299 + color.g * 0.587 + color.b * 0.114
772 }
773
774 fn assert_all_spans_legible(code: &str, syntax_name: &str) {
777 for iced_theme in [iced::Theme::Dark, iced::Theme::Light] {
778 let mut editor = CodeEditor::new(code, syntax_name);
779 editor.set_theme(crate::theme::from_iced_theme(&iced_theme));
780 let (syntax_set, syntax, theme) = editor.resolve_syntax();
781 let (Some(syntax), Some(theme)) = (syntax, theme) else {
782 continue;
783 };
784 let background = test_luminance(editor.style.background);
785
786 for line in 0..editor.buffer.line_count() {
787 let spans = editor
788 .highlighted_line_cached(line, syntax, theme, syntax_set);
789 for (color, text) in spans.iter() {
790 let contrast = (test_luminance(*color) - background).abs();
791 assert!(
792 contrast >= 0.12,
793 "{iced_theme:?}: {text:?} on line {line} is drawn in \
794 {color:?}, which vanishes into the background"
795 );
796 }
797 }
798 }
799 }
800
801 #[test]
802 fn test_invalid_json_tokens_stay_legible() {
803 assert_all_spans_legible("{\n \"a\": 1,,\n \"b\" 2\n}}\n]", "json");
807 }
808
809 #[test]
810 fn test_valid_code_keeps_distinct_theme_colors() {
811 let editor = CodeEditor::new("{\"key\": \"value\", \"n\": 42}", "json");
813 let (syntax_set, syntax, theme) = editor.resolve_syntax();
814 let (Some(syntax), Some(theme)) = (syntax, theme) else {
815 return;
816 };
817 let spans =
818 editor.highlighted_line_cached(0, syntax, theme, syntax_set);
819 let mut colors: Vec<Color> = Vec::new();
820 for (color, _) in spans.iter() {
821 if !colors.contains(color) {
822 colors.push(*color);
823 }
824 }
825 assert!(colors.len() >= 3, "expected varied token colors: {colors:?}");
826 assert_all_spans_legible("{\"key\": \"value\", \"n\": 42}", "json");
827 }
828
829 #[test]
830 fn test_highlighted_line_cached_handles_multiline_comments() {
831 let syntax_set = SyntaxSet::load_defaults_newlines();
832 let syntax = syntax_set
833 .find_syntax_by_extension("rs")
834 .unwrap_or_else(|| syntax_set.find_syntax_plain_text());
835 let theme = ThemeSet::load_defaults()
836 .themes
837 .get("base16-ocean.dark")
838 .cloned()
839 .unwrap_or_default();
840
841 let code = "let a = 1;\n/* open\nstill inside\n*/\nlet b = 2;";
843 let editor = CodeEditor::new(code, "rs");
844
845 let sequential =
847 editor.highlighted_line_cached(2, syntax, &theme, &syntax_set);
848 let independent = highlight_line_spans(
850 editor.buffer.line(2),
851 syntax,
852 &theme,
853 &syntax_set,
854 );
855
856 let sequential_color = sequential.first().map(|(color, _)| *color);
857 let independent_color = independent.first().map(|(color, _)| *color);
858 assert!(sequential_color.is_some());
859 assert!(independent_color.is_some());
860 assert_ne!(
861 sequential_color, independent_color,
862 "a line inside a block comment must be colored as a comment"
863 );
864 }
865
866 const TINY: &str = r#"
867name: Tiny
868file_extensions: [tiny]
869scope: source.tiny
870contexts:
871 main:
872 - match: '\bhello\b'
873 scope: keyword.tiny
874"#;
875
876 #[test]
877 #[allow(clippy::unwrap_used, clippy::expect_used)]
878 fn registered_syntaxes_are_found_by_extension() {
879 let set = OnceLock::new();
880 let queue = Mutex::new(Vec::new());
881 register_into(&set, &queue, [TINY]).expect("valid");
882 let built = set.get_or_init(|| build_extra_set(&queue));
883 assert!(queue.lock().unwrap().is_empty(), "the queue is consumed");
884
885 let tiny = find_registered(built, "TINY").expect("registered");
886 assert_eq!(tiny.name, "Tiny");
887 assert!(find_registered(built, "rs").is_none());
888
889 let spans = highlight_line_spans(
890 "say hello",
891 tiny,
892 &syntect::highlighting::Theme::default(),
893 built,
894 );
895 let texts: Vec<_> = spans.iter().map(|(_, t)| t.as_str()).collect();
896 assert_eq!(texts.concat(), "say hello");
897 }
898
899 #[test]
900 #[allow(clippy::unwrap_used, clippy::expect_used)]
901 fn registered_syntaxes_include_each_other_and_the_last_one_wins() {
902 let host = r#"
903name: Host
904file_extensions: [host]
905scope: source.host
906contexts:
907 main:
908 - include: scope:source.tiny
909"#;
910 let newer_tiny = TINY.replace("name: Tiny", "name: Newer Tiny");
911 let set = OnceLock::new();
912 let queue = Mutex::new(Vec::new());
913 register_into(&set, &queue, [TINY, host, newer_tiny.as_str()])
914 .expect("valid");
915 let built = set.get_or_init(|| build_extra_set(&queue));
916 assert_eq!(find_registered(built, "tiny").unwrap().name, "Newer Tiny");
917
918 let host = find_registered(built, "host").unwrap();
919 let mut state = ParseState::new(host);
920 let ops = state.parse_line("hello\n", built).expect("parses");
921 assert!(!ops.is_empty(), "the included grammar colors the word");
922 }
923
924 #[test]
925 #[allow(clippy::unwrap_used, clippy::expect_used)]
926 fn invalid_definitions_and_late_registration_are_rejected() {
927 let set = OnceLock::new();
928 let queue = Mutex::new(Vec::new());
929 let error = register_into(&set, &queue, [TINY, "name: [unclosed"])
930 .expect_err("invalid");
931 assert!(matches!(
932 error,
933 SyntaxRegistrationError::Parse { index: 1, .. }
934 ));
935 assert!(queue.lock().unwrap().is_empty(), "all or nothing");
936
937 set.get_or_init(|| build_extra_set(&queue));
938 assert_eq!(
939 register_into(&set, &queue, [TINY]),
940 Err(SyntaxRegistrationError::AlreadyBuilt)
941 );
942 }
943
944 #[test]
945 #[allow(clippy::unwrap_used, clippy::expect_used)]
946 fn an_include_of_a_missing_syntax_does_not_panic() {
947 let dangling = r#"
948name: Dangling
949file_extensions: [dangling]
950scope: source.dangling
951contexts:
952 main:
953 - include: scope:source.rust
954 - match: '\bok\b'
955 scope: keyword.dangling
956"#;
957 let set = OnceLock::new();
958 let queue = Mutex::new(Vec::new());
959 register_into(&set, &queue, [dangling]).expect("valid");
960 let built = set.get_or_init(|| build_extra_set(&queue));
961 let syntax = find_registered(built, "dangling").expect("registered");
962 let mut state = ParseState::new(syntax);
963 let _ = state.parse_line("ok then\n", built);
964 }
965
966 #[test]
967 fn with_nothing_registered_the_extra_set_is_empty() {
968 let queue = Mutex::new(Vec::new());
969 assert!(build_extra_set(&queue).syntaxes().is_empty());
970 }
971}