rizzler 0.1.0

the rizz editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Registry of customization slots — status segments, gutters, line decorators,
//! and bottom-strip components. Each slot is identified by an ordered position
//! plus a name (for add/remove/replace operations). The renderer's precompute
//! pass (in `state.rs`, see commit 3) walks this registry once per frame.
//!
//! Three kinds of slot payload:
//!
//! * [`LispRenderable::Static`] — a literal lisp value, consumed verbatim.
//! * [`LispRenderable::Callable`] — a closure or native fn called each frame
//!   with kind-specific arguments.
//! * [`LispRenderable::Builtin`] — a built-in Rust producer identified by
//!   [`BuiltinId`]. This is how the bundled `default-style.lisp` reuses the
//!   existing Rust gutters/segments/decorators without going through the
//!   lisp callback path.

use std::rc::Rc;

use ratatui::text::{Line, Span};
use rizz::RizzError;
use rizz::runtime::{self, Env, Value};

use crate::buffer::Buffer;
use crate::mode::EditingMode;
use crate::render::{DecoratorRanges, RenderedGutter, StateSnapshot, StyledRange};
use crate::styling::{
    Color, NamedColor, Style, Theme, rgb_value, spans_from_value, style_from_value,
};

// ---------------------------------------------------------------------------
// Renderable payload
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
pub enum LispRenderable {
    Static(Rc<Value>),
    Callable(Rc<Value>),
    Builtin(BuiltinId),
}

/// Identifiers for the Rust-side producers that lisp can reference by ident.
///
/// These let `default-style.lisp` write `(decorator-add 'current-line-highlight)`
/// and reuse the existing optimized Rust impl, rather than recreating the
/// behavior in lisp.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BuiltinId {
    // gutters
    LineNumbers,
    // line decorators
    BaseFg,
    SelectionHighlight,
    CurrentLineHighlight,
    // status segments
    ModeGlyph,
    LastKey,
    BufferNo,
}

impl BuiltinId {
    pub fn parse(s: &str) -> Option<Self> {
        Some(match s {
            "line-numbers" => Self::LineNumbers,
            "base-fg" => Self::BaseFg,
            "selection-highlight" => Self::SelectionHighlight,
            "current-line-highlight" => Self::CurrentLineHighlight,
            "mode-glyph" => Self::ModeGlyph,
            "last-key" => Self::LastKey,
            "buffer-no" => Self::BufferNo,
            _ => return None,
        })
    }

    /// The slot category this builtin belongs to. Used to reject mismatches
    /// like `(status-segment-add 'line-numbers ...)`.
    pub fn category(self) -> SlotCategory {
        match self {
            Self::LineNumbers => SlotCategory::Gutter,
            Self::BaseFg | Self::SelectionHighlight | Self::CurrentLineHighlight => {
                SlotCategory::Decorator
            }
            Self::ModeGlyph | Self::LastKey | Self::BufferNo => SlotCategory::StatusSegment,
        }
    }
}

// ---------------------------------------------------------------------------
// Slot kinds
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SlotCategory {
    StatusSegment,
    Gutter,
    Decorator,
    Bottom,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SegmentSide {
    Left,
    Right,
}

/// Per-kind layout metadata threaded along with each slot. The category enum
/// above is the type-only discriminant; this carries the extra data needed
/// to lay out the slot (which side, how wide, how many rows).
#[derive(Clone, Debug)]
pub enum SlotKind {
    StatusSegment { side: SegmentSide },
    Gutter { width: u16 },
    Decorator,
    Bottom { rows: u16 },
}

impl SlotKind {
    pub fn category(&self) -> SlotCategory {
        match self {
            Self::StatusSegment { .. } => SlotCategory::StatusSegment,
            Self::Gutter { .. } => SlotCategory::Gutter,
            Self::Decorator => SlotCategory::Decorator,
            Self::Bottom { .. } => SlotCategory::Bottom,
        }
    }
}

// ---------------------------------------------------------------------------
// Slot + registry
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
pub struct Slot {
    pub name: Rc<str>,
    pub kind: SlotKind,
    pub renderable: LispRenderable,
}

/// Ordered lists of slots per category. Mutated via `add`/`remove`/`replace`/
/// `clear`; iteration order within each list is the render order.
#[derive(Clone, Debug, Default)]
pub struct SlotRegistry {
    status_left: Vec<Slot>,
    status_right: Vec<Slot>,
    gutters: Vec<Slot>,
    decorators: Vec<Slot>,
    bottom: Vec<Slot>,
}

impl SlotRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn status_segments(&self, side: SegmentSide) -> &[Slot] {
        match side {
            SegmentSide::Left => &self.status_left,
            SegmentSide::Right => &self.status_right,
        }
    }

    pub fn gutters(&self) -> &[Slot] {
        &self.gutters
    }

    pub fn decorators(&self) -> &[Slot] {
        &self.decorators
    }

    pub fn bottom(&self) -> &[Slot] {
        &self.bottom
    }

    /// Append a slot to the appropriate list. If a slot with the same name
    /// already exists in that list, it is replaced in place (preserving its
    /// position) so re-registering is idempotent.
    pub fn add(&mut self, slot: Slot) {
        let list = self.list_mut(&slot.kind);
        match list.iter().position(|s| s.name == slot.name) {
            Some(i) => list[i] = slot,
            None => list.push(slot),
        }
    }

    /// Remove the slot named `name` from `category`. Returns `true` if a
    /// matching slot was found.
    pub fn remove(&mut self, category: SlotCategory, name: &str) -> bool {
        let list = match category {
            SlotCategory::StatusSegment => {
                // Try left first, then right.
                if remove_named(&mut self.status_left, name) {
                    return true;
                }
                &mut self.status_right
            }
            SlotCategory::Gutter => &mut self.gutters,
            SlotCategory::Decorator => &mut self.decorators,
            SlotCategory::Bottom => &mut self.bottom,
        };
        remove_named(list, name)
    }

    /// Replace an entire ordered list (status segments per side, gutters,
    /// decorators, bottom rows) with `new_slots` in source order.
    pub fn replace(
        &mut self,
        category: SlotCategory,
        side: Option<SegmentSide>,
        new_slots: Vec<Slot>,
    ) {
        let list = match (category, side) {
            (SlotCategory::StatusSegment, Some(SegmentSide::Left)) => &mut self.status_left,
            (SlotCategory::StatusSegment, Some(SegmentSide::Right)) => &mut self.status_right,
            (SlotCategory::Gutter, _) => &mut self.gutters,
            (SlotCategory::Decorator, _) => &mut self.decorators,
            (SlotCategory::Bottom, _) => &mut self.bottom,
            // Replacing status segments without specifying a side: clear both.
            (SlotCategory::StatusSegment, None) => {
                self.status_left.clear();
                self.status_right.clear();
                return;
            }
        };
        *list = new_slots;
    }

    pub fn clear(&mut self, category: SlotCategory) {
        match category {
            SlotCategory::StatusSegment => {
                self.status_left.clear();
                self.status_right.clear();
            }
            SlotCategory::Gutter => self.gutters.clear(),
            SlotCategory::Decorator => self.decorators.clear(),
            SlotCategory::Bottom => self.bottom.clear(),
        }
    }

    fn list_mut(&mut self, kind: &SlotKind) -> &mut Vec<Slot> {
        match kind {
            SlotKind::StatusSegment {
                side: SegmentSide::Left,
            } => &mut self.status_left,
            SlotKind::StatusSegment {
                side: SegmentSide::Right,
            } => &mut self.status_right,
            SlotKind::Gutter { .. } => &mut self.gutters,
            SlotKind::Decorator => &mut self.decorators,
            SlotKind::Bottom { .. } => &mut self.bottom,
        }
    }
}

fn remove_named(list: &mut Vec<Slot>, name: &str) -> bool {
    if let Some(i) = list.iter().position(|s| s.name.as_ref() == name) {
        list.remove(i);
        true
    } else {
        false
    }
}

// ---------------------------------------------------------------------------
// Producers
// ---------------------------------------------------------------------------
//
// One function per slot category. Each one dispatches on the slot's
// `renderable` (Static, Callable, Builtin) and returns the concrete data the
// renderer needs. Lisp errors are reported via `Result`; the precompute pass
// in `state.rs` is responsible for surfacing them.

/// Status segment producer. Returns a sequence of ratatui spans.
pub fn produce_status_segment(
    slot: &Slot,
    snap: &StateSnapshot<'_>,
    theme: &Theme,
    env: &Env,
) -> Result<Vec<Span<'static>>, RizzError> {
    match &slot.renderable {
        LispRenderable::Static(v) => Ok(spans_from_value(v, theme)?),
        LispRenderable::Callable(f) => {
            let v = runtime::apply(f, &[], env)?;
            Ok(spans_from_value(&v, theme)?)
        }
        LispRenderable::Builtin(b) => Ok(builtin_status_segment(*b, snap, theme)),
    }
}

/// Gutter producer. Returns one column's worth of pre-rendered rows for
/// `buf`, padded to a stable width.
pub fn produce_gutter(
    slot: &Slot,
    buf: &Buffer,
    theme: &Theme,
    env: &Env,
) -> Result<RenderedGutter, RizzError> {
    let SlotKind::Gutter {
        width: registered_width,
    } = slot.kind
    else {
        return Ok(RenderedGutter {
            width: 0,
            rows: vec![],
        });
    };

    match &slot.renderable {
        LispRenderable::Builtin(b) => Ok(builtin_gutter(*b, buf, theme)),
        LispRenderable::Static(_) | LispRenderable::Callable(_) => {
            let width = registered_width;
            let start = buf.file_pos().row.min(buf.len_lines());
            let visible = buf.viewport.row as usize;
            let last = buf.len_lines().saturating_sub(1);

            let mut rows = Vec::with_capacity(visible);
            for r in 0..visible {
                let lnum = start + r;
                let lnum_arg = if lnum <= last {
                    Rc::new(Value::Int(lnum as i64))
                } else {
                    Rc::new(Value::Unit)
                };
                let v = match &slot.renderable {
                    LispRenderable::Static(v) => v.clone(),
                    LispRenderable::Callable(f) => runtime::apply(f, &[lnum_arg], env)?,
                    LispRenderable::Builtin(_) => unreachable!(),
                };
                let spans = spans_from_value(&v, theme)?;
                rows.push(pad_line_to_width(spans, width));
            }
            Ok(RenderedGutter { width, rows })
        }
    }
}

/// Decorator producer. Returns a flat list of styled ranges to apply across
/// the buffer.
pub fn produce_decorator(
    slot: &Slot,
    buf: &Buffer,
    theme: &Theme,
    env: &Env,
) -> Result<DecoratorRanges, RizzError> {
    match &slot.renderable {
        LispRenderable::Builtin(b) => Ok(builtin_decorator(*b, buf, theme)),
        LispRenderable::Static(v) => Ok(DecoratorRanges {
            ranges: ranges_from_value(v, theme)?,
        }),
        LispRenderable::Callable(f) => {
            let v = runtime::apply(f, &[], env)?;
            Ok(DecoratorRanges {
                ranges: ranges_from_value(&v, theme)?,
            })
        }
    }
}

/// Bottom-strip component producer. Returns one inner `Vec<Span>` per row.
pub fn produce_bottom(
    slot: &Slot,
    snap: &StateSnapshot<'_>,
    theme: &Theme,
    env: &Env,
) -> Result<Vec<Vec<Span<'static>>>, RizzError> {
    let _ = snap; // no builtin bottom components yet
    match &slot.renderable {
        LispRenderable::Builtin(_) => Ok(vec![]),
        LispRenderable::Static(v) => Ok(rows_from_value(v, theme)?),
        LispRenderable::Callable(f) => {
            let v = runtime::apply(f, &[], env)?;
            Ok(rows_from_value(&v, theme)?)
        }
    }
}

// --- builtin status segments ---

fn builtin_status_segment(
    b: BuiltinId,
    snap: &StateSnapshot<'_>,
    _theme: &Theme,
) -> Vec<Span<'static>> {
    let s = match b {
        BuiltinId::ModeGlyph => match snap.focused().mode() {
            EditingMode::Insert => "i",
            EditingMode::Normal => "n",
            EditingMode::Visual => "v",
            EditingMode::VisualLine => "V",
            EditingMode::VisualBlock => "^V",
            EditingMode::Command => "c",
        }
        .to_string(),
        BuiltinId::LastKey => snap
            .keyevent
            .as_ref()
            .map(|e| e.code.to_string())
            .unwrap_or_else(|| "None".to_string()),
        BuiltinId::BufferNo => snap.bufno.to_string(),
        _ => return vec![],
    };
    vec![Span::raw(s)]
}

// --- builtin gutter ---

fn builtin_gutter(b: BuiltinId, buf: &Buffer, _theme: &Theme) -> RenderedGutter {
    match b {
        BuiltinId::LineNumbers => {
            let max = buf.len_lines().max(1);
            let digits = ((max as f64).log10().floor() as u16) + 1;
            let width = digits.max(2) + 1;
            let w = (width - 1) as usize;

            let start = buf.file_pos().row.min(buf.len_lines());
            let visible = buf.viewport.row as usize;
            let last = buf.len_lines().saturating_sub(1);

            let mut rows = Vec::with_capacity(visible);
            for r in 0..visible {
                let lnum = start + r;
                let text = if lnum <= last {
                    format!("{:>w$} ", lnum, w = w)
                } else {
                    " ".repeat(w + 1)
                };
                rows.push(Line::from(Span::raw(text)));
            }
            RenderedGutter { width, rows }
        }
        _ => RenderedGutter {
            width: 0,
            rows: vec![],
        },
    }
}

// --- builtin decorators ---

fn builtin_decorator(b: BuiltinId, buf: &Buffer, _theme: &Theme) -> DecoratorRanges {
    let start = buf.file_pos().row.min(buf.len_lines());
    let visible = buf.viewport.row as usize;

    let mut ranges = Vec::new();
    match b {
        BuiltinId::BaseFg => {
            let style = Style {
                fg: Some(Color::Named(NamedColor::Blue)),
                ..Default::default()
            };
            for (i, line) in buf.lines_at(start).take(visible).enumerate() {
                let text = line.to_string();
                let len = text.trim_end_matches(['\n', '\r']).chars().count();
                ranges.push(StyledRange {
                    row: start + i,
                    col: 0,
                    len,
                    style: style.clone(),
                    pad_to_width: false,
                });
            }
        }
        BuiltinId::SelectionHighlight => {
            let style = Style {
                bg: Some(Color::Rgb(60, 90, 130)),
                ..Default::default()
            };
            let Some(anchor) = buf.selection_anchor() else {
                return DecoratorRanges { ranges };
            };
            let cur = buf.abs_pos();
            let mode = buf.mode();
            if !mode.is_visual() {
                return DecoratorRanges { ranges };
            }
            let (min_row, max_row) = order(anchor.row, cur.row);
            for (i, line) in buf.lines_at(start).take(visible).enumerate() {
                let lnum = start + i;
                if lnum < min_row || lnum > max_row {
                    continue;
                }
                let text = line.to_string();
                let line_len = text.trim_end_matches(['\n', '\r']).chars().count();
                let (col, len, pad) = match mode {
                    EditingMode::VisualLine => (0usize, line_len.max(1), true),
                    EditingMode::VisualBlock => {
                        let (lo, hi) = order(anchor.col, cur.col);
                        (lo, hi.saturating_sub(lo) + 1, false)
                    }
                    EditingMode::Visual => {
                        let (lo_row, lo_col, hi_row, hi_col) =
                            if (anchor.row, anchor.col) <= (cur.row, cur.col) {
                                (anchor.row, anchor.col, cur.row, cur.col)
                            } else {
                                (cur.row, cur.col, anchor.row, anchor.col)
                            };
                        let s = if lnum == lo_row { lo_col } else { 0 };
                        let e = if lnum == hi_row {
                            hi_col + 1
                        } else {
                            line_len.max(1)
                        };
                        (s, e.saturating_sub(s), false)
                    }
                    _ => continue,
                };
                if len == 0 {
                    continue;
                }
                ranges.push(StyledRange {
                    row: lnum,
                    col,
                    len,
                    style: style.clone(),
                    pad_to_width: pad,
                });
            }
        }
        BuiltinId::CurrentLineHighlight => {
            let style = Style {
                bg: Some(Color::Named(NamedColor::DarkGray)),
                ..Default::default()
            };
            let cur_row = buf.file_pos().row + buf.cursor_pos().row as usize;
            ranges.push(StyledRange {
                row: cur_row,
                col: 0,
                len: 0,
                style,
                pad_to_width: true,
            });
        }
        _ => {}
    }
    DecoratorRanges { ranges }
}

fn order(a: usize, b: usize) -> (usize, usize) {
    if a <= b { (a, b) } else { (b, a) }
}

// --- value -> ranges ---
//
// Decorator slots return a sequence of `{row col len style pad}` maps.
// Each map's keys are strings (per the project-wide convention).

fn ranges_from_value(v: &Rc<Value>, theme: &Theme) -> Result<Vec<StyledRange>, RizzError> {
    let mut out = Vec::new();
    for entry in entries(v) {
        let Value::Map(m) = &*entry else {
            return Err(RizzError::from(rizz::runtime::RuntimeError::type_mismatch(
                "decorator",
                "map with row|col|len|style fields",
                &entry,
            )));
        };
        let key = |k: &str| Rc::new(Value::Str(k.into()));
        let int_field = |name: &str| -> Result<usize, RizzError> {
            let v = m.get(&key(name)).cloned().unwrap_or(Rc::new(Value::Int(0)));
            let n = v.as_int().ok_or_else(|| {
                rizz::runtime::RuntimeError::type_mismatch("decorator", "int", &v)
            })?;
            Ok(n.max(0) as usize)
        };
        let row = int_field("row")?;
        let col = int_field("col")?;
        let len = int_field("len")?;
        let style = match m.get(&key("style")) {
            Some(s) => style_from_value(s, theme)?,
            None => Style::default(),
        };
        let pad_to_width = m
            .get(&key("pad-to-width"))
            .map(|v| v.is_truthy())
            .unwrap_or(false);
        out.push(StyledRange {
            row,
            col,
            len,
            style,
            pad_to_width,
        });
    }
    Ok(out)
}

// --- value -> bottom rows ---
//
// A bottom component returns an array (or list) of "lines"; each line is in
// turn anything `spans_from_value` accepts. So `[(span "x") "y" {"text":"z"}]`
// is a three-row component.

fn rows_from_value(v: &Rc<Value>, theme: &Theme) -> Result<Vec<Vec<Span<'static>>>, RizzError> {
    let mut out = Vec::new();
    for row in entries(v) {
        out.push(spans_from_value(&row, theme)?);
    }
    Ok(out)
}

/// Iterate entries of either an `Array` or a `Cons` list. Rizz's
/// [`Value::iter`] only walks cons chains; arrays would otherwise be
/// returned as a single opaque entry.
fn entries(v: &Rc<Value>) -> Box<dyn Iterator<Item = Rc<Value>> + '_> {
    match &**v {
        Value::Array(xs) => Box::new(xs.iter().cloned().collect::<Vec<_>>().into_iter()),
        _ => Box::new(Value::iter(v)),
    }
}

// --- helpers ---

fn pad_line_to_width(spans: Vec<Span<'static>>, width: u16) -> Line<'static> {
    use unicode_width::UnicodeWidthStr;
    let used: usize = spans
        .iter()
        .map(|s| UnicodeWidthStr::width(s.content.as_ref()))
        .sum();
    let mut spans = spans;
    if (width as usize) > used {
        spans.push(Span::raw(" ".repeat(width as usize - used)));
    }
    Line::from(spans)
}

#[allow(dead_code)] // exposed for default-style.lisp callers in commit 5
pub fn default_face_rgb_bg() -> Rc<Value> {
    rgb_value(60, 90, 130)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn slot(name: &str, kind: SlotKind, b: BuiltinId) -> Slot {
        Slot {
            name: name.into(),
            kind,
            renderable: LispRenderable::Builtin(b),
        }
    }

    #[test]
    fn builtin_parse_round_trips() {
        assert_eq!(
            BuiltinId::parse("line-numbers"),
            Some(BuiltinId::LineNumbers)
        );
        assert_eq!(
            BuiltinId::parse("current-line-highlight"),
            Some(BuiltinId::CurrentLineHighlight)
        );
        assert_eq!(BuiltinId::parse("no-such-thing"), None);
    }

    #[test]
    fn builtin_category_matches_kind() {
        assert_eq!(BuiltinId::LineNumbers.category(), SlotCategory::Gutter);
        assert_eq!(BuiltinId::BaseFg.category(), SlotCategory::Decorator);
        assert_eq!(BuiltinId::ModeGlyph.category(), SlotCategory::StatusSegment);
    }

    #[test]
    fn add_then_remove_slot() {
        let mut r = SlotRegistry::new();
        r.add(slot(
            "lnum",
            SlotKind::Gutter { width: 4 },
            BuiltinId::LineNumbers,
        ));
        assert_eq!(r.gutters().len(), 1);
        assert!(r.remove(SlotCategory::Gutter, "lnum"));
        assert_eq!(r.gutters().len(), 0);
        assert!(!r.remove(SlotCategory::Gutter, "lnum"));
    }

    #[test]
    fn add_with_existing_name_replaces_in_place() {
        let mut r = SlotRegistry::new();
        r.add(slot("a", SlotKind::Decorator, BuiltinId::BaseFg));
        r.add(slot(
            "b",
            SlotKind::Decorator,
            BuiltinId::CurrentLineHighlight,
        ));
        // Re-add "a" with a different builtin; should replace in place.
        r.add(slot(
            "a",
            SlotKind::Decorator,
            BuiltinId::SelectionHighlight,
        ));
        let ds = r.decorators();
        assert_eq!(ds.len(), 2);
        assert_eq!(ds[0].name.as_ref(), "a");
        assert!(matches!(
            ds[0].renderable,
            LispRenderable::Builtin(BuiltinId::SelectionHighlight)
        ));
    }

    #[test]
    fn replace_swaps_entire_list() {
        let mut r = SlotRegistry::new();
        r.add(slot("a", SlotKind::Decorator, BuiltinId::BaseFg));
        r.add(slot(
            "b",
            SlotKind::Decorator,
            BuiltinId::CurrentLineHighlight,
        ));
        r.replace(
            SlotCategory::Decorator,
            None,
            vec![slot(
                "c",
                SlotKind::Decorator,
                BuiltinId::SelectionHighlight,
            )],
        );
        let ds = r.decorators();
        assert_eq!(ds.len(), 1);
        assert_eq!(ds[0].name.as_ref(), "c");
    }

    #[test]
    fn status_segments_are_per_side() {
        let mut r = SlotRegistry::new();
        r.add(slot(
            "mode",
            SlotKind::StatusSegment {
                side: SegmentSide::Left,
            },
            BuiltinId::ModeGlyph,
        ));
        r.add(slot(
            "key",
            SlotKind::StatusSegment {
                side: SegmentSide::Right,
            },
            BuiltinId::LastKey,
        ));
        assert_eq!(r.status_segments(SegmentSide::Left).len(), 1);
        assert_eq!(r.status_segments(SegmentSide::Right).len(), 1);
        assert!(r.remove(SlotCategory::StatusSegment, "mode"));
        assert_eq!(r.status_segments(SegmentSide::Left).len(), 0);
        assert_eq!(r.status_segments(SegmentSide::Right).len(), 1);
    }
}