croft-software 0.1.758

VSCode-style TUI written in Rust
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! Rendered Markdown preview (Cmd/Ctrl+Shift+V): a pure converter from
//! CommonMark text to styled ratatui lines, consumed by the editor's preview
//! view. pulldown-cmark (rustdoc / mdBook's engine) drives the event stream;
//! fenced code blocks route through the same tree-sitter highlighter the
//! editor uses, so a ```rust block in the preview colours exactly like the
//! file would. Paragraph text is emitted unwrapped — the editor renders the
//! lines through a wrapping `Paragraph`, so the preview reflows with the pane.

use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};

use crate::highlight::{
    HiSpan, LangKind, LangRegistry, compute_line_starts, highlight_text, lang_for_extension,
};
use crate::theme::Theme;

const FG: Color = Color::Rgb(0xC0, 0xC5, 0xCE);
const DIM: Color = Color::Rgb(0x60, 0x68, 0x78);
const CODE: Color = Color::Rgb(0xD0, 0x87, 0x70);
const RULE_COLS: usize = 60;

/// The editor-held preview state: the built lines, the vertical scroll, and
/// the edit seq the lines were built from (a stale seq rebuilds lazily on the
/// next render, so the preview follows external reloads and live edits).
pub struct MarkdownPreview {
    pub lines: Vec<Line<'static>>,
    pub scroll: u16,
    pub built_seq: u64,
    /// Local images resolved at build (#176): each owns a run of blank
    /// reserved lines in `lines` that the app's overlay paints into.
    pub images: Vec<MdImage>,
    /// Frame truth, written by the editor's render: each image's anchor
    /// as a VISUAL row in the wrapped paragraph (same order as
    /// `images`), the (built_seq, wrap width) the mapping was computed
    /// for, and the text area the paragraph painted into.
    pub anchor_rows: Vec<usize>,
    pub wrap_key: (u64, u16),
    pub last_area: ratatui::layout::Rect,
    /// True when this preview renders a Jupyter notebook (#180): the
    /// stale-rebuild and theme-switch paths dispatch to the notebook
    /// builder instead of the markdown one.
    pub notebook: bool,
    /// Frame truth, written by the editor's render: one entry per painted
    /// visual row of `last_area`, each holding that row's cell symbols by
    /// SCREEN COLUMN. Per-column (not a joined string) keeps wide glyphs
    /// aligned: a double-width character owns its column and leaves an
    /// empty continuation cell, so a column index always addresses the
    /// right character. The rendered view is a wrapped `Paragraph`, so
    /// these cells are the only faithful record of what the user sees.
    pub rows: Vec<Vec<String>>,
    /// The user's selection over the rendered view, as (row, col) pairs
    /// relative to `last_area`: `anchor` is where the drag started,
    /// `head` where it is now. `None` when nothing is selected.
    pub selection: Option<((u16, u16), (u16, u16))>,
    /// True while a mouse drag is extending `selection`.
    pub dragging: bool,
    /// Set when this preview renders a docx/odt document (#181): the
    /// rebuild paths re-walk THIS file instead of the text buffer.
    pub doc_path: Option<std::path::PathBuf>,
    /// True when `doc_path` names a MEDIA file (#183): the rebuild
    /// dispatch probes headers instead of walking document XML.
    pub media: bool,
}

/// One local image block in a rendered preview (#176).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MdImage {
    /// Index of the first reserved (blank) line in the built lines.
    pub first_line: usize,
    /// Reserved line count, from the image's aspect at build time.
    pub rows: u16,
    pub path: std::path::PathBuf,
}

/// Map a fenced code block's info string to a highlighter language. Accepts
/// the common fence names and falls back to the file-extension table.
fn lang_for_fence(info: &str) -> Option<LangKind> {
    let tag = info.split_whitespace().next().unwrap_or("");
    Some(match tag.to_ascii_lowercase().as_str() {
        "rust" => LangKind::Rust,
        "python" => LangKind::Python,
        "javascript" => LangKind::JavaScript,
        "typescript" => LangKind::TypeScript,
        "golang" => LangKind::Go,
        "shell" | "console" | "terminal" => LangKind::Bash,
        other => return lang_for_extension(other),
    })
}

/// Convert one highlighted source line into owned spans, filling unstyled
/// gaps with `code_fg` — the theme's code foreground, never the Base16
/// literal, so gaps match the captured tokens around them on every theme.
fn code_line_spans(line: &str, spans: &[HiSpan], code_fg: Color) -> Vec<Span<'static>> {
    let mut out = Vec::new();
    let mut cursor = 0usize;
    for sp in spans {
        let start = sp.start.min(line.len());
        let end = sp.end.min(line.len());
        if start > cursor {
            out.push(Span::styled(
                line[cursor..start].to_string(),
                Style::default().fg(code_fg),
            ));
        }
        if end > start {
            out.push(Span::styled(line[start..end].to_string(), sp.style));
        }
        cursor = cursor.max(end);
    }
    if cursor < line.len() {
        out.push(Span::styled(
            line[cursor..].to_string(),
            Style::default().fg(code_fg),
        ));
    }
    out
}

struct Renderer<'r> {
    theme: Theme,
    registry: &'r mut LangRegistry,
    out: Vec<Line<'static>>,
    cur: Vec<Span<'static>>,
    bold: u32,
    italic: u32,
    strike: u32,
    link: u32,
    heading: Option<HeadingLevel>,
    /// Ordered-list counters (`Some(next)`) or bullets (`None`), by depth.
    list_stack: Vec<Option<u64>>,
    quote_depth: usize,
    /// Set at `Item` start, emitted before the item's first content so a
    /// task-list marker can replace the plain bullet.
    pending_marker: Option<String>,
    /// Fence language + accumulated block text while inside a code block.
    code_block: Option<(Option<LangKind>, String)>,
    /// Rows of cell texts while inside a table (row 0 is the header).
    table: Option<Vec<Vec<String>>>,
    /// Directory local image paths resolve against (#176); None keeps
    /// every image a placeholder.
    base_dir: Option<std::path::PathBuf>,
    images: Vec<MdImage>,
    /// Set while inside a RESERVED image's tag pair: pulldown-cmark
    /// emits the alt content between Tag::Image and TagEnd::Image, and
    /// letting it through painted the alt text after the reserved rows
    /// (#196 review). Placeholder images keep their alt.
    suppress_inline: bool,
}

impl Renderer<'_> {
    fn inline_style(&self) -> Style {
        let mut style = Style::default().fg(self.theme.ui(FG));
        if let Some(level) = self.heading {
            style = style.add_modifier(Modifier::BOLD);
            style = match level {
                HeadingLevel::H1 => style
                    .fg(self.theme.accent())
                    .add_modifier(Modifier::UNDERLINED),
                HeadingLevel::H2 => style.fg(self.theme.accent()),
                HeadingLevel::H3 => style.fg(self.theme.ui(FG)),
                _ => style.fg(self.theme.ui(DIM)),
            };
        }
        if self.bold > 0 {
            style = style.add_modifier(Modifier::BOLD);
        }
        if self.italic > 0 {
            style = style.add_modifier(Modifier::ITALIC);
        }
        if self.strike > 0 {
            style = style.add_modifier(Modifier::CROSSED_OUT);
        }
        if self.link > 0 {
            style = style
                .fg(self.theme.accent())
                .add_modifier(Modifier::UNDERLINED);
        }
        if self.quote_depth > 0 {
            style = style.fg(self.theme.ui(DIM)).add_modifier(Modifier::ITALIC);
        }
        style
    }

    /// The block prefix a fresh content line carries: quote bars, then list
    /// indentation, then a pending item marker.
    fn begin_content(&mut self) {
        if !self.cur.is_empty() {
            return;
        }
        for _ in 0..self.quote_depth {
            self.cur.push(Span::styled(
                "\u{258e} ",
                Style::default().fg(self.theme.ui(DIM)),
            ));
        }
        if !self.list_stack.is_empty() {
            let depth = self.list_stack.len() - 1;
            self.cur.push(Span::raw("  ".repeat(depth)));
        }
        if let Some(marker) = self.pending_marker.take() {
            self.cur.push(Span::styled(
                marker,
                Style::default().fg(self.theme.accent()),
            ));
        } else if !self.list_stack.is_empty() {
            // A wrapped/continued line inside an item hangs under its text.
            self.cur.push(Span::raw("  "));
        }
    }

    fn push_text(&mut self, text: &str, style: Style) {
        self.begin_content();
        self.cur.push(Span::styled(text.to_string(), style));
    }

    fn flush_line(&mut self) {
        if self.cur.is_empty() {
            return;
        }
        let spans = std::mem::take(&mut self.cur);
        self.out.push(Line::from(spans));
    }

    fn ensure_blank(&mut self) {
        self.flush_line();
        if self.out.last().is_some_and(|l| !l.spans.is_empty()) {
            self.out.push(Line::default());
        }
    }

    fn end_code_block(&mut self) {
        let Some((kind, text)) = self.code_block.take() else {
            return;
        };
        let bar = Span::styled("\u{258e} ", Style::default().fg(self.theme.accent()));
        let (fr, fg_, fb) = self.theme.syntax().fg;
        let code_fg = Color::Rgb(fr, fg_, fb);
        let highlighted = kind.map(|k| {
            let bytes = text.as_bytes();
            let line_starts = compute_line_starts(bytes);
            highlight_text(self.registry, k, bytes, &line_starts)
        });
        for (i, line) in text.lines().enumerate() {
            let mut spans = vec![bar.clone()];
            match highlighted.as_ref().and_then(|h| h.get(i)) {
                Some(hi) if !hi.is_empty() => spans.extend(code_line_spans(line, hi, code_fg)),
                _ => spans.push(Span::styled(line.to_string(), Style::default().fg(code_fg))),
            }
            self.out.push(Line::from(spans));
        }
        self.out.push(Line::default());
    }

    fn end_table(&mut self) {
        let Some(rows) = self.table.take() else {
            return;
        };
        if rows.is_empty() {
            return;
        }
        let cols = rows.iter().map(Vec::len).max().unwrap_or(0);
        let mut widths = vec![0usize; cols];
        for row in &rows {
            for (i, cell) in row.iter().enumerate() {
                widths[i] = widths[i].max(cell.chars().count());
            }
        }
        for (r, row) in rows.iter().enumerate() {
            let mut spans: Vec<Span<'static>> = Vec::new();
            let style = if r == 0 {
                Style::default()
                    .fg(self.theme.ui(FG))
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(self.theme.ui(FG))
            };
            for (i, width) in widths.iter().enumerate() {
                if i > 0 {
                    spans.push(Span::styled(
                        " \u{2502} ",
                        Style::default().fg(self.theme.ui(DIM)),
                    ));
                }
                let cell = row.get(i).map(String::as_str).unwrap_or("");
                let pad = width.saturating_sub(cell.chars().count());
                spans.push(Span::styled(format!("{cell}{}", " ".repeat(pad)), style));
            }
            self.out.push(Line::from(spans));
            if r == 0 {
                // Header separator: ─ runs joined by ┼ at each column seam.
                let mut sep: Vec<Span<'static>> = Vec::new();
                for (i, width) in widths.iter().enumerate() {
                    if i > 0 {
                        sep.push(Span::styled(
                            "\u{2500}\u{253c}\u{2500}",
                            Style::default().fg(self.theme.ui(DIM)),
                        ));
                    }
                    sep.push(Span::styled(
                        "\u{2500}".repeat(*width),
                        Style::default().fg(self.theme.ui(DIM)),
                    ));
                }
                self.out.push(Line::from(sep));
            }
        }
        self.out.push(Line::default());
    }
}

/// Render markdown `text` into styled preview lines.
/// Image-less build: used by tests and any caller with no source
/// directory to resolve pictures against.
#[cfg_attr(not(test), allow(dead_code))]
pub fn render_markdown(
    text: &str,
    theme: Theme,
    registry: &mut LangRegistry,
) -> Vec<Line<'static>> {
    render_markdown_with_images(text, theme, registry, None).0
}

/// Like [`render_markdown`], resolving local images against `base_dir`
/// into reserved blocks (#176).
pub fn render_markdown_with_images(
    text: &str,
    theme: Theme,
    registry: &mut LangRegistry,
    base_dir: Option<&std::path::Path>,
) -> (Vec<Line<'static>>, Vec<MdImage>) {
    let options =
        Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TASKLISTS;
    let mut r = Renderer {
        theme,
        registry,
        out: Vec::new(),
        cur: Vec::new(),
        bold: 0,
        italic: 0,
        strike: 0,
        link: 0,
        heading: None,
        list_stack: Vec::new(),
        quote_depth: 0,
        pending_marker: None,
        code_block: None,
        table: None,
        base_dir: base_dir.map(|p| p.to_path_buf()),
        images: Vec::new(),
        suppress_inline: false,
    };
    for event in Parser::new_ext(text, options) {
        match event {
            Event::Start(tag) => match tag {
                Tag::Heading { level, .. } => {
                    r.ensure_blank();
                    r.heading = Some(level);
                }
                Tag::Paragraph if r.list_stack.is_empty() && r.quote_depth == 0 => {
                    r.ensure_blank();
                }
                Tag::CodeBlock(kind) => {
                    r.ensure_blank();
                    let lang = match &kind {
                        CodeBlockKind::Fenced(info) => lang_for_fence(info),
                        CodeBlockKind::Indented => None,
                    };
                    r.code_block = Some((lang, String::new()));
                }
                Tag::List(start) => {
                    if r.list_stack.is_empty() {
                        r.ensure_blank();
                    } else {
                        r.flush_line();
                    }
                    r.list_stack.push(start);
                }
                Tag::Item => {
                    r.flush_line();
                    let marker = match r.list_stack.last_mut() {
                        Some(Some(n)) => {
                            let m = format!("{n}. ");
                            *n += 1;
                            m
                        }
                        _ => "\u{2022} ".to_string(),
                    };
                    r.pending_marker = Some(marker);
                }
                Tag::BlockQuote(_) => {
                    r.ensure_blank();
                    r.quote_depth += 1;
                }
                Tag::Emphasis => r.italic += 1,
                Tag::Strong => r.bold += 1,
                Tag::Strikethrough => r.strike += 1,
                Tag::Link { .. } => r.link += 1,
                Tag::Image { dest_url, .. } => {
                    // Local, existing, decodable images reserve blank
                    // lines the app's inline-image overlay paints into
                    // (#176). Remote URLs and misses keep the labelled
                    // placeholder - the preview never fetches.
                    let local = (!dest_url.contains("://"))
                        .then(|| r.base_dir.as_ref().map(|d| d.join(dest_url.as_ref())))
                        .flatten()
                        .filter(|p| p.is_file());
                    let dims = local.as_ref().and_then(|p| image::image_dimensions(p).ok());
                    if let (Some(path), Some((px_w, px_h))) = (local, dims) {
                        r.ensure_blank();
                        // Cells are roughly twice as tall as wide; the
                        // preview column is ~72 cells. Clamped so a tall
                        // banner cannot swallow the viewport.
                        let rows = ((px_h as f32 / px_w.max(1) as f32) * 72.0 / 2.0)
                            .round()
                            .clamp(3.0, 18.0) as u16;
                        let first_line = r.out.len();
                        for _ in 0..rows {
                            r.out.push(Line::default());
                        }
                        r.images.push(MdImage {
                            first_line,
                            rows,
                            path,
                        });
                        r.suppress_inline = true;
                    } else {
                        r.begin_content();
                        let style = Style::default()
                            .fg(theme.ui(DIM))
                            .add_modifier(Modifier::ITALIC);
                        r.cur.push(Span::styled("\u{f03e} ", style));
                        r.cur.push(Span::styled(format!("({dest_url}) "), style));
                    }
                }
                Tag::Table(_) => {
                    r.ensure_blank();
                    r.table = Some(Vec::new());
                }
                Tag::TableHead => {
                    if let Some(rows) = r.table.as_mut() {
                        rows.push(Vec::new());
                    }
                }
                Tag::TableRow => {
                    if let Some(rows) = r.table.as_mut() {
                        rows.push(Vec::new());
                    }
                }
                Tag::TableCell => {
                    if let Some(rows) = r.table.as_mut()
                        && let Some(row) = rows.last_mut()
                    {
                        row.push(String::new());
                    }
                }
                _ => {}
            },
            Event::End(tag) => match tag {
                TagEnd::Heading(_) => {
                    r.flush_line();
                    r.heading = None;
                    r.out.push(Line::default());
                }
                TagEnd::Paragraph => {
                    r.flush_line();
                    if r.list_stack.is_empty() && r.quote_depth == 0 {
                        r.out.push(Line::default());
                    }
                }
                TagEnd::CodeBlock => r.end_code_block(),
                TagEnd::List(_) => {
                    r.flush_line();
                    r.list_stack.pop();
                    if r.list_stack.is_empty() {
                        r.out.push(Line::default());
                    }
                }
                TagEnd::Item => r.flush_line(),
                TagEnd::BlockQuote(_) => {
                    r.flush_line();
                    r.quote_depth = r.quote_depth.saturating_sub(1);
                    if r.quote_depth == 0 {
                        r.out.push(Line::default());
                    }
                }
                TagEnd::Emphasis => r.italic = r.italic.saturating_sub(1),
                TagEnd::Strong => r.bold = r.bold.saturating_sub(1),
                TagEnd::Strikethrough => r.strike = r.strike.saturating_sub(1),
                TagEnd::Link => r.link = r.link.saturating_sub(1),
                TagEnd::Image => r.suppress_inline = false,
                TagEnd::Table => r.end_table(),
                _ => {}
            },
            Event::Text(text) => {
                if r.suppress_inline {
                } else if let Some((_, buf)) = r.code_block.as_mut() {
                    buf.push_str(&text);
                } else if let Some(rows) = r.table.as_mut() {
                    if let Some(cell) = rows.last_mut().and_then(|row| row.last_mut()) {
                        cell.push_str(&text);
                    }
                } else {
                    let style = r.inline_style();
                    r.push_text(&text, style);
                }
            }
            Event::Code(code) => {
                if r.suppress_inline {
                } else if let Some(rows) = r.table.as_mut() {
                    if let Some(cell) = rows.last_mut().and_then(|row| row.last_mut()) {
                        cell.push_str(&code);
                    }
                } else {
                    r.begin_content();
                    r.cur.push(Span::styled(
                        code.to_string(),
                        Style::default().fg(theme.ui(CODE)),
                    ));
                }
            }
            Event::SoftBreak => {
                if !r.suppress_inline {
                    let style = r.inline_style();
                    r.push_text(" ", style);
                }
            }
            Event::HardBreak => {
                if !r.suppress_inline {
                    r.flush_line();
                }
            }
            Event::Rule => {
                r.ensure_blank();
                r.out.push(Line::from(Span::styled(
                    "\u{2500}".repeat(RULE_COLS),
                    Style::default().fg(theme.ui(DIM)),
                )));
                r.out.push(Line::default());
            }
            Event::TaskListMarker(checked) => {
                r.pending_marker = Some(if checked {
                    "\u{2611} ".to_string()
                } else {
                    "\u{2610} ".to_string()
                });
            }
            _ => {}
        }
    }
    r.flush_line();
    // Trim the leading/trailing blank separators so the preview starts at
    // the first real line - shifting the image anchors with the front
    // trim, and never eating a trailing image's RESERVED blanks.
    let reserved_end = r
        .images
        .iter()
        .map(|i| i.first_line + i.rows as usize)
        .max()
        .unwrap_or(0);
    let mut removed_front = 0usize;
    while r.out.first().is_some_and(|l| l.spans.is_empty())
        && r.images
            .first()
            .is_none_or(|i| removed_front < i.first_line)
    {
        r.out.remove(0);
        removed_front += 1;
    }
    for img in &mut r.images {
        img.first_line -= removed_front;
    }
    while r.out.len() > reserved_end.saturating_sub(removed_front)
        && r.out.last().is_some_and(|l| l.spans.is_empty())
    {
        r.out.pop();
    }
    (r.out, r.images)
}

impl MarkdownPreview {
    /// The selection normalised to (start, end) in reading order.
    fn ordered_selection(&self) -> Option<((u16, u16), (u16, u16))> {
        let (a, b) = self.selection?;
        // A click anchors head == anchor: that is a caret, not a
        // selection. Treating it as one would tint a stray cell and let a
        // plain click copy a character nobody selected.
        if a == b {
            return None;
        }
        Some(if (a.0, a.1) <= (b.0, b.1) {
            (a, b)
        } else {
            (b, a)
        })
    }

    /// True when cell (row, col) of the rendered view is selected. The
    /// render pass paints these cells with the selection background.
    pub fn cell_selected(&self, row: u16, col: u16) -> bool {
        let Some((start, end)) = self.ordered_selection() else {
            return false;
        };
        if row < start.0 || row > end.0 {
            return false;
        }
        let from = if row == start.0 { start.1 } else { 0 };
        let to = if row == end.0 { end.1 } else { u16::MAX };
        col >= from && col <= to
    }

    /// The selected text of the rendered view, extracted from the rows the
    /// last render recorded. Rows join with newlines, exactly as they read
    /// on screen (VS Code copies the rendered text, not the source).
    pub fn selection_text(&self) -> String {
        let Some((start, end)) = self.ordered_selection() else {
            return String::new();
        };
        let mut out: Vec<String> = Vec::new();
        for row in start.0..=end.0 {
            let Some(cells) = self.rows.get(row as usize) else {
                continue;
            };
            let from = if row == start.0 {
                (start.1 as usize).min(cells.len())
            } else {
                0
            };
            let to = if row == end.0 {
                ((end.1 as usize) + 1).min(cells.len())
            } else {
                cells.len()
            };
            // Column-indexed: a wide glyph's continuation cell is empty and
            // contributes nothing, so copied text keeps characters whole
            // however the row is sliced.
            out.push(if from <= to {
                cells[from..to].concat().trim_end().to_string()
            } else {
                String::new()
            });
        }
        // A single-row selection has no line break; multi-row keeps the
        // visual line structure, trailing padding already trimmed.
        out.join("\n").trim_end().to_string()
    }

    /// Whether anything is selected (drives the copy path and the
    /// "clear selection" gestures).
    pub fn has_selection(&self) -> bool {
        self.ordered_selection().is_some()
    }
}

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

    fn render(text: &str) -> Vec<Line<'static>> {
        render_markdown(text, Theme::default(), &mut LangRegistry::new())
    }

    fn text_of(line: &Line) -> String {
        line.spans.iter().map(|s| s.content.as_ref()).collect()
    }

    #[test]
    fn local_images_reserve_anchored_rows_and_urls_stay_placeholders() {
        let tmp = tempfile::tempdir().unwrap();
        image::RgbaImage::new(100, 50)
            .save(tmp.path().join("pic.png"))
            .unwrap();
        let text = "# Title\n\n![local](pic.png)\n\n![remote](https://x/y.png)\n\n![gone](nope.png)\n\ntail";
        let mut reg = crate::highlight::LangRegistry::default();
        let (lines, images) =
            super::render_markdown_with_images(text, Theme::BLACK, &mut reg, Some(tmp.path()));
        assert_eq!(images.len(), 1, "only the local existing image reserves");
        let img = &images[0];
        assert!(img.path.ends_with("pic.png"));
        // 100x50 at the 72-col budget: (50/100)*72/2 = 18 rows.
        assert_eq!(img.rows, 18);
        for i in 0..img.rows as usize {
            assert!(
                lines[img.first_line + i].spans.is_empty(),
                "reserved line {i} must be blank"
            );
        }
        let all = all_text(&lines);
        assert!(all.contains("https://x/y.png"), "URL keeps the placeholder");
        assert!(
            all.contains("nope.png"),
            "missing file keeps the placeholder"
        );
        assert!(all.contains("tail"));
    }

    #[test]
    fn reserved_image_alt_text_is_suppressed_but_placeholder_alt_survives() {
        // #196 review: pulldown-cmark emits the alt content between the
        // image tag pair; a reserved image must swallow it, a
        // placeholder keeps its label.
        let tmp = tempfile::tempdir().unwrap();
        image::RgbaImage::new(10, 10)
            .save(tmp.path().join("p.png"))
            .unwrap();
        let text = "![the alt words](p.png)\n\n![web alt](https://x/y.png)";
        let mut reg = crate::highlight::LangRegistry::default();
        let (lines, images) =
            super::render_markdown_with_images(text, Theme::BLACK, &mut reg, Some(tmp.path()));
        assert_eq!(images.len(), 1);
        let all = all_text(&lines);
        assert!(
            !all.contains("the alt words"),
            "reserved image swallows its alt: {all}"
        );
        assert!(all.contains("web alt"), "placeholder keeps its alt");
    }

    #[test]
    fn a_leading_image_survives_the_blank_trim_with_true_anchors() {
        let tmp = tempfile::tempdir().unwrap();
        image::RgbaImage::new(100, 200)
            .save(tmp.path().join("tall.png"))
            .unwrap();
        let text = "![t](tall.png)";
        let mut reg = crate::highlight::LangRegistry::default();
        let (lines, images) =
            super::render_markdown_with_images(text, Theme::BLACK, &mut reg, Some(tmp.path()));
        assert_eq!(images.len(), 1);
        assert_eq!(images[0].first_line, 0, "front trim shifted the anchor");
        assert_eq!(images[0].rows, 18, "tall image clamps at 18");
        assert!(
            lines.len() >= images[0].rows as usize,
            "the tail trim must not eat reserved rows: {} lines",
            lines.len()
        );
    }

    fn all_text(lines: &[Line]) -> String {
        lines.iter().map(text_of).collect::<Vec<_>>().join("\n")
    }

    #[test]
    fn heading_is_bold_and_separated_from_the_paragraph() {
        let lines = render("# Title\n\nBody text here.");
        assert_eq!(text_of(&lines[0]), "Title");
        assert!(
            lines[0].spans[0]
                .style
                .add_modifier
                .contains(Modifier::BOLD),
            "an H1 must render bold"
        );
        assert!(
            lines[1].spans.is_empty(),
            "a blank separator must follow the heading"
        );
        assert_eq!(text_of(&lines[2]), "Body text here.");
    }

    #[test]
    fn emphasis_inline_code_and_strikethrough_carry_their_modifiers() {
        let lines = render("mix of **bold**, *italic*, ~~gone~~, and `code()` inline");
        let line = &lines[0];
        let span_with = |t: &str| {
            line.spans
                .iter()
                .find(|s| s.content.as_ref() == t)
                .unwrap_or_else(|| panic!("span {t:?} missing in {:?}", text_of(line)))
                .clone()
        };
        assert!(
            span_with("bold")
                .style
                .add_modifier
                .contains(Modifier::BOLD)
        );
        assert!(
            span_with("italic")
                .style
                .add_modifier
                .contains(Modifier::ITALIC)
        );
        assert!(
            span_with("gone")
                .style
                .add_modifier
                .contains(Modifier::CROSSED_OUT)
        );
        assert_eq!(span_with("code()").style.fg, Some(CODE));
    }

    #[test]
    fn fenced_rust_block_gets_tree_sitter_colours() {
        let lines = render("```rust\nfn main() {}\n```");
        let code_line = lines
            .iter()
            .find(|l| text_of(l).contains("fn main"))
            .expect("the code line must render");
        let keyword = code_line
            .spans
            .iter()
            .find(|s| s.content.as_ref().trim() == "fn")
            .expect("the fn keyword must be its own span");
        assert_eq!(
            keyword.style.fg,
            Some(Color::Rgb(0xB4, 0x8E, 0xAD)),
            "the fenced block must carry the editor's keyword colour"
        );
    }

    #[test]
    fn lists_nest_with_indentation_and_ordered_counters() {
        let lines = render("- top\n  - inner\n\n1. first\n2. second");
        let text = all_text(&lines);
        assert!(text.contains("\u{2022} top"), "got:\n{text}");
        assert!(text.contains("  \u{2022} inner"), "got:\n{text}");
        assert!(text.contains("1. first"), "got:\n{text}");
        assert!(text.contains("2. second"), "got:\n{text}");
    }

    #[test]
    fn task_list_markers_render_as_checkboxes() {
        let text = all_text(&render("- [x] done\n- [ ] todo"));
        assert!(text.contains("\u{2611} done"), "got:\n{text}");
        assert!(text.contains("\u{2610} todo"), "got:\n{text}");
    }

    #[test]
    fn blockquote_carries_the_bar_prefix() {
        let text = all_text(&render("> quoted wisdom"));
        assert!(text.contains("\u{258e} quoted wisdom"), "got:\n{text}");
    }

    #[test]
    fn table_columns_pad_to_the_widest_cell() {
        let lines = render("| name | value |\n| --- | --- |\n| a | long-value |\n| bbbb | c |");
        let text = all_text(&lines);
        assert!(text.contains("name \u{2502} value"), "got:\n{text}");
        assert!(
            text.contains("a    \u{2502} long-value"),
            "cells must pad to the widest column; got:\n{text}"
        );
        assert!(
            text.contains("\u{2500}\u{253c}\u{2500}"),
            "a rule must separate the header; got:\n{text}"
        );
    }

    #[test]
    fn links_underline_and_rules_draw() {
        let lines = render("see [the docs](https://example.com)\n\n---");
        let link = lines[0]
            .spans
            .iter()
            .find(|s| s.content.as_ref() == "the docs")
            .expect("link text must render");
        assert!(link.style.add_modifier.contains(Modifier::UNDERLINED));
        assert!(all_text(&lines).contains(&"\u{2500}".repeat(RULE_COLS)));
    }

    #[test]
    fn code_block_text_follows_the_theme_palette_not_base16() {
        // A fence in a language the highlighter doesn't know (and any gap the
        // highlighter leaves) must paint the ACTIVE theme's code foreground.
        // The hardcoded Base16 slate left cold-grey code islands inside an
        // otherwise-themed Gruvbox/Dracula preview.
        let theme = *crate::theme::Theme::all()
            .iter()
            .find(|t| t.syntax().fg != crate::theme::SyntaxPalette::BASE16.fg)
            .expect("a bundled theme with a non-Base16 code palette");
        let (r, g, b) = theme.syntax().fg;
        let lines = render_markdown(
            "```notalanguage\nplain code line\n```\n",
            theme,
            &mut LangRegistry::new(),
        );
        let code = lines
            .iter()
            .flat_map(|l| l.spans.iter())
            .find(|s| s.content.as_ref() == "plain code line")
            .expect("the code line must render");
        assert_eq!(
            code.style.fg,
            Some(Color::Rgb(r, g, b)),
            "unhighlighted code must wear the theme's code fg, not Base16 slate"
        );
    }
}

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

    fn preview(rows: &[&str]) -> MarkdownPreview {
        MarkdownPreview {
            lines: Vec::new(),
            scroll: 0,
            built_seq: 0,
            images: Vec::new(),
            anchor_rows: Vec::new(),
            wrap_key: (0, 0),
            last_area: ratatui::layout::Rect::default(),
            rows: rows
                .iter()
                .map(|r| r.chars().map(|c| c.to_string()).collect())
                .collect(),
            selection: None,
            dragging: false,
            notebook: false,
            doc_path: None,
            media: false,
        }
    }

    /// Issue #215: the rendered Markdown view must be selectable. A drag
    /// within one row copies that span; a multi-row drag keeps the visual
    /// line structure; the selection is direction-agnostic.
    #[test]
    fn selection_text_extracts_the_rendered_rows() {
        let mut p = preview(&["Hello world", "second line", "third line"]);
        // Single row, columns 0..=4 -> "Hello".
        p.selection = Some(((0, 0), (0, 4)));
        assert_eq!(p.selection_text(), "Hello");
        // Multi-row: tail of row 0, all of row 1, head of row 2.
        p.selection = Some(((0, 6), (2, 4)));
        assert_eq!(p.selection_text(), "world\nsecond line\nthird");
        // Dragging upward selects the same span.
        p.selection = Some(((2, 4), (0, 6)));
        assert_eq!(p.selection_text(), "world\nsecond line\nthird");
        // No selection copies nothing.
        p.selection = None;
        assert_eq!(p.selection_text(), "");
        assert!(!p.has_selection());
    }

    /// Review round 1: a plain click (anchor == head) is a caret, not a
    /// selection: nothing tints and nothing copies.
    #[test]
    fn a_zero_area_click_is_not_a_selection() {
        let mut p = preview(&["abcdef"]);
        p.selection = Some(((0, 3), (0, 3)));
        assert!(!p.has_selection(), "a click selects nothing");
        assert!(!p.cell_selected(0, 3), "and tints nothing");
        assert_eq!(p.selection_text(), "", "and copies nothing");
    }

    /// Review round 1: rows are stored per SCREEN COLUMN, so a wide glyph
    /// (which owns one column and leaves an empty continuation cell) is
    /// copied whole and the columns after it still address the right
    /// characters.
    #[test]
    fn wide_glyphs_keep_columns_and_characters_aligned() {
        // Columns:      0     1(cont) 2    3
        let mut p = preview(&[""]);
        p.rows = vec![vec![
            "\u{5e83}".to_string(),
            String::new(),
            "a".to_string(),
            "b".to_string(),
        ]];
        p.selection = Some(((0, 0), (0, 2)));
        assert_eq!(
            p.selection_text(),
            "\u{5e83}a",
            "the wide glyph and the character at column 2 come through"
        );
        p.selection = Some(((0, 2), (0, 3)));
        assert_eq!(
            p.selection_text(),
            "ab",
            "columns after a wide glyph still address their own characters"
        );
    }

    #[test]
    fn cell_selected_marks_exactly_the_dragged_cells() {
        let mut p = preview(&["abcdef", "ghijkl"]);
        p.selection = Some(((0, 2), (1, 1)));
        assert!(!p.cell_selected(0, 1), "before the anchor column");
        assert!(p.cell_selected(0, 2), "the anchor cell");
        assert!(p.cell_selected(0, 5), "to the end of the first row");
        assert!(p.cell_selected(1, 0), "the head row from column 0");
        assert!(p.cell_selected(1, 1), "up to the head column");
        assert!(!p.cell_selected(1, 2), "past the head column");
        assert!(!p.cell_selected(2, 0), "below the selection");
    }

    /// A selection that outlives a rebuild (fewer rows) must not panic or
    /// invent text.
    #[test]
    fn selection_past_the_rendered_rows_is_harmless() {
        let mut p = preview(&["only row"]);
        p.selection = Some(((0, 2), (9, 40)));
        assert_eq!(p.selection_text(), "ly row");
    }
}