Skip to main content

fresh/view/
line_wrap_cache.rs

1//! Line-wrap pipeline-output cache.
2//!
3//! A bounded per-buffer cache from `LineWrapKey` to `Arc<Vec<ViewLine>>` —
4//! the final output of the render pipeline for a single logical line.
5//!
6//! See `docs/internal/line-wrap-cache-plan.md` for the full design.  In
7//! brief:
8//!
9//! * **Single source of truth.**  The value stored is what the renderer
10//!   actually produces.  Every consumer that needs to know "how many
11//!   visual rows?", "where does byte X land visually?", "what byte is at
12//!   visual column N?" reads the same `ViewLine` structures via the
13//!   methods `ViewLine` already exposes (`source_byte_at_char`,
14//!   `char_at_visual_col`, `source_byte_at_visual_col`, `visual_col_at_char`,
15//!   `visual_width`).  No second implementation to drift from.
16//!
17//! * **Two writers, one pipeline.**  The renderer populates cache entries
18//!   as a side effect of its normal per-frame work; the miss handler in
19//!   this module runs the same four-step pipeline scoped to a single
20//!   logical line.  Same inputs → same output.
21//!
22//! * **Invalidation by key.**  The key includes `pipeline_inputs_version`
23//!   (a packed u64 derived from `buffer.version()`, `SoftBreakManager::
24//!   version()`, and `ConcealManager::version()`) plus every geometry /
25//!   view dimension the pipeline reads.  Mutating any of those produces a
26//!   different key; old entries become unreachable and age out via FIFO
27//!   eviction.  There is no active invalidate step.
28//!
29//! * **Byte-budget eviction.**  Because `Vec<ViewLine>` sizes vary from
30//!   a few hundred bytes for a short line to megabytes for a long line
31//!   wrapping into thousands of rows, count-based eviction is the wrong
32//!   metric.  The cache tracks approximate total memory and evicts
33//!   oldest-first when a new insert would exceed the byte budget.
34//!
35//! Structural invariants maintained at all times:
36//!
37//!     self.map.len() == self.order.len()
38//!     self.current_bytes <= self.byte_budget  (after any insert)
39
40use crate::state::EditorState;
41use crate::view::ui::split_rendering::base_tokens::build_base_tokens;
42use crate::view::ui::split_rendering::transforms::{
43    apply_conceal_ranges, apply_soft_breaks, apply_wrapping_transform, splice_inline_virtual_text,
44};
45use crate::view::ui::view_pipeline::{ViewLine, ViewLineIterator};
46use fresh_core::api::ViewTokenWireKind;
47use std::collections::{HashMap, VecDeque};
48use std::sync::Arc;
49
50/// Default byte budget: 8 MiB.  Comfortably holds the full layout for a
51/// small-to-medium buffer, a handful of huge lines, or any interactive
52/// scroll span.  A single 200 KB line wrapping to ~2000 rows takes
53/// roughly 2 MB in its `Vec<ViewLine>` form, so the budget can absorb
54/// several such lines before churning.
55pub const DEFAULT_BYTE_BUDGET: usize = 8 * 1024 * 1024;
56
57/// View mode the pipeline is running in.  Conceals and some plugin-
58/// rendered content only apply in Compose.  Kept as a small plain enum
59/// so the key stays cheap to hash.
60#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
61pub enum CacheViewMode {
62    Source,
63    Compose,
64}
65
66/// Full set of inputs that determine a single logical line's wrapped
67/// layout.  Every mutable input must be represented here — if the
68/// caller forgets one, stale entries can be returned.
69///
70/// The `pipeline_inputs_version` folds in the buffer version plus the
71/// soft-break and conceal managers' versions (see
72/// [`pipeline_inputs_version`]).  The remaining fields are geometry /
73/// viewport config.
74#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
75pub struct LineWrapKey {
76    pub pipeline_inputs_version: u64,
77    pub view_mode: CacheViewMode,
78    pub line_start: usize,
79    pub effective_width: u32,
80    pub gutter_width: u16,
81    pub wrap_column: Option<u32>,
82    pub hanging_indent: bool,
83    pub line_wrap_enabled: bool,
84}
85
86/// Derive the combined pipeline-inputs version from the three source
87/// versions.  Any change to any of them flips the combined value.  This
88/// is not a hash — it's a packed integer with enough bit-budget to make
89/// accidental collisions astronomically unlikely in a single session.
90///
91/// * `buffer_version` gets the low 32 bits (wrapped to u32).  Buffer
92///   edits are the most frequent source of change.
93/// * `soft_breaks_version` is shifted up 32 bits.
94/// * `conceal_version` is shifted up 48 bits.
95/// * `virtual_text_version` is shifted up 16 bits.  Folded so that
96///   adding / removing plugin virtual lines (e.g.
97///   markdown_compose's table borders, git blame headers)
98///   invalidates the same caches the other three sources do —
99///   `VisualRowIndex` adds virtual line counts to its prefix sums and
100///   would otherwise serve a stale total when the plugin re-tiles a
101///   table.
102#[inline]
103pub fn pipeline_inputs_version(
104    buffer_version: u64,
105    soft_breaks_version: u32,
106    conceal_version: u32,
107    virtual_text_version: u32,
108) -> u64 {
109    (buffer_version & 0xFFFF_FFFF)
110        ^ ((soft_breaks_version as u64) << 32)
111        ^ ((conceal_version as u64) << 48)
112        ^ ((virtual_text_version as u64) << 16)
113}
114
115/// Estimate the in-memory size of a `Vec<ViewLine>` for byte-budget
116/// accounting.  Rough but stable — we'd rather over- than under-estimate
117/// so the budget stays honest.
118///
119/// Per `ViewLine`:
120///   - `text` (String): bytes in the rendered text
121///   - `char_source_bytes` (Vec<Option<usize>>): 16 bytes × chars
122///   - `char_styles` (Vec<Option<ViewTokenStyle>>): ~32 bytes × chars
123///   - `char_visual_cols` (Vec<usize>): 8 bytes × chars
124///   - `visual_to_char` (Vec<usize>): 8 bytes × visual cols
125///   - overhead (HashSet, enum, bool, alignment padding): ~64 bytes
126///
127/// Round up to `visual_width * 64 + text.len() + 96` for simplicity.
128fn estimate_view_lines_bytes(lines: &[ViewLine]) -> usize {
129    let mut total = 48; // Arc + Vec overhead
130    for line in lines {
131        let chars = line.char_source_bytes.len();
132        let visual = line.visual_to_char.len();
133        total += line.text.len() + chars * 56 + visual * 8 + 96;
134    }
135    total
136}
137
138/// Bounded FIFO cache from `LineWrapKey` to `Arc<Vec<ViewLine>>`.
139///
140/// FIFO (not LRU) because the dominant access pattern is sequential
141/// scrolling: each line is queried a few times in close succession, then
142/// rarely again.  FIFO is simpler to reason about and matches this
143/// pattern well enough.  If future profiling shows churn we can swap the
144/// eviction policy — the external API doesn't change.
145#[derive(Debug, Clone)]
146pub struct LineWrapCache {
147    map: HashMap<LineWrapKey, Arc<Vec<ViewLine>>>,
148    order: VecDeque<LineWrapKey>,
149    byte_budget: usize,
150    current_bytes: usize,
151}
152
153impl Default for LineWrapCache {
154    fn default() -> Self {
155        Self::with_byte_budget(DEFAULT_BYTE_BUDGET)
156    }
157}
158
159impl LineWrapCache {
160    pub fn with_byte_budget(byte_budget: usize) -> Self {
161        assert!(byte_budget > 0, "LineWrapCache byte_budget must be > 0");
162        Self {
163            map: HashMap::new(),
164            order: VecDeque::new(),
165            byte_budget,
166            current_bytes: 0,
167        }
168    }
169
170    pub fn len(&self) -> usize {
171        debug_assert_eq!(
172            self.map.len(),
173            self.order.len(),
174            "LineWrapCache invariant: map.len() == order.len()"
175        );
176        self.map.len()
177    }
178
179    pub fn is_empty(&self) -> bool {
180        self.len() == 0
181    }
182
183    pub fn byte_budget(&self) -> usize {
184        self.byte_budget
185    }
186
187    pub fn current_bytes(&self) -> usize {
188        self.current_bytes
189    }
190
191    /// Look up a cached value.  Returns `None` on miss.  The returned
192    /// `Arc` is a cheap clone; callers can hold it without copying the
193    /// underlying `Vec<ViewLine>`.
194    pub fn get(&self, key: &LineWrapKey) -> Option<Arc<Vec<ViewLine>>> {
195        self.map.get(key).cloned()
196    }
197
198    /// Query by key; on miss, run `compute` and store its result.  The
199    /// primary entry point for both the renderer's write path and the
200    /// scroll-math miss handler.
201    ///
202    /// Returns the (possibly just-computed) value as an `Arc`.  The
203    /// `compute` closure is called at most once per cache miss; hits do
204    /// not invoke it.
205    pub fn get_or_insert_with<F>(&mut self, key: LineWrapKey, compute: F) -> Arc<Vec<ViewLine>>
206    where
207        F: FnOnce() -> Vec<ViewLine>,
208    {
209        if let Some(v) = self.map.get(&key) {
210            return v.clone();
211        }
212        let value = Arc::new(compute());
213        self.insert_fresh(key, value.clone());
214        value
215    }
216
217    /// Unconditionally store a value for `key`.  If `key` is already
218    /// present, its value is replaced in place and its FIFO position is
219    /// **not** changed (this keeps the queue simple — re-inserts don't
220    /// refresh age).  Byte-budget accounting is updated.
221    pub fn put(&mut self, key: LineWrapKey, value: Arc<Vec<ViewLine>>) {
222        if let Some(existing) = self.map.get_mut(&key) {
223            let old_bytes = estimate_view_lines_bytes(existing);
224            let new_bytes = estimate_view_lines_bytes(&value);
225            *existing = value;
226            self.current_bytes = self.current_bytes + new_bytes - old_bytes.min(self.current_bytes);
227            return;
228        }
229        self.insert_fresh(key, value);
230    }
231
232    /// Remove all entries.  Used by tests and by future
233    /// plugin-lifecycle events.
234    pub fn clear(&mut self) {
235        self.map.clear();
236        self.order.clear();
237        self.current_bytes = 0;
238    }
239
240    /// Insert a never-before-seen key, evicting oldest-first until the
241    /// new entry fits inside `byte_budget`.
242    fn insert_fresh(&mut self, key: LineWrapKey, value: Arc<Vec<ViewLine>>) {
243        debug_assert!(!self.map.contains_key(&key));
244        let new_bytes = estimate_view_lines_bytes(&value);
245
246        // Evict until (current_bytes + new_bytes) fits.  Always keep at
247        // least one slot — if the single new entry alone exceeds the
248        // budget, we still accept it (the cache was asked to hold it;
249        // the alternative is silently dropping data the caller just
250        // paid to compute).
251        while self.current_bytes + new_bytes > self.byte_budget && !self.order.is_empty() {
252            if let Some(oldest_key) = self.order.pop_front() {
253                if let Some(oldest_val) = self.map.remove(&oldest_key) {
254                    let shed = estimate_view_lines_bytes(&oldest_val);
255                    self.current_bytes = self.current_bytes.saturating_sub(shed);
256                }
257            }
258        }
259
260        self.map.insert(key, value);
261        self.order.push_back(key);
262        self.current_bytes += new_bytes;
263        debug_assert_eq!(self.map.len(), self.order.len());
264    }
265}
266
267/// Materialise a line's layout as `Vec<ViewLine>` from plain text
268/// alone — no buffer iteration, no soft breaks, no conceals.
269///
270/// Useful at sites that have `line_text: &str` in hand and can't
271/// easily reach `EditorState` (or are inside a `line_iterator` borrow).
272/// The produced `ViewLine`s match the renderer's word-boundary wrap
273/// on the same text at the same geometry, so row counts and cursor
274/// mappings agree with `layout_for_line` in the absence of soft
275/// breaks / conceals.  When soft breaks or conceals ARE active for
276/// the line, callers should prefer `layout_for_line` to get accurate
277/// layout.
278pub fn layout_for_plain_text(
279    line_text: &str,
280    effective_width: usize,
281    gutter_width: usize,
282    hanging_indent: bool,
283    tab_size: usize,
284) -> Vec<ViewLine> {
285    use crate::view::ui::view_pipeline::LineStart;
286    use fresh_core::api::ViewTokenWire;
287    let tokens = vec![ViewTokenWire {
288        source_offset: Some(0),
289        kind: ViewTokenWireKind::Text(line_text.to_string()),
290        style: None,
291    }];
292    let wrapped = apply_wrapping_transform(tokens, effective_width, gutter_width, hanging_indent);
293    let mut lines: Vec<ViewLine> =
294        ViewLineIterator::new(&wrapped, false, true, tab_size, false).collect();
295    // Invariant: every logical line is at least one visual row.  An
296    // empty input produces zero ViewLines through the iterator; emit
297    // one placeholder so callers (scrollbar row counts, scroll math)
298    // see consistent ≥1 results matching `compute_line_layout`.
299    if lines.is_empty() {
300        lines.push(ViewLine {
301            text: String::new(),
302            source_start_byte: Some(0),
303            char_source_bytes: Vec::new(),
304            char_styles: Vec::new(),
305            char_visual_cols: Vec::new(),
306            visual_to_char: Vec::new(),
307            tab_starts: std::collections::HashSet::new(),
308            line_start: LineStart::Beginning,
309            ends_with_newline: false,
310            virtual_gutter_glyph: None,
311            virtual_line_style: None,
312        });
313    }
314    lines
315}
316
317/// Look up a line's layout in the cache, running the mini-pipeline to
318/// fill on miss.  The primary read-path entry point for consumers that
319/// need full `ViewLine` layout (not just row count).
320///
321/// Guarantees that the value returned matches what the renderer would
322/// produce for the same line under the same pipeline inputs: same
323/// function chain is called either way, so cache hit and miss are
324/// indistinguishable to the caller.
325pub fn layout_for_line(
326    state: &mut EditorState,
327    line_start: usize,
328    line_end: usize,
329    geom: &WrapGeometry,
330) -> Arc<Vec<ViewLine>> {
331    let version = pipeline_inputs_version(
332        state.buffer.version(),
333        state.soft_breaks.version(),
334        state.conceals.version(),
335        state.virtual_texts.version(),
336    );
337    let key = geom.key(line_start, version);
338    if let Some(cached) = state.line_wrap_cache.get(&key) {
339        return cached;
340    }
341    let layout = compute_line_layout(state, line_start, line_end, geom);
342    let arc = Arc::new(layout);
343    state.line_wrap_cache.put(key, arc.clone());
344    arc
345}
346
347/// Given a logical line's layout and a character position within the
348/// LOGICAL line (not the ViewLine), return `(segment_idx,
349/// col_in_segment)` — the index of the `ViewLine` the character falls
350/// into, and the visual column within that `ViewLine`.
351///
352/// Replaces `primitives::line_wrapping::char_position_to_segment` for
353/// callers that have a cached `Vec<ViewLine>`.
354///
355/// The trick: continuation `ViewLine`s can carry hanging-indent
356/// characters at their start whose `source_offset` is `None` (they
357/// don't correspond to any source byte).  Those chars must NOT count
358/// toward the source-character position we're walking past.  So we
359/// sum *source* characters per row (char_source_bytes entries that
360/// are `Some(_)`) to find the row containing `char_pos_in_line`, and
361/// within that row we locate the specific char whose source_offset
362/// matches.
363///
364/// If `layout` is empty, returns `(0, 0)`.  If the position is past
365/// the end of the last row, returns the last row with the last
366/// visual column of that row.
367pub fn char_position_in_layout(layout: &[ViewLine], char_pos_in_line: usize) -> (usize, usize) {
368    if layout.is_empty() {
369        return (0, 0);
370    }
371    let mut source_chars_consumed = 0usize;
372    for (i, line) in layout.iter().enumerate() {
373        let source_chars_in_row = line
374            .char_source_bytes
375            .iter()
376            .filter(|b| b.is_some())
377            .count();
378        if char_pos_in_line < source_chars_consumed + source_chars_in_row {
379            // The target source-char is in this row.  Find the
380            // `char_idx` whose position-among-source-chars equals
381            // the within-row offset, then convert to visual column.
382            let within_row = char_pos_in_line - source_chars_consumed;
383            let mut source_count = 0usize;
384            for (char_idx, byte) in line.char_source_bytes.iter().enumerate() {
385                if byte.is_some() {
386                    if source_count == within_row {
387                        return (i, line.visual_col_at_char(char_idx));
388                    }
389                    source_count += 1;
390                }
391            }
392            // Fallback: shouldn't happen given the length check above,
393            // but don't return garbage if it does.
394            return (i, line.visual_width().saturating_sub(1));
395        }
396        source_chars_consumed += source_chars_in_row;
397    }
398    // Past the end: return the last row's last visual column.  (A
399    // cursor one past the last source char on the last row lands
400    // here.)
401    let last_idx = layout.len() - 1;
402    let last = &layout[last_idx];
403    let last_col = last.visual_width().saturating_sub(1);
404    (last_idx, last_col)
405}
406
407/// Geometry + view config inputs to the wrap pipeline that aren't carried
408/// by `EditorState`.  Bundled so the plumbing through call sites doesn't
409/// grow a laundry list of parameters.
410#[derive(Debug, Clone, Copy)]
411pub struct WrapGeometry {
412    pub effective_width: usize,
413    pub gutter_width: usize,
414    pub hanging_indent: bool,
415    pub wrap_column: Option<u32>,
416    pub line_wrap_enabled: bool,
417    pub view_mode: CacheViewMode,
418}
419
420impl WrapGeometry {
421    /// Build a cache key for a logical line at `line_start` under these
422    /// geometry and pipeline-input versions.
423    pub fn key(&self, line_start: usize, pipeline_inputs_version: u64) -> LineWrapKey {
424        LineWrapKey {
425            pipeline_inputs_version,
426            view_mode: self.view_mode,
427            line_start,
428            effective_width: self.effective_width as u32,
429            gutter_width: self.gutter_width as u16,
430            wrap_column: self.wrap_column,
431            hanging_indent: self.hanging_indent,
432            line_wrap_enabled: self.line_wrap_enabled,
433        }
434    }
435}
436
437/// Run the same pipeline the renderer runs, scoped to exactly one
438/// logical line starting at `line_start`, and return the rendered
439/// [`ViewLine`]s for that line.  Used by the cache miss handler.
440///
441/// When `geom.line_wrap_enabled` is false, returns a single
442/// placeholder `ViewLine` — an unwrapped line always occupies exactly
443/// one visual row.  (Callers that only need a count can read
444/// `.len()`; callers that need coordinate mappings would not query
445/// this path with wrapping off.)
446///
447/// The four pipeline steps mirror `view_data::build_view_data`:
448///   1. `build_base_tokens(top_byte=line_start, count=1)`
449///   2. `apply_soft_breaks` (Compose mode, when any soft breaks overlap)
450///   3. `apply_conceal_ranges` (Compose mode, when any conceals overlap)
451///   4. `apply_wrapping_transform`
452/// followed by `ViewLineIterator::collect()` to materialise the
453/// `Vec<ViewLine>`.
454///
455/// The result is what the renderer would produce for this single
456/// logical line — the single source of truth the cache exists to
457/// share.
458pub fn compute_line_layout(
459    state: &mut EditorState,
460    line_start: usize,
461    line_end: usize,
462    geom: &WrapGeometry,
463) -> Vec<ViewLine> {
464    let is_binary = state.buffer.is_binary();
465    let line_ending = state.buffer.line_ending();
466    let estimated_line_length = state.buffer.estimated_line_length();
467    let tab_size = state.buffer_settings.tab_size;
468
469    // Step 1: build tokens for just this one logical line.
470    let mut tokens = build_base_tokens(
471        &mut state.buffer,
472        line_start,
473        estimated_line_length,
474        1, // just this one logical line
475        is_binary,
476        line_ending,
477        &[], // no fold skip ranges — folds affect what's rendered, not per-line wrap count
478    );
479
480    let is_compose = matches!(geom.view_mode, CacheViewMode::Compose);
481
482    // Step 2: soft breaks (Compose mode only; same gating as the renderer).
483    if is_compose && !state.soft_breaks.is_empty() {
484        let sb = state
485            .soft_breaks
486            .query_viewport(line_start, line_end, &state.marker_list);
487        if !sb.is_empty() {
488            tokens = apply_soft_breaks(tokens, &sb);
489        }
490    }
491
492    // Step 3: conceal ranges (Compose mode only).
493    if is_compose && !state.conceals.is_empty() {
494        let cr = state
495            .conceals
496            .query_viewport(line_start, line_end, &state.marker_list);
497        if !cr.is_empty() {
498            tokens = apply_conceal_ranges(tokens, &cr);
499        }
500    }
501
502    // Step 3.5: splice inline virtual text (inlay hints) so this per-line
503    // layout matches the renderer's — its width must affect wrap boundaries
504    // and visual-column counts identically. `theme` is `None`: this output
505    // feeds scroll-math / coordinate queries (never drawn), so only cell
506    // width matters, not colour.
507    if !state.virtual_texts.is_empty() {
508        tokens = splice_inline_virtual_text(tokens, state, None, line_start, line_end);
509    }
510
511    // Step 4: wrap (only when line-wrap is actually enabled).  When
512    // disabled, pass tokens through unchanged; ViewLineIterator will
513    // still yield one ViewLine per Newline boundary.
514    if geom.line_wrap_enabled {
515        tokens = apply_wrapping_transform(
516            tokens,
517            geom.effective_width,
518            geom.gutter_width,
519            geom.hanging_indent,
520        );
521    }
522
523    // Materialise the ViewLines.  `build_base_tokens` may emit tokens
524    // for more than one logical line; collect only the first logical
525    // line's ViewLines (those up to and including the first Newline).
526    let all_lines: Vec<ViewLine> =
527        ViewLineIterator::new(&tokens, is_binary, !is_binary, tab_size, false).collect();
528
529    // The `ViewLineIterator` produces one `ViewLine` per visual row.
530    // The Newline tokens inside split the stream at logical-line
531    // boundaries: every `ViewLine` after the first whose `line_start`
532    // is `AfterSourceNewline` begins a NEW logical line, which we
533    // don't want.  Keep only rows up to (but not including) the first
534    // such transition.
535    let mut result = Vec::with_capacity(all_lines.len().min(8));
536    for (i, line) in all_lines.into_iter().enumerate() {
537        use crate::view::ui::view_pipeline::LineStart;
538        if i > 0 && matches!(line.line_start, LineStart::AfterSourceNewline) {
539            break;
540        }
541        result.push(line);
542    }
543    if result.is_empty() {
544        // Defensive: even a completely empty logical line corresponds
545        // to exactly one visual row.  The iterator should always
546        // produce at least one, but be safe.
547        result.push(ViewLine {
548            text: String::new(),
549            source_start_byte: Some(line_start),
550            char_source_bytes: Vec::new(),
551            char_styles: Vec::new(),
552            char_visual_cols: Vec::new(),
553            visual_to_char: Vec::new(),
554            tab_starts: std::collections::HashSet::new(),
555            line_start: crate::view::ui::view_pipeline::LineStart::Beginning,
556            ends_with_newline: false,
557            virtual_gutter_glyph: None,
558            virtual_line_style: None,
559        });
560    }
561    result
562}
563
564/// Row count only.  Thin wrapper over [`compute_line_layout`] for
565/// callers that need just the visual-row count — scroll math,
566/// thumb-size math.  Prefer calling through the cache
567/// (`get_or_insert_with(key, || compute_line_layout(...)).len()`).
568pub fn count_visual_rows_via_pipeline(
569    state: &mut EditorState,
570    line_start: usize,
571    line_end: usize,
572    geom: &WrapGeometry,
573) -> u32 {
574    compute_line_layout(state, line_start, line_end, geom).len() as u32
575}
576
577/// Combined version of all pipeline inputs on the given state.  Fold into
578/// a `LineWrapKey` to make stale entries unreachable on any mutation.
579#[inline]
580pub fn state_pipeline_inputs_version(state: &EditorState) -> u64 {
581    pipeline_inputs_version(
582        state.buffer.version(),
583        state.soft_breaks.version(),
584        state.conceals.version(),
585        state.virtual_texts.version(),
586    )
587}
588
589/// Build a placeholder `Vec<ViewLine>` of a given row count for cache
590/// consumers that only need `.len()` (e.g. scroll math's count-only
591/// queries, or the per-viewport row-count memoization).  The returned
592/// `ViewLine`s have empty char/visual mappings — they carry no real
593/// layout information.
594///
595/// This exists because the cache is typed on `Vec<ViewLine>` so the
596/// cross-consumer path can share real layout, but some call sites
597/// don't yet have access to `EditorState` (needed by
598/// [`compute_line_layout`]).  When those sites are migrated to take
599/// `&mut EditorState`, this helper can go away.
600pub fn placeholder_layout_for_row_count(n: u32) -> Vec<ViewLine> {
601    use crate::view::ui::view_pipeline::LineStart;
602    (0..n)
603        .map(|_| ViewLine {
604            text: String::new(),
605            source_start_byte: None,
606            char_source_bytes: Vec::new(),
607            char_styles: Vec::new(),
608            char_visual_cols: Vec::new(),
609            visual_to_char: Vec::new(),
610            tab_starts: std::collections::HashSet::new(),
611            line_start: LineStart::Beginning,
612            ends_with_newline: false,
613            virtual_gutter_glyph: None,
614            virtual_line_style: None,
615        })
616        .collect()
617}
618
619/// Count visual rows for a single line's text after applying the
620/// plugin's soft breaks AND the renderer's word-wrap.  Mirrors the
621/// renderer's full pipeline (`apply_soft_breaks` → `apply_wrapping_transform`)
622/// so the scroll math agrees row-for-row with the rendered output even
623/// when the plugin has injected breaks at narrower-than-viewport
624/// widths (e.g. markdown_compose's per-paragraph wrap).
625///
626/// `soft_breaks_in_line` is the slice of `(byte_position, indent)` pairs
627/// for breaks falling **inside** `[line_start, line_start + line_text.len())`.
628/// Callers should pre-filter from the buffer-wide list.
629///
630/// When `soft_breaks_in_line` is empty this is a thin wrapper over
631/// [`count_visual_rows_for_text`].
632pub fn count_visual_rows_for_text_with_soft_breaks(
633    line_text: &str,
634    line_start: usize,
635    soft_breaks_in_line: &[(usize, u16)],
636    effective_width: usize,
637    gutter_width: usize,
638    hanging_indent: bool,
639) -> u32 {
640    if soft_breaks_in_line.is_empty() {
641        return count_visual_rows_for_text(
642            line_text,
643            effective_width,
644            gutter_width,
645            hanging_indent,
646        );
647    }
648
649    let mut total: u32 = 0;
650    let mut prev_end: usize = 0; // byte offset within `line_text`
651    let mut prev_indent: u16 = 0;
652
653    for &(pos, indent) in soft_breaks_in_line {
654        // Defensive: callers pre-filter, but ignore anything out of
655        // range so a stale break list can't OOB-slice the line.
656        if pos < line_start {
657            continue;
658        }
659        let rel = pos - line_start;
660        if rel >= line_text.len() {
661            continue;
662        }
663        if rel < prev_end {
664            // Break list is sorted; this would only fire on a
665            // duplicate or a not-byte-aligned offset.  Skip rather
666            // than panic.
667            continue;
668        }
669        let segment = &line_text[prev_end..rel];
670        total = total.saturating_add(count_segment_rows_with_indent(
671            segment,
672            prev_indent,
673            effective_width,
674            gutter_width,
675            hanging_indent,
676        ));
677        // The renderer's `apply_soft_breaks` consumes the Space token
678        // *at* the break position when one is present (see
679        // transforms.rs::apply_soft_breaks).  Skip exactly one
680        // character at `rel` to mirror that — UTF-8 safe.
681        let consumed = line_text[rel..]
682            .chars()
683            .next()
684            .map(|c| c.len_utf8())
685            .unwrap_or(0);
686        prev_end = (rel + consumed).min(line_text.len());
687        prev_indent = indent;
688    }
689    let segment = &line_text[prev_end..];
690    total = total.saturating_add(count_segment_rows_with_indent(
691        segment,
692        prev_indent,
693        effective_width,
694        gutter_width,
695        hanging_indent,
696    ));
697    total.max(1)
698}
699
700/// Helper for [`count_visual_rows_for_text_with_soft_breaks`]:
701/// row count for one inter-break segment with `leading_indent`
702/// columns reserved at the front.  An empty segment still occupies
703/// one visual row (matches the renderer, which emits a trailing
704/// `Break` for the broken position).
705fn count_segment_rows_with_indent(
706    segment: &str,
707    leading_indent: u16,
708    effective_width: usize,
709    gutter_width: usize,
710    hanging_indent: bool,
711) -> u32 {
712    if segment.is_empty() && leading_indent == 0 {
713        return 1;
714    }
715    if leading_indent == 0 {
716        return count_visual_rows_for_text(segment, effective_width, gutter_width, hanging_indent);
717    }
718    // Prepend the indent columns; this lets the renderer's word-wrap
719    // see the same `current_line_width` it would after
720    // `apply_soft_breaks` injected indent Spaces.
721    let mut prefixed = String::with_capacity(leading_indent as usize + segment.len());
722    for _ in 0..leading_indent {
723        prefixed.push(' ');
724    }
725    prefixed.push_str(segment);
726    count_visual_rows_for_text(&prefixed, effective_width, gutter_width, hanging_indent)
727}
728
729/// Count visual rows for a single line's text under the renderer's
730/// wrap algorithm.  Pure function of (text, geometry).
731///
732/// Behaves exactly like the renderer's per-logical-line wrap count:
733/// runs `apply_wrapping_transform` on a single-`Text`-token input and
734/// tallies non-empty rows.  A trailing `Break` emitted when the last
735/// chunk exactly fills the effective width is followed by nothing
736/// meaningful and does not count as a row.
737pub fn count_visual_rows_for_text(
738    line_text: &str,
739    effective_width: usize,
740    gutter_width: usize,
741    hanging_indent: bool,
742) -> u32 {
743    use crate::view::ui::split_rendering::transforms::apply_wrapping_transform;
744    use fresh_core::api::ViewTokenWire;
745
746    let tokens = vec![ViewTokenWire {
747        source_offset: Some(0),
748        kind: ViewTokenWireKind::Text(line_text.to_string()),
749        style: None,
750    }];
751    let wrapped = apply_wrapping_transform(tokens, effective_width, gutter_width, hanging_indent);
752    let mut rows: u32 = 0;
753    let mut row_has_content = false;
754    for t in &wrapped {
755        match &t.kind {
756            ViewTokenWireKind::Newline => break,
757            ViewTokenWireKind::Break => {
758                if row_has_content {
759                    rows += 1;
760                }
761                row_has_content = false;
762            }
763            ViewTokenWireKind::Text(s) => {
764                if !s.is_empty() {
765                    row_has_content = true;
766                }
767            }
768            ViewTokenWireKind::Space | ViewTokenWireKind::BinaryByte(_) => {
769                row_has_content = true;
770            }
771        }
772    }
773    if row_has_content {
774        rows += 1;
775    }
776    rows.max(1)
777}
778
779#[cfg(test)]
780mod tests {
781    use super::*;
782    use crate::view::ui::view_pipeline::LineStart;
783
784    fn key(line_start: usize, version: u64) -> LineWrapKey {
785        LineWrapKey {
786            pipeline_inputs_version: version,
787            view_mode: CacheViewMode::Source,
788            line_start,
789            effective_width: 80,
790            gutter_width: 6,
791            wrap_column: None,
792            hanging_indent: false,
793            line_wrap_enabled: true,
794        }
795    }
796
797    /// Build a dummy `Vec<ViewLine>` of length `n` for primitive tests
798    /// that only care about how the cache stores / evicts values, not
799    /// about the actual pipeline output.  Each `ViewLine` is empty
800    /// apart from its row identity.
801    fn dummy_lines(n: u32) -> Vec<ViewLine> {
802        (0..n)
803            .map(|_| ViewLine {
804                text: String::new(),
805                source_start_byte: Some(0),
806                char_source_bytes: Vec::new(),
807                char_styles: Vec::new(),
808                char_visual_cols: Vec::new(),
809                visual_to_char: Vec::new(),
810                tab_starts: std::collections::HashSet::new(),
811                line_start: LineStart::Beginning,
812                ends_with_newline: false,
813                virtual_gutter_glyph: None,
814                virtual_line_style: None,
815            })
816            .collect()
817    }
818
819    /// Roomy byte budget for tests that shouldn't evict.
820    const ROOMY: usize = 1024 * 1024;
821    /// Tight byte budget that evicts after a handful of empty lines.
822    /// Each empty `ViewLine` is ~96 bytes plus 48 Vec/Arc overhead, so
823    /// this budget holds roughly 3 entries.
824    const TIGHT: usize = 500;
825
826    #[test]
827    fn empty_cache_is_empty() {
828        let cache = LineWrapCache::default();
829        assert!(cache.is_empty());
830        assert_eq!(cache.len(), 0);
831        assert_eq!(cache.current_bytes(), 0);
832    }
833
834    #[test]
835    fn get_or_insert_caches_on_miss() {
836        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
837        let mut compute_calls = 0;
838        let v = cache.get_or_insert_with(key(100, 1), || {
839            compute_calls += 1;
840            dummy_lines(7)
841        });
842        assert_eq!(v.len(), 7);
843        assert_eq!(compute_calls, 1);
844        assert_eq!(cache.len(), 1);
845    }
846
847    #[test]
848    fn repeat_lookup_is_a_hit() {
849        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
850        let mut compute_calls = 0;
851        cache.get_or_insert_with(key(100, 1), || {
852            compute_calls += 1;
853            dummy_lines(7)
854        });
855        let v = cache.get_or_insert_with(key(100, 1), || {
856            compute_calls += 1;
857            dummy_lines(99) // wrong value, should not be invoked
858        });
859        assert_eq!(v.len(), 7);
860        assert_eq!(compute_calls, 1, "second lookup should be a hit");
861    }
862
863    #[test]
864    fn different_versions_are_separate_entries() {
865        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
866        cache.get_or_insert_with(key(100, 1), || dummy_lines(3));
867        cache.get_or_insert_with(key(100, 2), || dummy_lines(5));
868        assert_eq!(cache.get(&key(100, 1)).map(|v| v.len()), Some(3));
869        assert_eq!(cache.get(&key(100, 2)).map(|v| v.len()), Some(5));
870        assert_eq!(cache.len(), 2);
871    }
872
873    #[test]
874    fn evicts_oldest_when_byte_budget_reached() {
875        let mut cache = LineWrapCache::with_byte_budget(TIGHT);
876        cache.get_or_insert_with(key(100, 1), || dummy_lines(1));
877        cache.get_or_insert_with(key(200, 1), || dummy_lines(1));
878        cache.get_or_insert_with(key(300, 1), || dummy_lines(1));
879        // Adding a fourth tiny entry should evict at least the oldest
880        // (line_start=100) to stay within the budget.
881        cache.get_or_insert_with(key(400, 1), || dummy_lines(1));
882        assert!(cache.current_bytes() <= TIGHT);
883        assert_eq!(cache.get(&key(100, 1)).is_none(), true, "oldest evicted");
884        // Later entries still reachable.
885        assert!(cache.get(&key(400, 1)).is_some());
886    }
887
888    #[test]
889    fn structural_invariant_holds_under_many_inserts() {
890        let mut cache = LineWrapCache::with_byte_budget(TIGHT);
891        for i in 0..200u64 {
892            cache.get_or_insert_with(key(i as usize, i), || dummy_lines(1));
893            assert_eq!(cache.len(), cache.map.len());
894            assert_eq!(cache.len(), cache.order.len());
895            assert_eq!(cache.current_bytes <= cache.byte_budget, true);
896        }
897    }
898
899    #[test]
900    fn put_overwrites_existing_value_without_reordering() {
901        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
902        cache.get_or_insert_with(key(100, 1), || dummy_lines(1));
903        cache.get_or_insert_with(key(200, 1), || dummy_lines(1));
904        cache.get_or_insert_with(key(300, 1), || dummy_lines(1));
905        // Overwrite middle with a different-sized value.
906        cache.put(key(200, 1), Arc::new(dummy_lines(42)));
907        assert_eq!(cache.get(&key(200, 1)).map(|v| v.len()), Some(42));
908        // key=100 is still the oldest in the FIFO.
909        cache.get_or_insert_with(key(400, 1), || dummy_lines(1));
910        // With ROOMY budget nothing's evicted yet; all present.
911        for k in [100usize, 200, 300, 400] {
912            assert!(cache.get(&key(k, 1)).is_some(), "k={k} should be present");
913        }
914    }
915
916    #[test]
917    fn clear_empties_cache() {
918        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
919        cache.get_or_insert_with(key(100, 1), || dummy_lines(1));
920        cache.get_or_insert_with(key(200, 1), || dummy_lines(1));
921        cache.clear();
922        assert!(cache.is_empty());
923        assert_eq!(cache.current_bytes(), 0);
924        assert!(cache.get(&key(100, 1)).is_none());
925    }
926
927    #[test]
928    fn pipeline_inputs_version_changes_when_any_source_changes() {
929        let a = pipeline_inputs_version(100, 5, 3, 7);
930        assert_ne!(
931            a,
932            pipeline_inputs_version(101, 5, 3, 7),
933            "buffer bump changes version"
934        );
935        assert_ne!(
936            a,
937            pipeline_inputs_version(100, 6, 3, 7),
938            "soft-break bump changes version"
939        );
940        assert_ne!(
941            a,
942            pipeline_inputs_version(100, 5, 4, 7),
943            "conceal bump changes version"
944        );
945        assert_ne!(
946            a,
947            pipeline_inputs_version(100, 5, 3, 8),
948            "virtual-text bump changes version"
949        );
950    }
951
952    #[test]
953    #[should_panic]
954    fn zero_byte_budget_rejected() {
955        LineWrapCache::with_byte_budget(0);
956    }
957
958    /// Even if a single new entry's estimated size exceeds the budget,
959    /// the cache accepts it rather than silently dropping data the
960    /// caller just paid to compute.  Later inserts will still evict it
961    /// like any other FIFO entry.
962    #[test]
963    fn oversize_entry_is_accepted_then_agable() {
964        let mut cache = LineWrapCache::with_byte_budget(TIGHT);
965        // dummy_lines(50) is ~7 KB per line × 50 = ~350 KB... no, empty
966        // ViewLines are ~96 bytes each, so 50 × 96 ≈ 5 KB.  That
967        // exceeds TIGHT (500 bytes).
968        cache.get_or_insert_with(key(1, 1), || dummy_lines(50));
969        assert!(cache.get(&key(1, 1)).is_some());
970        // Inserting a second entry evicts the oversize one.
971        cache.get_or_insert_with(key(2, 1), || dummy_lines(1));
972        assert!(cache.get(&key(1, 1)).is_none());
973        assert!(cache.get(&key(2, 1)).is_some());
974    }
975
976    // -------------------------------------------------------------------
977    // Layer 4: wrap-function invariants.
978    //
979    // These hold for any correct wrap regardless of cache state. A cache
980    // bug that corrupts a stored value would eventually violate one of
981    // them via the cache-backed path (e.g. width-monotonicity).
982    // -------------------------------------------------------------------
983
984    /// An empty line wraps to exactly one visual row.
985    #[test]
986    fn empty_line_is_one_row() {
987        for width in [5usize, 10, 42, 80, 120] {
988            assert_eq!(count_visual_rows_for_text("", width, 0, false), 1);
989            assert_eq!(count_visual_rows_for_text("", width, 6, false), 1);
990        }
991    }
992
993    /// A line whose visual width fits inside the available width wraps to
994    /// exactly one row.  Tests a few short ASCII strings at a few widths.
995    #[test]
996    fn line_that_fits_is_one_row() {
997        // "hello world" = 11 chars; at effective_width=80, gutter=6 →
998        // available width = 74 > 11, must be 1 row.
999        for text in ["hello", "hello world", "a b c d"] {
1000            assert_eq!(count_visual_rows_for_text(text, 80, 6, false), 1);
1001        }
1002    }
1003
1004    /// Width monotonicity: widening `effective_width` never *increases*
1005    /// the row count.
1006    ///
1007    /// For a fixed text, any correct wrap satisfies
1008    ///     w1 <= w2  →  rows(w1) >= rows(w2).
1009    #[test]
1010    fn width_monotonicity() {
1011        let texts = [
1012            "",
1013            "short",
1014            "a b c d e f g h i j k l m n o",
1015            "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
1016            "word00 word01 word02 word03 word04 word05 word06 word07",
1017        ];
1018        let gutter = 2usize;
1019        for text in &texts {
1020            let mut prev_rows: Option<u32> = None;
1021            // effective_width must be > gutter to leave any available
1022            // width; start well above.
1023            for w in [10usize, 15, 20, 30, 50, 80, 120, 200] {
1024                let rows = count_visual_rows_for_text(text, w, gutter, false);
1025                if let Some(prev) = prev_rows {
1026                    assert!(
1027                        rows <= prev,
1028                        "width monotonicity violated: rows({} chars, w={}) = {} > rows at prev w = {}. \
1029                         text={:?}",
1030                        text.len(),
1031                        w,
1032                        rows,
1033                        prev,
1034                        text,
1035                    );
1036                }
1037                prev_rows = Some(rows);
1038            }
1039        }
1040    }
1041
1042    /// No row count is ever zero — even pathologically narrow widths or
1043    /// unusual inputs return at least 1.
1044    #[test]
1045    fn row_count_is_always_at_least_one() {
1046        let cases = [
1047            ("", 80usize),
1048            ("x", 80),
1049            ("", 2), // near-minimum width
1050            ("abc", 3),
1051            (
1052                "a very long line with lots of words that will definitely wrap",
1053                20,
1054            ),
1055        ];
1056        for (text, w) in cases {
1057            assert!(
1058                count_visual_rows_for_text(text, w, 0, false) >= 1,
1059                "row count < 1 for text={:?}, width={}",
1060                text,
1061                w,
1062            );
1063        }
1064    }
1065
1066    /// Adding characters never *decreases* the row count at a fixed width.
1067    ///
1068    /// Subset-superset property: if `a` is a prefix of `b`, `rows(a) <=
1069    /// rows(b)`.  A cache that returned a stale value for a shortened
1070    /// line would fail this.
1071    #[test]
1072    fn prefix_never_has_more_rows() {
1073        let base = "aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee";
1074        let width = 20usize;
1075        let gutter = 2usize;
1076        let mut prev_rows: u32 = 0;
1077        for len in (0..=base.len()).step_by(5) {
1078            let prefix = &base[..len];
1079            let rows = count_visual_rows_for_text(prefix, width, gutter, false);
1080            assert!(
1081                rows >= prev_rows,
1082                "prefix property violated: len={}, rows={}, prev_rows={}",
1083                len,
1084                rows,
1085                prev_rows,
1086            );
1087            prev_rows = rows;
1088        }
1089    }
1090
1091    /// Deterministic: same input → same output, always.
1092    #[test]
1093    fn count_is_deterministic() {
1094        let text = "word00 word01 word02 word03 word04 word05 word06 word07 word08 word09";
1095        let w = 30usize;
1096        let g = 4usize;
1097        let r1 = count_visual_rows_for_text(text, w, g, false);
1098        for _ in 0..16 {
1099            let r = count_visual_rows_for_text(text, w, g, false);
1100            assert_eq!(r, r1, "non-deterministic row count");
1101        }
1102    }
1103
1104    // -------------------------------------------------------------------
1105    // Layer 3 (partial): shadow-model property test.
1106    //
1107    // A "shadow" cache always recomputes from the pure `count_visual_rows
1108    // _for_text` function; the "real" cache uses `LineWrapCache`. A
1109    // mutation-free op stream with random (text, width) probes must
1110    // always agree between real and shadow — otherwise the cache is
1111    // returning a value inconsistent with fresh computation. Covers the
1112    // insert / hit / evict surfaces on the cache primitive without
1113    // running the full editor pipeline.
1114    //
1115    // Full plugin-state shadow (buffer edits, soft-break injection,
1116    // conceals, view-mode toggles) lives in an e2e-level test — this
1117    // layer is the pure-primitive check.
1118    // -------------------------------------------------------------------
1119
1120    #[test]
1121    fn shadow_agreement_pure_primitive() {
1122        // Deterministic "random" inputs from simple counters, so this is
1123        // reproducible without a proptest dep.
1124        let texts: Vec<String> = (0..30)
1125            .map(|i| {
1126                let n = (i * 7 + 3) % 120 + 5;
1127                let seed = [b'a', b'b', b'c', b' ', b'd', b'e', b'f', b' ', b'1', b'2'];
1128                (0..n).map(|k| seed[k % seed.len()] as char).collect()
1129            })
1130            .collect();
1131        let widths: [usize; 5] = [12, 20, 42, 80, 120];
1132
1133        // Cache stores Vec<ViewLine>, so the shadow compares the LENGTH
1134        // (row count) the cache would expose with a fresh recompute.
1135        // The full-pipeline shadow (ViewLine coordinates agreeing with
1136        // the renderer) lives in e2e tests; this primitive-level shadow
1137        // checks that the FIFO / byte-budget machinery doesn't corrupt
1138        // stored values across inserts and evictions.
1139        //
1140        // Real cache values are built from `dummy_lines(shadow_count)`
1141        // so the cache value's length equals the shadow row count.
1142        let mut real = LineWrapCache::with_byte_budget(TIGHT);
1143        for step in 0..400usize {
1144            let t_idx = (step * 37 + 11) % texts.len();
1145            let w_idx = (step * 5 + 3) % widths.len();
1146            let text = &texts[t_idx];
1147            let width = widths[w_idx];
1148
1149            let shadow_rows = count_visual_rows_for_text(text, width, 2, false);
1150
1151            let key = LineWrapKey {
1152                pipeline_inputs_version: 0,
1153                view_mode: CacheViewMode::Source,
1154                line_start: t_idx, // stand-in for byte; distinct per text
1155                effective_width: width as u32,
1156                gutter_width: 2,
1157                wrap_column: None,
1158                hanging_indent: false,
1159                line_wrap_enabled: true,
1160            };
1161            let real_val = real.get_or_insert_with(key, || dummy_lines(shadow_rows));
1162            assert_eq!(
1163                real_val.len() as u32,
1164                shadow_rows,
1165                "shadow disagreement at step {step}: text_idx={t_idx}, width={width}, \
1166                 real={}, shadow={shadow_rows}",
1167                real_val.len(),
1168            );
1169            assert!(
1170                real.current_bytes() <= real.byte_budget(),
1171                "cache exceeded byte budget"
1172            );
1173        }
1174    }
1175
1176    /// Version-bump invalidation: entries stored under version V are
1177    /// NEVER returned when a lookup is built at version V+1.  The
1178    /// old entry sits in memory until FIFO evicts it, but no caller
1179    /// should ever get the stale value.
1180    #[test]
1181    fn version_bump_makes_old_entry_unreachable() {
1182        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
1183        let key_v0 = LineWrapKey {
1184            pipeline_inputs_version: 100,
1185            view_mode: CacheViewMode::Source,
1186            line_start: 42,
1187            effective_width: 80,
1188            gutter_width: 6,
1189            wrap_column: None,
1190            hanging_indent: false,
1191            line_wrap_enabled: true,
1192        };
1193        cache.get_or_insert_with(key_v0, || dummy_lines(5));
1194        assert_eq!(cache.get(&key_v0).map(|v| v.len()), Some(5));
1195
1196        let key_v1 = LineWrapKey {
1197            pipeline_inputs_version: 101,
1198            ..key_v0
1199        };
1200        assert!(
1201            cache.get(&key_v1).is_none(),
1202            "v1 lookup must miss even though v0 entry is still present"
1203        );
1204
1205        // Miss path stores under v1; v0 remains in the map, untouched.
1206        let mut miss_called = 0;
1207        let v = cache.get_or_insert_with(key_v1, || {
1208            miss_called += 1;
1209            dummy_lines(7)
1210        });
1211        assert_eq!(v.len(), 7);
1212        assert_eq!(miss_called, 1);
1213        assert_eq!(cache.get(&key_v1).map(|v| v.len()), Some(7));
1214        assert_eq!(
1215            cache.get(&key_v0).map(|v| v.len()),
1216            Some(5),
1217            "v0 entry preserved until evicted"
1218        );
1219    }
1220
1221    /// All geometry dimensions in the key are distinct — changing any one
1222    /// produces a miss.
1223    #[test]
1224    fn every_key_dimension_separates_entries() {
1225        let base = LineWrapKey {
1226            pipeline_inputs_version: 1,
1227            view_mode: CacheViewMode::Source,
1228            line_start: 10,
1229            effective_width: 80,
1230            gutter_width: 6,
1231            wrap_column: None,
1232            hanging_indent: false,
1233            line_wrap_enabled: true,
1234        };
1235
1236        // Vary each field in turn; each variation must be a distinct key.
1237        let variations: [LineWrapKey; 8] = [
1238            LineWrapKey {
1239                pipeline_inputs_version: 2,
1240                ..base
1241            },
1242            LineWrapKey {
1243                view_mode: CacheViewMode::Compose,
1244                ..base
1245            },
1246            LineWrapKey {
1247                line_start: 11,
1248                ..base
1249            },
1250            LineWrapKey {
1251                effective_width: 81,
1252                ..base
1253            },
1254            LineWrapKey {
1255                gutter_width: 7,
1256                ..base
1257            },
1258            LineWrapKey {
1259                wrap_column: Some(70),
1260                ..base
1261            },
1262            LineWrapKey {
1263                hanging_indent: true,
1264                ..base
1265            },
1266            LineWrapKey {
1267                line_wrap_enabled: false,
1268                ..base
1269            },
1270        ];
1271
1272        let mut cache = LineWrapCache::with_byte_budget(ROOMY);
1273        cache.get_or_insert_with(base, || dummy_lines(1));
1274        for (i, v) in variations.iter().enumerate() {
1275            assert_ne!(*v, base, "variation {i} shouldn't equal base");
1276            assert!(
1277                cache.get(v).is_none(),
1278                "variation {i} unexpectedly hit base entry"
1279            );
1280            cache.get_or_insert_with(*v, || dummy_lines(2 + i as u32));
1281        }
1282        // Base entry is still reachable.
1283        assert_eq!(cache.get(&base).map(|v| v.len()), Some(1));
1284        // Each variation stored its own value (distinguished by length).
1285        for (i, v) in variations.iter().enumerate() {
1286            assert_eq!(cache.get(v).map(|v| v.len()), Some(2 + i));
1287        }
1288    }
1289}