Skip to main content

Module preview_highlight

Module preview_highlight 

Source
Expand description

Shared text helpers for the note-preview surfaces (the Query panel’s context preview and the note browser’s preview pane). Both highlight query needles in a note’s body and wrap long lines; the case-insensitive matching is the one piece that is both subtle (must stay on character boundaries for every non-ASCII case fold) and was previously implemented twice — once correctly, once dropping highlights for length-changing folds. It lives here now; each surface keeps its own span styling.

Functions§

match_ranges
Non-overlapping byte ranges in haystack where any of needles matches, case-insensitively, earliest- and longest-first.
style_ranges
Walk line against precomputed non-overlapping ranges (from match_ranges, ascending) and emit one item per segment via mk(slice, is_match) — alternating non-match gaps and matched spans, plus the trailing gap. Empty ranges yields the whole line as a single non-match segment. Shared so the gap/match/tail splitting lives in one place; each caller supplies its own constructor (owned 'static spans vs borrowed 'a spans, with its own styles).
wrap_line
Wrap line into pieces that each fit within max_width characters (not bytes). Breaks at word boundaries when possible, hard-breaks an over-long word otherwise. A max_width of 0 returns the line unchanged.