Skip to main content

iced_code_editor/canvas_editor/render/
highlighting.rs

1//! Syntax resolution and token highlighting for [`CodeEditor`].
2//!
3//! Everything here answers "what colour is this text?", which is a different
4//! question from "where on the canvas does it go?" -- the latter lives in
5//! [`super::text`]. The two process-wide syntect singletons, the per-editor
6//! memo in front of them ([`CodeEditor::resolve_syntax`]), the incremental
7//! per-line cache ([`CodeEditor::highlighted_line_cached`]) and the
8//! rainbow-bracket palettes are all colour decisions, so they sit together
9//! here, leaving `text.rs` with the `draw_*` family.
10
11use 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
33/// Loading syntect's syntax definitions is expensive, so the set is built once
34/// per process and shared by every editor instance.
35static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
36
37/// Definitions added with [`register_syntaxes`], waiting for [`EXTRA_SET`] to
38/// be built.
39static EXTRA_SYNTAXES: Mutex<Vec<SyntaxDefinition>> = Mutex::new(Vec::new());
40
41/// The registered syntaxes, in a set of their own (see [`register_syntaxes`]).
42/// Kept apart from [`SYNTAX_SET`] because adding to the bundled set means
43/// re-linking every bundled grammar, which costs hundreds of milliseconds.
44static EXTRA_SET: OnceLock<SyntaxSet> = OnceLock::new();
45
46/// Error returned by [`register_syntaxes`].
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum SyntaxRegistrationError {
49    /// A definition is not a valid `.sublime-syntax` file. `index` is its
50    /// position in the input; nothing was registered.
51    Parse {
52        /// Position of the definition in the input.
53        index: usize,
54        /// The parser's message.
55        message: String,
56    },
57    /// An editor has already highlighted text, and the syntax set is built
58    /// once per process: register syntaxes before creating editors.
59    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
79/// Adds syntax definitions, in Sublime Text's `.sublime-syntax` format, to the
80/// set every editor highlights with.
81///
82/// A definition is chosen like the bundled ones, by the file extensions it
83/// lists (`file_extensions`) matching the editor's syntax name. Registered
84/// syntaxes are looked up before the bundled ones, so one can replace a bundled
85/// syntax; among registered ones claiming the same extension, the one
86/// registered last wins.
87///
88/// Registered syntaxes form a set of their own, which keeps building it cheap
89/// (adding to the bundled set would re-link every bundled grammar). They can
90/// include each other by scope (`include: scope:source.zig`), but not the
91/// bundled ones: register the grammars a definition includes along with it.
92/// An include that resolves to nothing is ignored.
93///
94/// The set is built once per process, the first time an editor highlights
95/// text, so call this before that, typically at startup. All or nothing: if
96/// one definition does not parse, none is registered.
97///
98/// # Errors
99///
100/// [`SyntaxRegistrationError::Parse`] for an invalid definition, and
101/// [`SyntaxRegistrationError::AlreadyBuilt`] when the set already exists.
102///
103/// # Example
104///
105/// ```
106/// let ini = r#"
107/// name: Tiny INI
108/// file_extensions: [tini]
109/// scope: source.tini
110/// contexts:
111///   main:
112///     - match: '^\[.*\]$'
113///       scope: entity.name.section.tini
114/// "#;
115/// iced_code_editor::register_syntaxes([ini]).expect("valid syntax");
116/// ```
117pub fn register_syntaxes<'a>(
118    definitions: impl IntoIterator<Item = &'a str>,
119) -> Result<(), SyntaxRegistrationError> {
120    register_into(&EXTRA_SET, &EXTRA_SYNTAXES, definitions)
121}
122
123/// [`register_syntaxes`] against an explicit set and queue (tests use their
124/// own, so they don't depend on the process-wide one).
125fn 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    // Hold the queue while checking, so the set can't be built in between.
140    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
148/// The bundled syntaxes.
149fn 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
160/// The registered syntaxes (see [`register_syntaxes`]), empty when none were.
161fn 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
171/// The registered syntax for an editor's syntax name, if any: the last one
172/// registered that lists it among its file extensions.
173fn 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
182/// Syntect's bundled themes, loaded once per process (see [`SYNTAX_SET`]).
183static THEME_SET: OnceLock<ThemeSet> = OnceLock::new();
184
185/// Converts a syntect highlight [`Style`] into an iced [`Color`].
186///
187/// Only the foreground color is used; alpha is left fully opaque.
188///
189/// # Arguments
190///
191/// * `style` - The syntect style whose foreground color is converted.
192fn color_from_style(style: Style) -> Color {
193    color_from_syntect(style.foreground)
194}
195
196/// Converts a syntect [`syntect::highlighting::Color`] into an opaque iced
197/// [`Color`].
198fn 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
206/// Perceptual luminance (ITU-R BT.601), used only to compare contrast.
207fn luminance(color: Color) -> f32 {
208    color.r * 0.299 + color.g * 0.587 + color.b * 0.114
209}
210
211/// Minimum luminance separation for a token to count as legible.
212const MIN_CONTRAST: f32 = 0.12;
213
214/// Whether `color` stands out enough from `background` to be read.
215fn contrasts_with(color: Color, background: Color) -> bool {
216    (luminance(color) - luminance(background)).abs() >= MIN_CONTRAST
217}
218
219/// Picks a foreground color for `style` that is legible on `background`.
220///
221/// Some syntect themes express a scope as a *background* highlight: the
222/// base16 themes paint `invalid.illegal` as "foreground = the theme
223/// background, background = red". Drawing only the foreground on the editor
224/// background therefore made half-typed code (an unmatched brace, a stray
225/// comma) disappear until it became valid again.
226///
227/// Resolution order: the style's foreground if it contrasts, then its
228/// background (the color the theme chose to stand out, so invalid tokens
229/// read as red), then `fallback`, the editor's plain text color.
230fn 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
242/// Tokenizes a full logical line into colored spans using syntect.
243///
244/// The returned spans cover the entire line in order, each pairing an iced
245/// [`Color`] with the owned token text. Each call highlights the line
246/// independently from the syntax's initial state, so it does not handle
247/// multi-line constructs; it is used for tests and benchmarks. Rendering uses
248/// the sequential `CodeEditor::highlighted_line_cached` instead.
249///
250/// # Arguments
251///
252/// * `line` - The full logical line content (without trailing newline).
253/// * `syntax` - The syntect syntax definition to tokenize with.
254/// * `theme` - The syntect highlighting theme providing token colors.
255/// * `syntax_set` - The syntax set the `syntax` belongs to.
256///
257/// # Returns
258///
259/// The ordered colored spans covering the entire line.
260pub 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    // Check contrast against the theme's own colors, for the same reason as
272    // the rendering path (see `readable_color`).
273    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
286/// Color cycle for bracket-pair colorization on a **dark** editor background,
287/// indexed by nesting depth modulo its length so a matching pair always shares
288/// a color. Matches the well-known VS Code Dark+ rainbow-bracket palette (gold,
289/// orchid, light sky blue) for a look users are likely already familiar with.
290const BRACKET_PAIR_COLORS_DARK: [Color; 3] = [
291    Color { r: 1.0, g: 0.843, b: 0.0, a: 1.0 }, // gold
292    Color { r: 0.855, g: 0.439, b: 0.839, a: 1.0 }, // orchid
293    Color { r: 0.529, g: 0.808, b: 0.980, a: 1.0 }, // light sky blue
294];
295
296/// The same cycle for a **light** editor background, following VS Code Light+.
297///
298/// The dark palette is built from bright, low-contrast-on-white hues; reusing
299/// it on a light background leaves brackets barely visible, so the light theme
300/// gets its own saturated, dark-toned triple.
301const BRACKET_PAIR_COLORS_LIGHT: [Color; 3] = [
302    Color { r: 0.016, g: 0.192, b: 0.980, a: 1.0 }, // blue
303    Color { r: 0.192, g: 0.576, b: 0.192, a: 1.0 }, // green
304    Color { r: 0.482, g: 0.220, b: 0.078, a: 1.0 }, // brown
305];
306
307impl CodeEditor {
308    /// Returns the rainbow-bracket palette matching this editor's theme.
309    ///
310    /// Like the syntect token palette (see [`CodeEditor::resolve_syntax`]),
311    /// the cycle is picked from the lightness of the style's background, so
312    /// brackets stay legible under both light and dark themes.
313    ///
314    /// # Returns
315    ///
316    /// The three-color cycle to index by nesting depth.
317    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    /// Resolves the syntect syntax and theme used to color this editor's text.
326    ///
327    /// Both the syntax set and the theme set are process-wide singletons, and
328    /// the per-editor result is memoized in [`ResolvedSyntax`], so this is
329    /// cheap enough to call on every render -- the underlying
330    /// `find_syntax_by_extension` scan is not. Common language aliases and
331    /// extensions (`python`/`py`, `markdown`/`md`, …) are normalized here, and
332    /// an unknown syntax falls back to plain text rather than losing the text.
333    ///
334    /// The token palette is [`crate::Style::syntax_theme_name`] when set to a
335    /// bundled theme; otherwise it follows the editor's own theme: a light
336    /// editor background selects `base16-ocean.light`, a dark one
337    /// `base16-ocean.dark`.
338    ///
339    /// # Returns
340    ///
341    /// The shared syntax set, the syntax to tokenize with, and the theme
342    /// providing token colors. The syntax and theme are `None` only when
343    /// syntect ships no definition at all for them.
344    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        // A syntax is parsed with the set it belongs to.
355        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        // Pair the token palette with the editor's own theme: a dark-tuned
363        // palette on a light background leaves comments and strings unreadable.
364        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        // A host-chosen theme wins; an unknown name falls back to the
383        // automatic one rather than losing the colors.
384        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        // Registered syntaxes first, then the bundled ones, normalizing common
390        // language aliases/extensions used by consumers.
391        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    /// The color this editor's syntax theme (with
417    /// [`crate::Style::token_colors`]) gives a TextMate scope such as
418    /// `"entity.name.function"`, to color [`crate::RangeStyle`]s like the
419    /// rest of the text.
420    ///
421    /// Returns `None` when the scope name is invalid, when the theme has no
422    /// rule for it (the syntax highlighting's color is then the right one),
423    /// or when its color would not stand out from the editor background.
424    ///
425    /// # Example
426    ///
427    /// ```
428    /// use iced_code_editor::CodeEditor;
429    ///
430    /// let editor = CodeEditor::new("fn main() {}", "rs");
431    /// assert!(editor.scope_color("entity.name.function").is_some());
432    /// assert!(editor.scope_color("no.such.scope.anywhere").is_none());
433    /// ```
434    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    /// Returns the memoized syntax-highlighted spans for a logical line.
449    ///
450    /// Highlighting is performed sequentially: lines `0..=logical_line` are
451    /// tokenized in order, each resuming from the syntect state left by the
452    /// previous line, so multi-line constructs (block comments, multi-line
453    /// strings) are colored correctly. The result is stored as a dense valid
454    /// prefix in [`HighlightCache`] and reused across wrapped visual segments
455    /// and across renders; an edit truncates the prefix from the changed line
456    /// (see [`CodeEditor::invalidate_highlight_from`]) instead of clearing it,
457    /// so deep lines are not re-parsed from the top on every keystroke. The
458    /// cache is reset only when the active syntax changes.
459    ///
460    /// # Arguments
461    ///
462    /// * `logical_line` - Index of the logical line in the buffer.
463    /// * `syntax` - The syntect syntax definition to tokenize with.
464    /// * `theme` - The syntect highlighting theme providing token colors.
465    /// * `syntax_set` - The syntax set the `syntax` belongs to.
466    ///
467    /// # Returns
468    ///
469    /// A shared handle to the line's colored token spans.
470    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        // Reset the whole cache only when the active syntax changes.
480        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            // Unreachable: populated just above. `unwrap`/`panic` are denied,
488            // so fall back to a single independent highlight without caching.
489            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        // Extend the valid prefix sequentially up to `logical_line`, carrying
502        // the parser/highlight state forward across lines.
503        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                // Stop when this frame's time budget runs out, even if the
531                // line budget has not. The clock is only read every few lines.
532                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                // syntect's `_newlines` syntaxes expect a trailing '\n' for correct
541                // end-of-line context handling; the stored buffer line has none.
542                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                // Captured before the highlighter advances the scope stack.
549                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        // Charge only the lines actually parsed: stopping on the time budget
587        // must not consume the rest of the line budget.
588        self.highlight_lines_remaining
589            .set(self.highlight_lines_remaining.get().saturating_sub(parsed));
590        // The target is still out of reach: ask for another frame. The line
591        // budget is only refilled when the content layer is redrawn, so
592        // without this the catch-up would stop until an unrelated redraw.
593        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        // Every dark-theme bracket color must read as light (drawn on a dark
633        // background) and every light-theme one as dark, or the brackets
634        // vanish into the page.
635        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        // Plenty of line budget, but the frame's clock already ran out.
723        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    /// Perceptual luminance (ITU-R BT.601), recomputed locally so the test
769    /// does not depend on the code under test.
770    fn test_luminance(color: Color) -> f32 {
771        color.r * 0.299 + color.g * 0.587 + color.b * 0.114
772    }
773
774    /// Every rendered span of `code` must stay legible on the editor
775    /// background, for both the light and the dark palette.
776    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        // A half-typed JSON body: the grammar flags the stray tokens as
804        // `invalid.illegal`, which base16 themes paint as background-coloured
805        // text on a red block.
806        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        // The legibility fallback must not flatten ordinary highlighting.
812        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        // Line index 2 ("still inside") sits within a `/* ... */` block.
842        let code = "let a = 1;\n/* open\nstill inside\n*/\nlet b = 2;";
843        let editor = CodeEditor::new(code, "rs");
844
845        // Sequential highlighting resumes inside the block comment.
846        let sequential =
847            editor.highlighted_line_cached(2, syntax, &theme, &syntax_set);
848        // Independent highlighting wrongly treats the line as ordinary code.
849        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}