Skip to main content

text_document/
highlight.rs

1//! Syntax highlighting support.
2//!
3//! Provides a [`SyntaxHighlighter`] trait inspired by Qt's `QSyntaxHighlighter`.
4//! Implementors produce shadow formatting that is merged into
5//! [`FragmentContent`] at layout time but never touches the stored
6//! `format_runs` / `block_images` tables — export, cursor, undo, and
7//! search remain unaffected.
8
9use std::any::Any;
10use std::collections::HashMap;
11use std::sync::Arc;
12
13use frontend::commands::block_commands;
14
15use crate::flow::FragmentContent;
16use crate::inner::TextDocumentInner;
17use crate::{CharVerticalAlignment, Color, TextFormat, UnderlineStyle};
18
19// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
20// Public types
21// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
22
23/// Formatting applied by a syntax highlighter to a text range.
24///
25/// All fields are `Option`: `None` means "don't override the real format."
26/// Only non-`None` fields take precedence over the corresponding
27/// [`TextFormat`] field for display purposes.
28#[derive(Debug, Clone, Default, PartialEq, Eq)]
29pub struct HighlightFormat {
30    pub foreground_color: Option<Color>,
31    pub background_color: Option<Color>,
32    pub underline_color: Option<Color>,
33    pub font_family: Option<String>,
34    pub font_point_size: Option<u32>,
35    pub font_weight: Option<u32>,
36    pub font_bold: Option<bool>,
37    pub font_italic: Option<bool>,
38    pub font_underline: Option<bool>,
39    pub font_overline: Option<bool>,
40    pub font_strikeout: Option<bool>,
41    pub letter_spacing: Option<i32>,
42    pub word_spacing: Option<i32>,
43    pub underline_style: Option<UnderlineStyle>,
44    pub vertical_alignment: Option<CharVerticalAlignment>,
45    pub tooltip: Option<String>,
46}
47
48/// A single highlight span within a block.
49///
50/// `start` and `length` are block-relative **character** offsets.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct HighlightSpan {
53    pub start: usize,
54    pub length: usize,
55    pub format: HighlightFormat,
56}
57
58/// Context passed to [`SyntaxHighlighter::highlight_block`].
59///
60/// Provides methods to set highlight formatting and manage per-block state.
61pub struct HighlightContext {
62    spans: Vec<HighlightSpan>,
63    previous_state: i64,
64    current_state: i64,
65    block_id: usize,
66    user_data: Option<Box<dyn Any + Send + Sync>>,
67}
68
69impl HighlightContext {
70    /// Create a new context for highlighting a block.
71    pub fn new(
72        block_id: usize,
73        previous_state: i64,
74        user_data: Option<Box<dyn Any + Send + Sync>>,
75    ) -> Self {
76        Self {
77            spans: Vec::new(),
78            previous_state,
79            current_state: -1,
80            block_id,
81            user_data,
82        }
83    }
84
85    /// Apply a highlight format to a character range within the current block.
86    ///
87    /// Zero-length spans are silently ignored.
88    pub fn set_format(&mut self, start: usize, length: usize, format: HighlightFormat) {
89        if length == 0 {
90            return;
91        }
92        self.spans.push(HighlightSpan {
93            start,
94            length,
95            format,
96        });
97    }
98
99    /// Get the block state of the previous block (−1 if no state was set).
100    pub fn previous_block_state(&self) -> i64 {
101        self.previous_state
102    }
103
104    /// Set the block state for the current block.
105    ///
106    /// If the new state differs from the previously stored value, the next
107    /// block will be re-highlighted automatically (cascade).
108    pub fn set_current_block_state(&mut self, state: i64) {
109        self.current_state = state;
110    }
111
112    /// Get the current block state (defaults to −1).
113    pub fn current_block_state(&self) -> i64 {
114        self.current_state
115    }
116
117    /// Get the block ID.
118    pub fn block_id(&self) -> usize {
119        self.block_id
120    }
121
122    /// Set per-block user data (replaces any existing data).
123    pub fn set_user_data(&mut self, data: Box<dyn Any + Send + Sync>) {
124        self.user_data = Some(data);
125    }
126
127    /// Get a reference to the per-block user data.
128    pub fn user_data(&self) -> Option<&(dyn Any + Send + Sync)> {
129        self.user_data.as_deref()
130    }
131
132    /// Get a mutable reference to the per-block user data.
133    pub fn user_data_mut(&mut self) -> Option<&mut (dyn Any + Send + Sync)> {
134        self.user_data.as_deref_mut()
135    }
136
137    /// Consume the context and return the accumulated spans, final state,
138    /// and user data.
139    pub fn into_parts(self) -> (Vec<HighlightSpan>, i64, Option<Box<dyn Any + Send + Sync>>) {
140        (self.spans, self.current_state, self.user_data)
141    }
142}
143
144/// A user-implemented syntax highlighter that applies visual-only formatting.
145///
146/// Inspired by Qt's `QSyntaxHighlighter`. Implement this trait and attach it
147/// to a document via [`TextDocument::set_syntax_highlighter`](crate::TextDocument::set_syntax_highlighter).
148///
149/// The highlighter is called once per block when the document content changes.
150/// Use [`HighlightContext::set_format`] to apply highlight spans. Use
151/// [`HighlightContext::set_current_block_state`] and
152/// [`HighlightContext::previous_block_state`] for multi-block constructs
153/// (e.g., multiline comments).
154pub trait SyntaxHighlighter: Send + Sync {
155    /// Called for each block that needs re-highlighting.
156    fn highlight_block(&self, text: &str, ctx: &mut HighlightContext);
157}
158
159/// Identifies one registered highlight session (see [`crate::TextDocument::add_syntax_session`]
160/// / [`crate::TextDocument::add_range_session`]).
161///
162/// A document can carry several highlight layers at once — a syntax highlighter, a
163/// spell-checker, and one find session *per view*. Each is a session with its own id, so a
164/// per-view [`HighlightMask`] can name exactly the ones a given pane should render.
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
166pub struct SessionId(pub u64);
167
168/// One highlight range carried by a *range session*, in **absolute document char offsets** —
169/// the same coordinate space [`crate::FindMatch`] and replace report in.
170///
171/// A range session is the shape used for search highlighting and (eventually) an
172/// externally-driven spell-checker: the host computes the ranges and hands the whole set over
173/// with [`crate::TextDocument::set_session_ranges`], rather than implementing a per-block
174/// callback. The document slices these absolute ranges to per-block spans at snapshot time
175/// (a block's absolute char start is its `document_position`).
176#[derive(Debug, Clone, PartialEq, Eq)]
177pub struct RangeHighlight {
178    /// Absolute char offset into the document text.
179    pub start: usize,
180    pub length: usize,
181    pub format: HighlightFormat,
182}
183
184/// Which highlight sessions a particular **view** renders.
185///
186/// Two panes over one shared document can carry different find queries, so "which highlights
187/// to show" is a property of the *view*, not the document. A snapshot is built under a mask;
188/// only the sessions the mask admits contribute spans, and the effective
189/// `HighlighterKind` is the join over just those.
190///
191/// The default ([`HighlightMask::all`]) shows every session — the behaviour of the old
192/// `snapshot_flow()`. [`HighlightMask::none`] shows none — the old
193/// `snapshot_flow_without_highlights()`, and it must stay exactly as cheap, since every
194/// read-only preview pane uses it.
195#[derive(Debug, Clone, Default, PartialEq, Eq)]
196pub struct HighlightMask {
197    /// `None` = admit every session (the default). `Some(set)` = admit only these ids.
198    included: Option<Vec<SessionId>>,
199}
200
201impl HighlightMask {
202    /// The all-admitting mask as a `const`, so a snapshot builder can reference "show
203    /// everything" with a `'static` lifetime — no temporary to outlive.
204    pub(crate) const ALL: HighlightMask = HighlightMask { included: None };
205
206    /// Show every session attached to the document. The default, and the shape of a plain
207    /// `snapshot_flow()`.
208    pub fn all() -> Self {
209        Self { included: None }
210    }
211
212    /// Show no highlights at all — as cheap as the old `show_highlights = false`.
213    pub fn none() -> Self {
214        Self {
215            included: Some(Vec::new()),
216        }
217    }
218
219    /// Show only the named sessions (e.g. the shared syntax + spell sessions plus *this*
220    /// view's own find session).
221    pub fn only(ids: impl IntoIterator<Item = SessionId>) -> Self {
222        Self {
223            included: Some(ids.into_iter().collect()),
224        }
225    }
226
227    /// Add a session to a mask that already names some (a no-op on [`HighlightMask::all`],
228    /// which already admits everything).
229    pub fn with(mut self, id: SessionId) -> Self {
230        if let Some(ids) = &mut self.included
231            && !ids.contains(&id)
232        {
233            ids.push(id);
234        }
235        self
236    }
237
238    /// Whether this mask admits `id`.
239    pub(crate) fn admits(&self, id: SessionId) -> bool {
240        match &self.included {
241            None => true,
242            Some(ids) => ids.contains(&id),
243        }
244    }
245
246    /// Whether this mask admits nothing — the fast path an empty/no-op preview takes, which
247    /// must be exactly as cheap as the old boolean `false`.
248    pub(crate) fn is_empty(&self) -> bool {
249        matches!(&self.included, Some(ids) if ids.is_empty())
250    }
251}
252
253/// What a snapshot renders, resolved **once at the root** and threaded down unchanged: the
254/// effective [`HighlighterKind`](enum@HighlighterKind) (the join over the view's admitted
255/// sessions) and the mask that selected them.
256///
257/// This replaces the plain `effective_kind: HighlighterKind` the block/frame builders used to
258/// take — carrying the mask alongside so the leaf that resolves a block's spans knows which
259/// sessions this view shows, without re-deriving the kind per block.
260#[derive(Clone, Copy)]
261pub(crate) struct SnapshotHighlights<'a> {
262    pub kind: HighlighterKind,
263    pub mask: &'a HighlightMask,
264    /// Skip the paint-only overlay (`paint_highlights`) entirely: fragments are
265    /// still split for metric sessions, but no `extract_paint_spans` runs. For
266    /// consumers that read only the fragments/geometry and discard the visual
267    /// overlay — the accessibility tree above all — this drops the whole
268    /// paint-span computation, which is O(spans) per block (superlinear when a
269    /// block carries thousands of ranges, e.g. a spell-checked Lorem scene).
270    /// The produced `fragments` are byte-identical to a normal snapshot; only
271    /// `paint_highlights` differs (always empty here).
272    pub suppress_paint: bool,
273}
274
275// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
276// Internal storage
277// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
278
279/// Per-block highlight state.
280pub(crate) struct BlockHighlightData {
281    pub spans: Vec<HighlightSpan>,
282    pub state: i64,
283    pub user_data: Option<Box<dyn Any + Send + Sync>>,
284}
285
286/// A **syntax session**: a callback highlighter and its per-block cascade cache. This is
287/// exactly the old single-highlighter storage — one `SyntaxHighlighter` invoked once per
288/// block, with its own `previous_state`/`current_state` timeline and per-block user data.
289///
290/// Each syntax session owns its cascade **independently**. A multiline-comment state from one
291/// highlighter must never leak into another's `previous_block_state`, so the state is threaded
292/// per session, never shared.
293pub(crate) struct SyntaxSession {
294    pub highlighter: Arc<dyn SyntaxHighlighter>,
295    pub blocks: HashMap<usize, BlockHighlightData>,
296}
297
298/// A **range session**: absolute-offset ranges, set wholesale by the host and sliced to
299/// per-block spans on demand. No callback, no cascade — used for find and spell.
300///
301/// The ranges carry a **per-block index** and a **cached kind**, both built once by
302/// [`set_ranges`](HighlightRegistry::set_ranges) from the document's block layout at push time.
303/// They replace two per-query full scans of the whole range vector that made a snapshot
304/// O(blocks × ranges): a spell-check of a Lorem-Ipsum-dense document flags one range per word
305/// (tens of thousands of them), and *every* block used to walk *all* of them.
306pub(crate) struct RangeSession {
307    pub ranges: Vec<RangeHighlight>,
308    /// `block_id → indices into `ranges` that overlap that block`, at the block layout of the
309    /// last push. A block **absent** from the map has no ranges for this session.
310    ///
311    /// Freshness: the ranges' absolute offsets and this index are a matched snapshot of one
312    /// push. If the document is edited *without* a following push, both go stale together — the
313    /// same window the un-indexed code already had (it, too, clipped last-push absolute ranges
314    /// against fresh geometry). The one new shape is **missing vs. stale**: a block *created*
315    /// since the last push has no key here, so it shows nothing (rather than a stale span) until
316    /// the next push rebuilds the index. For the spell producer a structural edit re-tokenises
317    /// and re-pushes on the next frame, closing the window; a brand-new empty block has nothing
318    /// to flag anyway.
319    block_index: HashMap<usize, Vec<u32>>,
320    /// The session's [`HighlighterKind`], computed in the same pass as `block_index` so
321    /// [`effective_kind`](HighlightRegistry::effective_kind) is O(1) per session instead of a
322    /// full range scan on every snapshot root.
323    kind: HighlighterKind,
324}
325
326/// The two session shapes.
327pub(crate) enum SessionBody {
328    Syntax(SyntaxSession),
329    Range(RangeSession),
330}
331
332/// One registered session with its stable id and merge priority.
333pub(crate) struct Session {
334    pub id: SessionId,
335    /// Where this session sits in the merge order — see [`HighlightRegistry::sessions`].
336    pub priority: i32,
337    pub body: SessionBody,
338}
339
340/// Every highlight session on the document, sorted by **`(priority, id)`** — which is the merge
341/// order: when two sessions format the same character, the one later in this vector wins, field
342/// by field (see [`merge_overlapping_highlights`]). Replaces the old single
343/// `Option<HighlightData>` slot.
344///
345/// Priority defaults to `0`, where the order degenerates to registration order — the behaviour
346/// every existing caller had. It exists because registration order alone is not something a
347/// layer can rely on: an editor that attaches a background layer when its *view* is created
348/// registers after or before a find session purely according to which the user opened first, so
349/// "the find match paints over the ambient band" would hold in one window and not in the next.
350/// A layer that must lose declares a negative priority and stops caring when it was added.
351#[derive(Default)]
352pub(crate) struct HighlightRegistry {
353    pub sessions: Vec<Session>,
354    next_id: u64,
355    /// The session owned by the classic single-highlighter `set_syntax_highlighter` shim, if
356    /// one is installed. Kept apart so the shim replaces **only its own** session and never a
357    /// spell-checker or find layer another caller added via `add_syntax_session`.
358    shim: Option<SessionId>,
359}
360
361impl HighlightRegistry {
362    /// Mint the next session id.
363    fn alloc_id(&mut self) -> SessionId {
364        let id = SessionId(self.next_id);
365        self.next_id += 1;
366        id
367    }
368
369    /// Insert a session at its place in the `(priority, id)` order.
370    ///
371    /// Ids only ever increase, so appending within a priority band keeps the tie-break right;
372    /// the search finds the first session that outranks `priority` and inserts before it. One
373    /// insertion point for both session kinds is what keeps every downstream iteration —
374    /// span collection and the kind join alike — priority-ordered for free.
375    fn insert_session(&mut self, id: SessionId, priority: i32, body: SessionBody) {
376        let at = self
377            .sessions
378            .iter()
379            .position(|s| s.priority > priority)
380            .unwrap_or(self.sessions.len());
381        self.sessions.insert(at, Session { id, priority, body });
382    }
383
384    /// Register a syntax session (empty cache; the caller rehighlights).
385    pub(crate) fn add_syntax(
386        &mut self,
387        highlighter: Arc<dyn SyntaxHighlighter>,
388        priority: i32,
389    ) -> SessionId {
390        let id = self.alloc_id();
391        self.insert_session(
392            id,
393            priority,
394            SessionBody::Syntax(SyntaxSession {
395                highlighter,
396                blocks: HashMap::new(),
397            }),
398        );
399        id
400    }
401
402    /// Register an empty range session.
403    pub(crate) fn add_range(&mut self, priority: i32) -> SessionId {
404        let id = self.alloc_id();
405        self.insert_session(
406            id,
407            priority,
408            SessionBody::Range(RangeSession {
409                ranges: Vec::new(),
410                block_index: HashMap::new(),
411                kind: HighlighterKind::None,
412            }),
413        );
414        id
415    }
416
417    /// Replace a range session's ranges, building its per-block index and cached kind from the
418    /// document's `block_positions` (each `(block_id, absolute_char_start)`, sorted by start).
419    ///
420    /// Returns the **extent that changed** — `(position, length)` spanning both the ranges that
421    /// went away and the ones that arrived — or `None` if `id` is not a range session (or does
422    /// not exist); a caller handing ranges to a syntax session is a bug, not a silent no-op to
423    /// swallow. `Some((_, 0))` means nothing at all changed.
424    ///
425    /// The extent is what lets a live view recolor just the affected block instead of
426    /// re-snapshotting the whole document on every push. It is the **union of the two sets**
427    /// rather than a precise diff: computing it costs one pass over vectors this function
428    /// already walks, and a caret-driven layer's before/after ranges are adjacent anyway.
429    pub(crate) fn set_ranges(
430        &mut self,
431        id: SessionId,
432        ranges: Vec<RangeHighlight>,
433        block_positions: &[(u64, usize)],
434    ) -> Option<(usize, usize)> {
435        for s in &mut self.sessions {
436            if s.id == id {
437                let SessionBody::Range(r) = &mut s.body else {
438                    return None;
439                };
440                let extent = changed_extent(&r.ranges, &ranges);
441                r.kind = compute_range_kind(&ranges);
442                r.block_index = build_block_index(&ranges, block_positions);
443                r.ranges = ranges;
444                return Some(extent);
445            }
446        }
447        None
448    }
449
450    /// Retire a session. Returns whether it existed.
451    pub(crate) fn remove(&mut self, id: SessionId) -> bool {
452        let before = self.sessions.len();
453        self.sessions.retain(|s| s.id != id);
454        self.sessions.len() != before
455    }
456
457    /// Install / replace / clear the classic single-highlighter shim
458    /// (`set_syntax_highlighter`). Replaces **only** the shim's own session — a spell-checker
459    /// or any other layer registered independently via [`add_syntax`](Self::add_syntax) is left
460    /// untouched. `None` clears the shim.
461    pub(crate) fn set_shim(&mut self, highlighter: Option<Arc<dyn SyntaxHighlighter>>) {
462        if let Some(id) = self.shim.take() {
463            self.remove(id);
464        }
465        if let Some(hl) = highlighter {
466            // The classic single-highlighter shim keeps the default band, so installing one
467            // behaves exactly as it did before priorities existed.
468            self.shim = Some(self.add_syntax(hl, 0));
469        }
470    }
471
472    /// Whether any session is attached.
473    pub(crate) fn is_empty(&self) -> bool {
474        self.sessions.is_empty()
475    }
476}
477
478/// Classification of the active highlighter's output.
479///
480/// Drives whether highlights are merged into the shaping input
481/// (`fragments`) or kept as a separate post-shape recolor overlay.
482#[derive(Debug, Clone, Copy, PartialEq, Eq)]
483pub(crate) enum HighlighterKind {
484    /// No highlighter attached.
485    None,
486    /// Every span touches only paint attributes (colors, underline
487    /// style, underline/overline/strikeout, tooltip). Glyph metrics are
488    /// unchanged, so the layout engine can recolor without reshaping —
489    /// `fragments` stay base and the spans ride as a paint overlay.
490    PaintOnly,
491    /// At least one span touches a metric-affecting field. Highlights
492    /// are merged into `fragments` (reshape required on change).
493    Metric,
494}
495
496/// Returns `true` if this format sets a metric-affecting field, i.e. one that changes glyph
497/// advances or line height: font family / size / weight / bold / italic, letter / word
498/// spacing, or vertical alignment (sub/superscript). The color and underline-decoration fields
499/// are paint-only and never trigger `true`.
500pub(crate) fn format_touches_metrics(f: &HighlightFormat) -> bool {
501    f.font_family.is_some()
502        || f.font_point_size.is_some()
503        || f.font_weight.is_some()
504        || f.font_bold.is_some()
505        || f.font_italic.is_some()
506        || f.letter_spacing.is_some()
507        || f.word_spacing.is_some()
508        || f.vertical_alignment.is_some()
509}
510
511/// Returns `true` if any span sets a metric-affecting field. See [`format_touches_metrics`].
512pub(crate) fn spans_touch_metrics(spans: &[HighlightSpan]) -> bool {
513    spans.iter().any(|s| format_touches_metrics(&s.format))
514}
515
516impl HighlighterKind {
517    /// None < PaintOnly < Metric. The join over a view's admitted sessions is the max: one
518    /// metric-affecting session forces the reshape path for the whole snapshot.
519    fn rank(self) -> u8 {
520        match self {
521            HighlighterKind::None => 0,
522            HighlighterKind::PaintOnly => 1,
523            HighlighterKind::Metric => 2,
524        }
525    }
526
527    fn join(self, other: HighlighterKind) -> HighlighterKind {
528        if other.rank() > self.rank() {
529            other
530        } else {
531            self
532        }
533    }
534}
535
536/// The kind of one syntax session, from its cached spans.
537fn syntax_session_kind(s: &SyntaxSession) -> HighlighterKind {
538    let mut any = false;
539    for bd in s.blocks.values() {
540        if spans_touch_metrics(&bd.spans) {
541            return HighlighterKind::Metric;
542        }
543        any |= !bd.spans.is_empty();
544    }
545    if any {
546        HighlighterKind::PaintOnly
547    } else {
548        HighlighterKind::None
549    }
550}
551
552/// The span of document affected by replacing `old` with `new`, as `(position, length)`.
553///
554/// Ranges identical in both sets contribute nothing — that is what makes a caret-driven layer
555/// cheap: re-pushing an unchanged set reports a zero length, and a caret that moved one
556/// sentence reports only the two sentences involved.
557///
558/// A zero length means "nothing changed", never "the whole document" — an empty-to-empty push
559/// really does affect nothing.
560///
561/// **Linear, by design.** This runs inside every `set_ranges`, and a spell-checked scene pushes
562/// tens of thousands of ranges on each edit, so comparing the sets by membership (quadratic)
563/// would cost more than the snapshot it exists to avoid — `range_index_perf` pins exactly that.
564/// Instead it skips the identical head and tail element-wise and covers only the window
565/// between them. Every producer builds its ranges in document order, so that window is tight;
566/// were one ever to reorder them, the result would merely be *wider* than necessary, which
567/// costs a larger recolor and never a wrong one.
568fn changed_extent(old: &[RangeHighlight], new: &[RangeHighlight]) -> (usize, usize) {
569    let common = old.len().min(new.len());
570
571    let mut head = 0;
572    while head < common && old[head] == new[head] {
573        head += 1;
574    }
575    if head == old.len() && head == new.len() {
576        return (0, 0);
577    }
578    let mut tail = 0;
579    while tail < common - head && old[old.len() - 1 - tail] == new[new.len() - 1 - tail] {
580        tail += 1;
581    }
582
583    let (mut lo, mut hi) = (usize::MAX, 0usize);
584    for r in old[head..old.len() - tail]
585        .iter()
586        .chain(&new[head..new.len() - tail])
587    {
588        lo = lo.min(r.start);
589        hi = hi.max(r.start + r.length);
590    }
591    if lo == usize::MAX {
592        (0, 0)
593    } else {
594        (lo, hi - lo)
595    }
596}
597
598/// The kind a set of ranges implies, from their formats. Computed **once** at
599/// [`set_ranges`](HighlightRegistry::set_ranges) time and cached on the [`RangeSession`], so
600/// [`effective_kind`](HighlightRegistry::effective_kind) never rescans the whole vector.
601fn compute_range_kind(ranges: &[RangeHighlight]) -> HighlighterKind {
602    let mut any = false;
603    for r in ranges {
604        if format_touches_metrics(&r.format) {
605            return HighlighterKind::Metric;
606        }
607        any |= r.length > 0;
608    }
609    if any {
610        HighlighterKind::PaintOnly
611    } else {
612        HighlighterKind::None
613    }
614}
615
616/// Bucket each range into every block it overlaps, from the document's block layout at push
617/// time (`block_positions` = each `(block_id, absolute_char_start)`, **sorted by start**).
618///
619/// A block spans `[start_i, start_{i+1})` in absolute char space (the last runs to `MAX`); a
620/// range is bucketed into a block when their half-open spans intersect. Almost always that is a
621/// single block — the spell/find producers emit ranges within one paragraph — but a range that
622/// happens to straddle a boundary is added to **every** block it touches, so the per-block clip
623/// downstream sees it in each, exactly as the old full scan did.
624fn build_block_index(
625    ranges: &[RangeHighlight],
626    block_positions: &[(u64, usize)],
627) -> HashMap<usize, Vec<u32>> {
628    let mut index: HashMap<usize, Vec<u32>> = HashMap::new();
629    if block_positions.is_empty() {
630        return index;
631    }
632    for (ri, r) in ranges.iter().enumerate() {
633        let r_end = r.start.saturating_add(r.length); // exclusive
634        // The block containing `r.start`: the last block whose start <= r.start.
635        let mut bi = match block_positions.binary_search_by_key(&r.start, |&(_, p)| p) {
636            Ok(i) => i,
637            Err(i) => i.saturating_sub(1),
638        };
639        // Walk forward over every block the range still overlaps.
640        while bi < block_positions.len() {
641            let b_start = block_positions[bi].1;
642            if b_start >= r_end {
643                break; // this block (and all later ones) start past the range
644            }
645            let b_end = block_positions
646                .get(bi + 1)
647                .map(|&(_, p)| p)
648                .unwrap_or(usize::MAX);
649            // Half-open intersection [b_start, b_end) ∩ [r.start, r_end).
650            if r.start < b_end && b_start < r_end {
651                index
652                    .entry(block_positions[bi].0 as usize)
653                    .or_default()
654                    .push(ri as u32);
655            }
656            bi += 1;
657        }
658    }
659    index
660}
661
662impl HighlightRegistry {
663    /// The effective [`HighlighterKind`](enum@HighlighterKind) for a view — the join over the
664    /// sessions the mask admits. Computed **once at the snapshot root** and threaded down as a
665    /// plain value, exactly like the old single document-wide kind; a view showing only
666    /// paint-only sessions never pays the reshape path for a metric session it does not show.
667    pub(crate) fn effective_kind(&self, mask: &HighlightMask) -> HighlighterKind {
668        if mask.is_empty() {
669            return HighlighterKind::None;
670        }
671        let mut kind = HighlighterKind::None;
672        for s in &self.sessions {
673            if !mask.admits(s.id) {
674                continue;
675            }
676            let k = match &s.body {
677                SessionBody::Syntax(syn) => syntax_session_kind(syn),
678                SessionBody::Range(r) => r.kind, // cached at set_ranges — no rescan
679            };
680            kind = kind.join(k);
681            if kind == HighlighterKind::Metric {
682                break;
683            }
684        }
685        kind
686    }
687}
688
689// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
690// Merge algorithm
691// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
692
693/// Apply highlight format overrides onto a base `TextFormat`.
694fn apply_highlight(base: &TextFormat, hl: &HighlightFormat) -> TextFormat {
695    TextFormat {
696        font_family: hl.font_family.clone().or_else(|| base.font_family.clone()),
697        font_point_size: hl.font_point_size.or(base.font_point_size),
698        font_weight: hl.font_weight.or(base.font_weight),
699        font_bold: hl.font_bold.or(base.font_bold),
700        font_italic: hl.font_italic.or(base.font_italic),
701        font_underline: hl.font_underline.or(base.font_underline),
702        font_overline: hl.font_overline.or(base.font_overline),
703        font_strikeout: hl.font_strikeout.or(base.font_strikeout),
704        letter_spacing: hl.letter_spacing.or(base.letter_spacing),
705        word_spacing: hl.word_spacing.or(base.word_spacing),
706        underline_style: hl
707            .underline_style
708            .clone()
709            .or_else(|| base.underline_style.clone()),
710        vertical_alignment: hl
711            .vertical_alignment
712            .clone()
713            .or_else(|| base.vertical_alignment.clone()),
714        tooltip: hl.tooltip.clone().or_else(|| base.tooltip.clone()),
715        foreground_color: hl.foreground_color.or(base.foreground_color),
716        background_color: hl.background_color.or(base.background_color),
717        underline_color: hl.underline_color.or(base.underline_color),
718        // Anchors are not overridden by highlights.
719        anchor_href: base.anchor_href.clone(),
720        anchor_names: base.anchor_names.clone(),
721        is_anchor: base.is_anchor,
722    }
723}
724
725/// Merge a set of overlapping highlights into a single `HighlightFormat`.
726/// Later spans override earlier spans for the same field.
727fn merge_overlapping_highlights(spans: &[&HighlightSpan]) -> HighlightFormat {
728    let mut merged = HighlightFormat::default();
729    for span in spans {
730        let f = &span.format;
731        if f.foreground_color.is_some() {
732            merged.foreground_color = f.foreground_color;
733        }
734        if f.background_color.is_some() {
735            merged.background_color = f.background_color;
736        }
737        if f.underline_color.is_some() {
738            merged.underline_color = f.underline_color;
739        }
740        if f.font_family.is_some() {
741            merged.font_family = f.font_family.clone();
742        }
743        if f.font_point_size.is_some() {
744            merged.font_point_size = f.font_point_size;
745        }
746        if f.font_weight.is_some() {
747            merged.font_weight = f.font_weight;
748        }
749        if f.font_bold.is_some() {
750            merged.font_bold = f.font_bold;
751        }
752        if f.font_italic.is_some() {
753            merged.font_italic = f.font_italic;
754        }
755        if f.font_underline.is_some() {
756            merged.font_underline = f.font_underline;
757        }
758        if f.font_overline.is_some() {
759            merged.font_overline = f.font_overline;
760        }
761        if f.font_strikeout.is_some() {
762            merged.font_strikeout = f.font_strikeout;
763        }
764        if f.letter_spacing.is_some() {
765            merged.letter_spacing = f.letter_spacing;
766        }
767        if f.word_spacing.is_some() {
768            merged.word_spacing = f.word_spacing;
769        }
770        if f.underline_style.is_some() {
771            merged.underline_style = f.underline_style.clone();
772        }
773        if f.vertical_alignment.is_some() {
774            merged.vertical_alignment = f.vertical_alignment.clone();
775        }
776        if f.tooltip.is_some() {
777            merged.tooltip = f.tooltip.clone();
778        }
779    }
780    merged
781}
782
783/// Flatten a block's stored highlight spans into a list of
784/// [`PaintHighlightSpan`](crate::flow::PaintHighlightSpan)s for the
785/// paint-overlay path.
786///
787/// Only called when the active highlighter is [`HighlighterKind::PaintOnly`],
788/// so metric fields are guaranteed absent and ignored here. Overlapping
789/// spans are resolved exactly like `merge_highlight_spans` (split at every
790/// boundary, last-wins per field) so the overlay matches what the merged
791/// path would have produced. `block_len` is the block's character length.
792/// Sub-ranges with no paint field set are skipped.
793pub(crate) fn extract_paint_spans(
794    spans: &[HighlightSpan],
795    block_len: usize,
796) -> Vec<crate::flow::PaintHighlightSpan> {
797    if spans.is_empty() || block_len == 0 {
798        return Vec::new();
799    }
800
801    // Collect and dedupe all span boundaries within (0, block_len).
802    let mut boundaries = vec![0usize, block_len];
803    for s in spans {
804        let end = s.start.saturating_add(s.length);
805        if s.start > 0 && s.start < block_len {
806            boundaries.push(s.start);
807        }
808        if end > 0 && end < block_len {
809            boundaries.push(end);
810        }
811    }
812    boundaries.sort_unstable();
813    boundaries.dedup();
814
815    // Sweep the boundaries left→right, maintaining the set of spans active in the
816    // current window in ORIGINAL-INDEX order (a `BTreeSet` of indices). This
817    // replaces the former O(boundaries × spans) rescan — which re-filtered every
818    // span at every boundary and went quadratic on a block carrying thousands of
819    // ranges (a spell-checked Lorem paragraph, where every window still walked all
820    // ~m ranges) — with O(m log m + Σ|active|). The emitted spans are byte-identical:
821    // `BTreeSet` iterates indices ascending, the same order the old
822    // `spans.iter().filter()` produced, so `merge_overlapping_highlights` sees the
823    // same (last-wins) sequence. Each span is inserted and removed exactly once as
824    // the two monotonic pointers advance.
825    let n = spans.len();
826    let mut by_start: Vec<usize> = (0..n).collect();
827    by_start.sort_by_key(|&i| spans[i].start);
828    let mut by_end: Vec<usize> = (0..n).collect();
829    by_end.sort_by_key(|&i| spans[i].start.saturating_add(spans[i].length));
830
831    let mut active: std::collections::BTreeSet<usize> = std::collections::BTreeSet::new();
832    let mut ps = 0usize; // next span to activate (ordered by start)
833    let mut pe = 0usize; // next span to deactivate (ordered by end)
834    let mut scratch: Vec<&HighlightSpan> = Vec::new();
835
836    let mut result = Vec::new();
837    for w in boundaries.windows(2) {
838        let (sub_start, sub_end) = (w[0], w[1]);
839        if sub_end <= sub_start {
840            continue;
841        }
842        // A span is active in [sub_start, sub_end) iff start <= sub_start < end.
843        // Activate every span that has started by the left edge, then deactivate
844        // every span that has ended by it — add-before-remove so a zero-length
845        // span (start == end == sub_start) is excluded, matching the old strict
846        // `end > sub_start` test.
847        while ps < n && spans[by_start[ps]].start <= sub_start {
848            active.insert(by_start[ps]);
849            ps += 1;
850        }
851        while pe < n
852            && spans[by_end[pe]]
853                .start
854                .saturating_add(spans[by_end[pe]].length)
855                <= sub_start
856        {
857            active.remove(&by_end[pe]);
858            pe += 1;
859        }
860        if active.is_empty() {
861            continue;
862        }
863        scratch.clear();
864        scratch.extend(active.iter().map(|&i| &spans[i]));
865        let merged = merge_overlapping_highlights(&scratch);
866        if merged.foreground_color.is_none()
867            && merged.background_color.is_none()
868            && merged.underline_color.is_none()
869            && merged.underline_style.is_none()
870            && merged.font_underline.is_none()
871            && merged.font_overline.is_none()
872            && merged.font_strikeout.is_none()
873        {
874            continue;
875        }
876        result.push(crate::flow::PaintHighlightSpan {
877            start: sub_start,
878            length: sub_end - sub_start,
879            foreground_color: merged.foreground_color,
880            background_color: merged.background_color,
881            underline_color: merged.underline_color,
882            underline_style: merged.underline_style,
883            font_underline: merged.font_underline,
884            font_overline: merged.font_overline,
885            font_strikeout: merged.font_strikeout,
886        });
887    }
888    result
889}
890
891/// Merge highlight spans into a list of fragments.
892///
893/// Text fragments that overlap with highlight spans are split at span
894/// boundaries. The highlight format is overlaid onto the base `TextFormat`.
895/// Image fragments receive the overlay without splitting.
896/// Local copy of the word-start computation from `text_block.rs`:
897/// returns character indices (not byte offsets) where a Unicode word
898/// starts, per UAX #29. Mirrors the upstream helper so highlight
899/// splits produce accessibility-correct word_starts for each
900/// sub-fragment without reaching into `text_block`.
901fn compute_word_starts_local(text: &str) -> Vec<u8> {
902    use unicode_segmentation::UnicodeSegmentation;
903    let mut result = Vec::new();
904    let mut byte_to_char: Vec<(usize, usize)> = Vec::new();
905    for (ci, (bi, _)) in text.char_indices().enumerate() {
906        byte_to_char.push((bi, ci));
907    }
908    for (byte_off, _word) in text.unicode_word_indices() {
909        let char_idx = byte_to_char
910            .iter()
911            .find(|(bi, _)| *bi == byte_off)
912            .map(|(_, ci)| *ci)
913            .unwrap_or(0);
914        if let Ok(idx) = u8::try_from(char_idx) {
915            result.push(idx);
916        } else {
917            break;
918        }
919    }
920    result
921}
922
923pub(crate) fn merge_highlight_spans(
924    fragments: Vec<FragmentContent>,
925    spans: &[HighlightSpan],
926) -> Vec<FragmentContent> {
927    if spans.is_empty() {
928        return fragments;
929    }
930
931    let mut result = Vec::with_capacity(fragments.len());
932
933    for frag in fragments {
934        match frag {
935            FragmentContent::Text {
936                ref text,
937                ref format,
938                offset,
939                length,
940                element_id,
941                word_starts: _,
942            } => {
943                let frag_end = offset + length;
944
945                // Collect highlight boundaries within this fragment's range.
946                let mut boundaries = Vec::new();
947                boundaries.push(offset);
948                boundaries.push(frag_end);
949
950                for span in spans {
951                    let span_end = span.start + span.length;
952                    // Does this span overlap the fragment?
953                    if span.start < frag_end && span_end > offset {
954                        if span.start > offset && span.start < frag_end {
955                            boundaries.push(span.start);
956                        }
957                        if span_end > offset && span_end < frag_end {
958                            boundaries.push(span_end);
959                        }
960                    }
961                }
962
963                boundaries.sort_unstable();
964                boundaries.dedup();
965
966                // Split the text at each boundary and apply overlapping highlights.
967                let chars: Vec<char> = text.chars().collect();
968                for window in boundaries.windows(2) {
969                    let sub_start = window[0];
970                    let sub_end = window[1];
971                    let sub_len = sub_end - sub_start;
972                    if sub_len == 0 {
973                        continue;
974                    }
975
976                    // Collect all highlight spans overlapping [sub_start, sub_end).
977                    let active: Vec<&HighlightSpan> = spans
978                        .iter()
979                        .filter(|s| {
980                            let s_end = s.start + s.length;
981                            s.start < sub_end && s_end > sub_start
982                        })
983                        .collect();
984
985                    let char_start = sub_start - offset;
986                    let char_end = char_start + sub_len;
987                    let sub_text: String = chars[char_start..char_end].iter().collect();
988
989                    let sub_format = if active.is_empty() {
990                        format.clone()
991                    } else {
992                        let merged_hl = merge_overlapping_highlights(&active);
993                        apply_highlight(format, &merged_hl)
994                    };
995
996                    let sub_word_starts = compute_word_starts_local(&sub_text);
997                    result.push(FragmentContent::Text {
998                        text: sub_text,
999                        format: sub_format,
1000                        offset: sub_start,
1001                        length: sub_len,
1002                        // All sub-fragments split from one source
1003                        // `FragmentContent::Text` reference the same
1004                        // underlying format run — only the highlight
1005                        // formatting differs. Sharing the id is
1006                        // correct for accessibility (the underlying
1007                        // text belongs to one stable run) at the cost
1008                        // that synthetic NodeIds for highlighted
1009                        // sub-runs collide unless the caller further
1010                        // disambiguates.
1011                        // The bastyde-widgets layer handles that by
1012                        // mixing the `offset` into the synthetic-id
1013                        // hash alongside `element_id`.
1014                        element_id,
1015                        word_starts: sub_word_starts,
1016                    });
1017                }
1018            }
1019            FragmentContent::Image {
1020                ref name,
1021                ref alt,
1022                width,
1023                height,
1024                quality,
1025                ref format,
1026                offset,
1027                element_id,
1028            } => {
1029                // Find overlapping highlights for this single-char position.
1030                let active: Vec<&HighlightSpan> = spans
1031                    .iter()
1032                    .filter(|s| {
1033                        let s_end = s.start + s.length;
1034                        s.start < offset + 1 && s_end > offset
1035                    })
1036                    .collect();
1037
1038                let img_format = if active.is_empty() {
1039                    format.clone()
1040                } else {
1041                    let merged_hl = merge_overlapping_highlights(&active);
1042                    apply_highlight(format, &merged_hl)
1043                };
1044
1045                result.push(FragmentContent::Image {
1046                    name: name.clone(),
1047                    alt: alt.clone(),
1048                    width,
1049                    height,
1050                    quality,
1051                    format: img_format,
1052                    offset,
1053                    element_id,
1054                });
1055            }
1056            // A reference is a single-character position too, so it takes a
1057            // highlight the same way an image does — a find hit or a spell
1058            // range covering it must recolour the marker, not skip it.
1059            FragmentContent::FootnoteReference {
1060                ref label,
1061                ref marker,
1062                ref format,
1063                offset,
1064                element_id,
1065            } => {
1066                let active: Vec<&HighlightSpan> = spans
1067                    .iter()
1068                    .filter(|s| {
1069                        let s_end = s.start + s.length;
1070                        s.start < offset + 1 && s_end > offset
1071                    })
1072                    .collect();
1073
1074                let note_format = if active.is_empty() {
1075                    format.clone()
1076                } else {
1077                    let merged_hl = merge_overlapping_highlights(&active);
1078                    apply_highlight(format, &merged_hl)
1079                };
1080
1081                result.push(FragmentContent::FootnoteReference {
1082                    label: label.clone(),
1083                    marker: marker.clone(),
1084                    format: note_format,
1085                    offset,
1086                    element_id,
1087                });
1088            }
1089        }
1090    }
1091
1092    result
1093}
1094
1095// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1096// Re-highlighting
1097// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1098
1099/// Every block's id + text, sorted by `document_position`. The text materialization is the
1100/// expensive part (rope → String for the whole document), so callers that only need block
1101/// *positions* use [`ordered_block_positions`] instead.
1102fn ordered_block_ids(inner: &TextDocumentInner) -> Vec<(u64, String)> {
1103    let mut blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
1104    let store = inner.ctx.db_context.get_store();
1105    crate::inner::refresh_block_positions(&mut blocks, store);
1106    blocks.sort_by_key(|b| b.document_position);
1107    blocks
1108        .into_iter()
1109        .map(|b| {
1110            let entity: common::entities::Block = b.clone().into();
1111            let text = common::database::rope_helpers::block_content_via_store(&entity, store);
1112            (b.id, text)
1113        })
1114        .collect()
1115}
1116
1117/// Every block's id + absolute char start (`document_position`), sorted — **without**
1118/// materializing any block text. This is the cheap sibling of [`ordered_block_ids`]; the
1119/// double full-document scan it exists to prevent is described on [`TextDocumentInner::rehighlight_affected`].
1120pub(crate) fn ordered_block_positions(inner: &TextDocumentInner) -> Vec<(u64, usize)> {
1121    let mut blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
1122    let store = inner.ctx.db_context.get_store();
1123    crate::inner::refresh_block_positions(&mut blocks, store);
1124    blocks.sort_by_key(|b| b.document_position);
1125    blocks
1126        .into_iter()
1127        .map(|b| (b.id, b.document_position.max(0) as usize))
1128        .collect()
1129}
1130
1131/// A block's absolute char start and char length — the geometry a range session needs to
1132/// slice its absolute ranges to this block. `document_position` is the block's absolute char
1133/// start, exactly as the find/replace path resolves offsets against it.
1134fn block_geometry(inner: &TextDocumentInner, block_id: usize) -> (usize, usize) {
1135    let store = inner.ctx.db_context.get_store();
1136    let Some(mut dto) = block_commands::get_block(&inner.ctx, &(block_id as u64))
1137        .ok()
1138        .flatten()
1139    else {
1140        return (0, 0);
1141    };
1142    crate::inner::refresh_block_position(&mut dto, store);
1143    let abs_start = dto.document_position.max(0) as usize;
1144    let entity: common::entities::Block = dto.into();
1145    let len = common::database::rope_helpers::block_char_length(&entity, store).max(0) as usize;
1146    (abs_start, len)
1147}
1148
1149/// The block-relative highlight spans a **view** sees for one block: every session the mask
1150/// admits, in registration order (so a later session's field overrides an earlier one), with
1151/// range sessions' absolute ranges sliced to this block.
1152///
1153/// The block geometry needed to slice range sessions is fetched **lazily** — a document with
1154/// only syntax sessions (or a mask that admits none) pays nothing for it.
1155pub(crate) fn merged_spans_for_block(
1156    inner: &TextDocumentInner,
1157    block_id: usize,
1158    mask: &HighlightMask,
1159) -> Vec<HighlightSpan> {
1160    if mask.is_empty() || inner.highlights.is_empty() {
1161        return Vec::new();
1162    }
1163
1164    let mut geom: Option<(usize, usize)> = None;
1165    let mut out: Vec<HighlightSpan> = Vec::new();
1166
1167    for s in &inner.highlights.sessions {
1168        if !mask.admits(s.id) {
1169            continue;
1170        }
1171        match &s.body {
1172            SessionBody::Syntax(syn) => {
1173                if let Some(bd) = syn.blocks.get(&block_id) {
1174                    out.extend(bd.spans.iter().cloned());
1175                }
1176            }
1177            SessionBody::Range(r) => {
1178                // Only the ranges the index says touch this block — O(ranges-in-block), not
1179                // O(all-ranges). A block absent from the index (empty bucket) contributes
1180                // nothing; see [`RangeSession::block_index`] on the missing-vs-stale window.
1181                let Some(indices) = r.block_index.get(&block_id) else {
1182                    continue;
1183                };
1184                let (abs_start, len) = *geom.get_or_insert_with(|| block_geometry(inner, block_id));
1185                let block_end = abs_start + len;
1186                for &ri in indices {
1187                    let rng = &r.ranges[ri as usize];
1188                    // Saturating: a range session's offsets come from the host (an externally
1189                    // driven spell-checker included), so a wild `start + length` must clamp, not
1190                    // overflow-panic in debug / wrap in release.
1191                    let lo = rng.start.max(abs_start);
1192                    let hi = rng.start.saturating_add(rng.length).min(block_end);
1193                    if lo < hi {
1194                        out.push(HighlightSpan {
1195                            start: lo - abs_start,
1196                            length: hi - lo,
1197                            format: rng.format.clone(),
1198                        });
1199                    }
1200                }
1201            }
1202        }
1203    }
1204    out
1205}
1206
1207impl TextDocumentInner {
1208    /// Re-highlight every block, for every **syntax** session (range sessions carry their
1209    /// ranges directly and never run a callback).
1210    ///
1211    /// Each syntax session runs its **own** cascade — its own `previous_state` timeline,
1212    /// reset to −1 here, and its own per-block cache. State never crosses between sessions, or
1213    /// one highlighter's multiline-comment run would corrupt another's. The block text is
1214    /// materialized **once** and shared across sessions, though: the rope → String scan is the
1215    /// cost, and running it per session would be N× for no reason.
1216    pub(crate) fn rehighlight_all(&mut self) {
1217        if self.highlights.is_empty() {
1218            self.recompute_highlight_kind();
1219            return;
1220        }
1221        let blocks = ordered_block_ids(self);
1222
1223        for si in 0..self.highlights.sessions.len() {
1224            let SessionBody::Syntax(syn) = &self.highlights.sessions[si].body else {
1225                continue;
1226            };
1227            let highlighter = Arc::clone(&syn.highlighter);
1228
1229            let mut fresh: HashMap<usize, BlockHighlightData> = HashMap::new();
1230            let mut previous_state: i64 = -1;
1231            for (block_id, text) in &blocks {
1232                let bid = *block_id as usize;
1233                let mut ctx = HighlightContext::new(bid, previous_state, None);
1234                highlighter.highlight_block(text, &mut ctx);
1235                let (spans, state, user_data) = ctx.into_parts();
1236                previous_state = state;
1237                fresh.insert(
1238                    bid,
1239                    BlockHighlightData {
1240                        spans,
1241                        state,
1242                        user_data,
1243                    },
1244                );
1245            }
1246            if let SessionBody::Syntax(syn) = &mut self.highlights.sessions[si].body {
1247                syn.blocks = fresh;
1248            }
1249        }
1250
1251        self.recompute_highlight_kind();
1252    }
1253
1254    /// Recompute the cached document-wide [`highlight_kind`](TextDocumentInner::highlight_kind)
1255    /// — the effective kind under the all-sessions mask, which is what the unmasked
1256    /// `snapshot_flow()` uses. Masked snapshots derive their own kind at the root.
1257    pub(crate) fn recompute_highlight_kind(&mut self) {
1258        self.highlight_kind = self.highlights.effective_kind(&HighlightMask::all());
1259    }
1260
1261    /// This syntax session's cached block state (−1 if unset / not a syntax session).
1262    fn syntax_block_state(&self, session_idx: usize, block_id: usize) -> i64 {
1263        match &self.highlights.sessions[session_idx].body {
1264            SessionBody::Syntax(s) => s.blocks.get(&block_id).map_or(-1, |d| d.state),
1265            _ => -1,
1266        }
1267    }
1268
1269    /// Re-highlight from a block for every syntax session, cascading each until its own state
1270    /// stabilizes.
1271    pub(crate) fn rehighlight_from_block(&mut self, start_block_id: usize) {
1272        if self.highlights.is_empty() {
1273            return;
1274        }
1275        let blocks = ordered_block_ids(self);
1276        let Some(start_idx) = blocks
1277            .iter()
1278            .position(|(id, _)| *id as usize == start_block_id)
1279        else {
1280            return;
1281        };
1282
1283        for si in 0..self.highlights.sessions.len() {
1284            if matches!(self.highlights.sessions[si].body, SessionBody::Syntax(_)) {
1285                self.rehighlight_session_from(si, start_idx, &blocks);
1286            }
1287        }
1288
1289        self.recompute_highlight_kind();
1290    }
1291
1292    /// One syntax session's cascade from `start_idx` (see [`Self::rehighlight_from_block`]).
1293    fn rehighlight_session_from(
1294        &mut self,
1295        session_idx: usize,
1296        start_idx: usize,
1297        blocks: &[(u64, String)],
1298    ) {
1299        let highlighter = match &self.highlights.sessions[session_idx].body {
1300            SessionBody::Syntax(s) => Arc::clone(&s.highlighter),
1301            _ => return,
1302        };
1303
1304        for i in start_idx..blocks.len() {
1305            let (block_id, ref text) = blocks[i];
1306            let bid = block_id as usize;
1307
1308            let previous_state = if i == 0 {
1309                -1
1310            } else {
1311                self.syntax_block_state(session_idx, blocks[i - 1].0 as usize)
1312            };
1313
1314            // Reuse the block's existing user data (this session's own).
1315            let user_data = match &mut self.highlights.sessions[session_idx].body {
1316                SessionBody::Syntax(s) => s.blocks.get_mut(&bid).and_then(|d| d.user_data.take()),
1317                _ => None,
1318            };
1319            let old_state = self.syntax_block_state(session_idx, bid);
1320
1321            let mut ctx = HighlightContext::new(bid, previous_state, user_data);
1322            highlighter.highlight_block(text, &mut ctx);
1323            let (spans, state, user_data) = ctx.into_parts();
1324
1325            if let SessionBody::Syntax(s) = &mut self.highlights.sessions[session_idx].body {
1326                s.blocks.insert(
1327                    bid,
1328                    BlockHighlightData {
1329                        spans,
1330                        state,
1331                        user_data,
1332                    },
1333                );
1334            }
1335
1336            // Past the start and the state is unchanged: this session's cascade has settled.
1337            if i > start_idx && state == old_state {
1338                break;
1339            }
1340        }
1341    }
1342
1343    /// Re-highlight the blocks a content change at `position` affects.
1344    ///
1345    /// The target block is found from a **positions-only** scan (no block text materialized),
1346    /// then [`rehighlight_from_block`](Self::rehighlight_from_block) does the one text scan it
1347    /// needs. The old code materialized every block's text here *just to locate one block*, and
1348    /// `rehighlight_from_block` then materialized them all again — two full-document rope →
1349    /// String passes on every single keystroke, at N=1.
1350    pub(crate) fn rehighlight_affected(&mut self, position: usize) {
1351        if self.highlights.is_empty() {
1352            return;
1353        }
1354        let positions = ordered_block_positions(self);
1355        if positions.is_empty() {
1356            return;
1357        }
1358        // The last block whose start is at or before `position` contains it.
1359        let target_bid = positions
1360            .iter()
1361            .rev()
1362            .find(|(_, bp)| position >= *bp)
1363            .map(|(id, _)| *id as usize)
1364            .unwrap_or_else(|| positions[0].0 as usize);
1365
1366        self.rehighlight_from_block(target_bid);
1367    }
1368}
1369
1370#[cfg(test)]
1371mod paint_span_tests {
1372    use super::*;
1373
1374    /// The previous O(boundaries × spans) implementation, kept verbatim as the
1375    /// oracle the sweep must reproduce byte-for-byte.
1376    fn extract_paint_spans_reference(
1377        spans: &[HighlightSpan],
1378        block_len: usize,
1379    ) -> Vec<crate::flow::PaintHighlightSpan> {
1380        if spans.is_empty() || block_len == 0 {
1381            return Vec::new();
1382        }
1383        let mut boundaries = vec![0usize, block_len];
1384        for s in spans {
1385            let end = s.start.saturating_add(s.length);
1386            if s.start > 0 && s.start < block_len {
1387                boundaries.push(s.start);
1388            }
1389            if end > 0 && end < block_len {
1390                boundaries.push(end);
1391            }
1392        }
1393        boundaries.sort_unstable();
1394        boundaries.dedup();
1395        let mut result = Vec::new();
1396        for w in boundaries.windows(2) {
1397            let (sub_start, sub_end) = (w[0], w[1]);
1398            if sub_end <= sub_start {
1399                continue;
1400            }
1401            let active: Vec<&HighlightSpan> = spans
1402                .iter()
1403                .filter(|s| s.start < sub_end && s.start + s.length > sub_start)
1404                .collect();
1405            if active.is_empty() {
1406                continue;
1407            }
1408            let merged = merge_overlapping_highlights(&active);
1409            if merged.foreground_color.is_none()
1410                && merged.background_color.is_none()
1411                && merged.underline_color.is_none()
1412                && merged.underline_style.is_none()
1413                && merged.font_underline.is_none()
1414                && merged.font_overline.is_none()
1415                && merged.font_strikeout.is_none()
1416            {
1417                continue;
1418            }
1419            result.push(crate::flow::PaintHighlightSpan {
1420                start: sub_start,
1421                length: sub_end - sub_start,
1422                foreground_color: merged.foreground_color,
1423                background_color: merged.background_color,
1424                underline_color: merged.underline_color,
1425                underline_style: merged.underline_style,
1426                font_underline: merged.font_underline,
1427                font_overline: merged.font_overline,
1428                font_strikeout: merged.font_strikeout,
1429            });
1430        }
1431        result
1432    }
1433
1434    /// A span whose single paint field is keyed by `k`, so that last-wins merging
1435    /// across overlaps produces observably different output when the active-set
1436    /// ORDER is wrong — the property most at risk in the rewrite.
1437    fn span(start: usize, length: usize, k: usize) -> HighlightSpan {
1438        let c = |v: usize| crate::Color {
1439            red: v as u8,
1440            green: (v >> 8) as u8,
1441            blue: 7,
1442            alpha: 255,
1443        };
1444        let format = match k % 4 {
1445            0 => HighlightFormat {
1446                background_color: Some(c(k)),
1447                ..Default::default()
1448            },
1449            1 => HighlightFormat {
1450                foreground_color: Some(c(k)),
1451                ..Default::default()
1452            },
1453            2 => HighlightFormat {
1454                underline_color: Some(c(k)),
1455                ..Default::default()
1456            },
1457            // No paint field: must be dropped by both implementations.
1458            _ => HighlightFormat {
1459                font_bold: Some(true),
1460                ..Default::default()
1461            },
1462        };
1463        HighlightSpan {
1464            start,
1465            length,
1466            format,
1467        }
1468    }
1469
1470    #[test]
1471    fn sweep_matches_reference_on_edge_cases() {
1472        let cases: Vec<(Vec<HighlightSpan>, usize)> = vec![
1473            (vec![], 10),
1474            (vec![span(0, 4, 0)], 0),                  // empty block
1475            (vec![span(0, 5, 0)], 10),                 // single
1476            (vec![span(0, 3, 0), span(5, 3, 1)], 10),  // disjoint
1477            (vec![span(2, 5, 0), span(4, 5, 1)], 12),  // overlap: later wins in [4,7)
1478            (vec![span(0, 10, 0), span(3, 2, 1)], 10), // nested
1479            (vec![span(0, 3, 0), span(3, 3, 1)], 10),  // adjacent (touch, no overlap)
1480            (vec![span(4, 0, 0)], 10),                 // zero-length → nothing
1481            (vec![span(0, 4, 3)], 10),                 // no paint field → dropped
1482            (vec![span(8, 5, 0)], 10),                 // spills past block_len
1483            (vec![span(12, 3, 0)], 10),                // entirely past block_len
1484            (vec![span(0, 4, 0), span(0, 4, 1), span(0, 4, 2)], 10), // coincident, order matters
1485        ];
1486        for (i, (spans, len)) in cases.iter().enumerate() {
1487            assert_eq!(
1488                extract_paint_spans(spans, *len),
1489                extract_paint_spans_reference(spans, *len),
1490                "edge case {i}: spans={spans:?} block_len={len}"
1491            );
1492        }
1493    }
1494
1495    #[test]
1496    fn sweep_matches_reference_randomized() {
1497        // Deterministic LCG — no rng dependency, reproducible across runs.
1498        let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
1499        let mut next = |bound: usize| -> usize {
1500            state = state
1501                .wrapping_mul(6364136223846793005)
1502                .wrapping_add(1442695040888963407);
1503            ((state >> 33) as usize) % bound.max(1)
1504        };
1505        for trial in 0..2000 {
1506            let block_len = 1 + next(40);
1507            let m = next(14); // 0..13 spans, a mix of overlap densities
1508            let mut spans = Vec::with_capacity(m);
1509            for k in 0..m {
1510                let start = next(block_len + 4); // sometimes at/past the end
1511                let length = next(block_len + 2); // sometimes zero, sometimes spilling over
1512                spans.push(span(start, length, trial + k));
1513            }
1514            assert_eq!(
1515                extract_paint_spans(&spans, block_len),
1516                extract_paint_spans_reference(&spans, block_len),
1517                "trial {trial}: spans={spans:?} block_len={block_len}"
1518            );
1519        }
1520    }
1521}
1522
1523#[cfg(test)]
1524mod index_tests {
1525    use super::*;
1526
1527    fn paint(start: usize, length: usize) -> RangeHighlight {
1528        RangeHighlight {
1529            start,
1530            length,
1531            format: HighlightFormat {
1532                background_color: Some(crate::Color {
1533                    red: 255,
1534                    green: 0,
1535                    blue: 0,
1536                    alpha: 255,
1537                }),
1538                ..Default::default()
1539            },
1540        }
1541    }
1542
1543    fn metric(start: usize, length: usize) -> RangeHighlight {
1544        RangeHighlight {
1545            start,
1546            length,
1547            format: HighlightFormat {
1548                font_bold: Some(true),
1549                ..Default::default()
1550            },
1551        }
1552    }
1553
1554    // ── compute_range_kind (B2-M2): the cached kind matches the old per-range classification ──
1555
1556    #[test]
1557    fn kind_is_none_when_nothing_paints() {
1558        assert_eq!(compute_range_kind(&[]), HighlighterKind::None);
1559        // A zero-length paint range colours nothing → None.
1560        assert_eq!(compute_range_kind(&[paint(3, 0)]), HighlighterKind::None);
1561    }
1562
1563    #[test]
1564    fn kind_is_paint_only_for_a_background_range() {
1565        assert_eq!(
1566            compute_range_kind(&[paint(0, 4)]),
1567            HighlighterKind::PaintOnly
1568        );
1569    }
1570
1571    #[test]
1572    fn kind_is_metric_when_any_range_touches_metrics() {
1573        // One metric range among paint ones lifts the whole session to Metric (a bold run
1574        // reshapes, so the view must take the reshape path).
1575        assert_eq!(
1576            compute_range_kind(&[paint(0, 2), metric(4, 3), paint(8, 1)]),
1577            HighlighterKind::Metric
1578        );
1579    }
1580
1581    #[test]
1582    fn set_ranges_caches_the_kind_read_back_by_effective_kind() {
1583        let mut reg = HighlightRegistry::default();
1584        let id = reg.add_range(0);
1585        let positions = [(0u64, 0usize)];
1586        assert_eq!(
1587            reg.effective_kind(&HighlightMask::all()),
1588            HighlighterKind::None
1589        );
1590
1591        reg.set_ranges(id, vec![paint(0, 5)], &positions);
1592        assert_eq!(
1593            reg.effective_kind(&HighlightMask::all()),
1594            HighlighterKind::PaintOnly
1595        );
1596
1597        reg.set_ranges(id, vec![metric(0, 5)], &positions);
1598        assert_eq!(
1599            reg.effective_kind(&HighlightMask::all()),
1600            HighlighterKind::Metric,
1601            "the cached kind updates on every push"
1602        );
1603    }
1604
1605    // ── changed_extent: what a push actually touched ──
1606
1607    /// Re-pushing an identical set touched nothing, so a view has nothing to recolor. This is
1608    /// the case that matters most: a caret-driven layer re-pushes constantly.
1609    #[test]
1610    fn an_unchanged_push_reports_an_empty_extent() {
1611        let set = [paint(10, 5), paint(30, 5)];
1612        assert_eq!(changed_extent(&set, &set), (0, 0));
1613        assert_eq!(changed_extent(&[], &[]), (0, 0));
1614    }
1615
1616    /// A caret moving from one sentence to the next reports only the span covering both, so the
1617    /// recolor stays local instead of falling back to the whole document.
1618    #[test]
1619    fn a_moved_range_reports_the_span_covering_both_positions() {
1620        // 10..15 gone, 20..25 arrived → 10..25.
1621        assert_eq!(changed_extent(&[paint(10, 5)], &[paint(20, 5)]), (10, 15));
1622    }
1623
1624    #[test]
1625    fn adding_or_clearing_reports_just_that_range() {
1626        assert_eq!(changed_extent(&[], &[paint(7, 3)]), (7, 3));
1627        assert_eq!(changed_extent(&[paint(7, 3)], &[]), (7, 3));
1628    }
1629
1630    /// Ranges that survive the push contribute nothing, so a find session that keeps most of
1631    /// its matches reports only the ones that actually moved.
1632    #[test]
1633    fn unchanged_ranges_are_excluded_from_the_extent() {
1634        let old = [paint(0, 2), paint(50, 2)];
1635        let new = [paint(0, 2), paint(60, 2)];
1636        assert_eq!(changed_extent(&old, &new), (50, 12), "only the moved one");
1637    }
1638
1639    /// A format change at the same offsets — what a theme switch does — still reports the range,
1640    /// because `RangeHighlight` compares its format too.
1641    #[test]
1642    fn a_format_only_change_still_reports_its_range() {
1643        assert_eq!(changed_extent(&[paint(4, 6)], &[metric(4, 6)]), (4, 6));
1644    }
1645
1646    // ── build_block_index: bucketing ──
1647
1648    /// Blocks at 0, 10, 20 (each 10 wide). Ranges land in the block(s) they overlap.
1649    fn three_blocks() -> Vec<(u64, usize)> {
1650        vec![(100, 0), (101, 10), (102, 20)]
1651    }
1652
1653    fn bucket(index: &std::collections::HashMap<usize, Vec<u32>>, block: usize) -> Vec<u32> {
1654        index.get(&block).cloned().unwrap_or_default()
1655    }
1656
1657    #[test]
1658    fn a_range_lands_only_in_its_own_block() {
1659        let idx = build_block_index(&[paint(12, 3)], &three_blocks());
1660        assert_eq!(
1661            bucket(&idx, 101),
1662            vec![0],
1663            "12..15 is inside block 101 [10,20)"
1664        );
1665        assert!(bucket(&idx, 100).is_empty());
1666        assert!(bucket(&idx, 102).is_empty());
1667    }
1668
1669    #[test]
1670    fn a_straddling_range_lands_in_every_block_it_touches() {
1671        // 8..22 spans blocks 100 [0,10), 101 [10,20), 102 [20,∞).
1672        let idx = build_block_index(&[paint(8, 14)], &three_blocks());
1673        assert_eq!(bucket(&idx, 100), vec![0]);
1674        assert_eq!(bucket(&idx, 101), vec![0]);
1675        assert_eq!(bucket(&idx, 102), vec![0]);
1676    }
1677
1678    #[test]
1679    fn a_zero_length_range_buckets_into_its_block_but_paints_nothing() {
1680        // A degenerate zero-length range still buckets into the block that contains its point
1681        // (here 101), which is harmless: the per-block clip drops it (lo == hi), so it paints
1682        // nothing — proven end-to-end by the coverage differential in the integration tests.
1683        // It must not scatter into other blocks.
1684        let idx = build_block_index(&[paint(12, 0)], &three_blocks());
1685        assert_eq!(bucket(&idx, 101), vec![0]);
1686        assert!(bucket(&idx, 100).is_empty());
1687        assert!(bucket(&idx, 102).is_empty());
1688    }
1689
1690    #[test]
1691    fn an_out_of_range_start_is_bucketed_into_the_last_block_only_if_it_overlaps() {
1692        // start far past the end: only the last (unbounded) block could contain it, and it does
1693        // — the last block runs to usize::MAX — so it buckets there. That is harmless: the
1694        // per-block clip against real geometry drops it (start > block_end).
1695        let idx = build_block_index(&[paint(9999, 3)], &three_blocks());
1696        assert_eq!(
1697            bucket(&idx, 102),
1698            vec![0],
1699            "the unbounded last block is the only candidate"
1700        );
1701        assert!(bucket(&idx, 100).is_empty());
1702        assert!(bucket(&idx, 101).is_empty());
1703    }
1704
1705    #[test]
1706    fn empty_positions_yields_an_empty_index() {
1707        assert!(build_block_index(&[paint(0, 5)], &[]).is_empty());
1708    }
1709}