clankerdiff-ratatui 0.1.0

Embeddable Ratatui diff review widget
Documentation
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
//! Read-only whole-document and append-stream Markdown rendering.

use crate::syntax::highlighted_line;
use clankerdiff_markdown::{
    MarkdownBlock, MarkdownBlockKind, MarkdownDocument, MarkdownInline, MarkdownStream,
    MarkdownStreamIdentity,
};
use clankerdiff_syntax::{DocumentHighlights, LanguageHint, SourceSequenceId, SyntaxHighlighter};
use clankerdiff_theme::{Fingerprint, ReviewTheme, Rgba};
use ratatui::{
    style::{Color, Modifier, Style},
    text::{Line, Span},
};
use std::sync::Arc;
use unicode_width::UnicodeWidthChar;

/// Width and spacing policy for transcript-style Markdown.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MarkdownRenderOptions {
    pub width: u16,
    pub block_spacing: bool,
}

impl Default for MarkdownRenderOptions {
    fn default() -> Self {
        Self {
            width: 80,
            block_spacing: true,
        }
    }
}

/// Deterministic work counters for incremental Markdown rendering.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct MarkdownRenderStats {
    /// Bytes actually supplied to the Markdown parser.
    pub parsed_bytes: usize,
    pub parsed_documents: u64,
    pub rows_generated: usize,
    pub rows_reused: usize,
}

/// Renderer-owned cache for one logical streaming Markdown item.
#[derive(Debug, Clone, Default)]
pub struct StreamingMarkdownState {
    parsed: Option<ParsedSource>,
    options: MarkdownRenderOptions,
    theme_revision: Fingerprint,
    lines: Arc<[Line<'static>]>,
    stats: MarkdownRenderStats,
}

impl StreamingMarkdownState {
    pub fn reset(&mut self) {
        *self = Self::default();
    }

    /// Returns accumulated renderer work counters and resets them.
    pub fn take_stats(&mut self) -> MarkdownRenderStats {
        std::mem::take(&mut self.stats)
    }
}

#[derive(Debug, Clone)]
struct ParsedSource {
    identity: MarkdownStreamIdentity,
    revision: u64,
    document: MarkdownDocument,
}

impl ParsedSource {
    fn parse(stream: &MarkdownStream) -> Self {
        Self {
            identity: stream.identity(),
            revision: stream.revision(),
            document: MarkdownDocument::parse(stream.source()),
        }
    }

    fn matches(&self, stream: &MarkdownStream) -> bool {
        self.identity == stream.identity() && self.revision == stream.revision()
    }
}

/// Stateless whole-document renderer plus streaming cache services.
#[derive(Debug, Default)]
pub struct MarkdownRenderer;

impl MarkdownRenderer {
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Renders a canonical semantic document without review gutters or controls.
    #[must_use]
    pub fn render_lines(
        &self,
        document: &MarkdownDocument,
        options: MarkdownRenderOptions,
        theme: &ReviewTheme,
        highlighter: &mut SyntaxHighlighter,
    ) -> Arc<[Line<'static>]> {
        Arc::from(document_rows(document, options, theme, highlighter))
    }

    pub fn render_stream_lines(
        &self,
        state: &mut StreamingMarkdownState,
        stream: &MarkdownStream,
        options: MarkdownRenderOptions,
        theme: &ReviewTheme,
        highlighter: &mut SyntaxHighlighter,
    ) -> Arc<[Line<'static>]> {
        let theme_revision = theme.revision();
        let parsed = match state.parsed.take() {
            Some(parsed) if parsed.matches(stream) => {
                if state.options == options && state.theme_revision == theme_revision {
                    state.stats.rows_reused += state.lines.len();
                    state.parsed = Some(parsed);
                    return Arc::clone(&state.lines);
                }
                parsed
            }
            _ => {
                state.stats.parsed_bytes += stream.source().len();
                state.stats.parsed_documents += 1;
                ParsedSource::parse(stream)
            }
        };
        let rows = document_rows(&parsed.document, options, theme, highlighter);
        state.stats.rows_generated += rows.len();
        state.parsed = Some(parsed);
        state.options = options;
        state.theme_revision = theme_revision;
        state.lines = Arc::from(rows);
        Arc::clone(&state.lines)
    }
}

fn document_rows(
    document: &MarkdownDocument,
    options: MarkdownRenderOptions,
    theme: &ReviewTheme,
    highlighter: &mut SyntaxHighlighter,
) -> Vec<Line<'static>> {
    let mut output = Vec::new();
    for (index, block) in document.blocks().iter().enumerate() {
        render_block(
            block,
            options.width.max(1),
            theme,
            highlighter,
            &mut output,
            "",
        );
        if options.block_spacing && index + 1 < document.blocks().len() {
            output.push(Line::default());
        }
    }
    output
}

/// Renders highlighted code lines as wrapped rows on the code background.
fn code_rows(
    lines: &[&str],
    highlights: &DocumentHighlights,
    theme: &ReviewTheme,
    width: u16,
    prefix: &str,
) -> Vec<Line<'static>> {
    let base = Style::new()
        .fg(color(theme.markdown.code))
        .bg(color(theme.diff.background));
    let mut output = Vec::new();
    for (index, line) in lines.iter().enumerate() {
        let spans = highlights.line(index).unwrap_or_default();
        let mut rendered = highlighted_line(line, spans, base);
        if !prefix.is_empty() {
            rendered
                .spans
                .insert(0, Span::styled(prefix.to_owned(), base));
        }
        push_wrapped_spans(&mut output, rendered.spans, width);
    }
    output
}

fn render_block(
    block: &MarkdownBlock,
    width: u16,
    theme: &ReviewTheme,
    highlighter: &mut SyntaxHighlighter,
    output: &mut Vec<Line<'static>>,
    prefix: &str,
) {
    match &block.kind {
        MarkdownBlockKind::Heading { level, content } => {
            let marker = format!("{} ", "#".repeat(usize::from(*level)));
            let base = Style::new()
                .fg(color(theme.markdown.heading))
                .add_modifier(Modifier::BOLD);
            let mut spans = vec![Span::styled(format!("{prefix}{marker}"), base)];
            spans.extend(inline_spans(content, base, theme));
            push_wrapped_spans(output, spans, width);
        }
        MarkdownBlockKind::Paragraph { content } | MarkdownBlockKind::HtmlFallback { content } => {
            let base = Style::new().fg(color(theme.diff.foreground));
            let mut spans = vec![Span::styled(prefix.to_owned(), base)];
            spans.extend(inline_spans(content, base, theme));
            push_wrapped_spans(output, spans, width);
        }
        MarkdownBlockKind::List {
            ordered,
            start,
            items,
        } => {
            for (index, item) in items.iter().enumerate() {
                let marker = if *ordered {
                    format!("{}.", start.unwrap_or(1).saturating_add(index as u64))
                } else {
                    "•".to_owned()
                };
                let base = Style::new().fg(color(theme.diff.foreground));
                let mut spans = vec![Span::styled(
                    format!("{prefix}{}{marker} ", "  ".repeat(item.depth)),
                    base,
                )];
                spans.extend(inline_spans(&item.content, base, theme));
                push_wrapped_spans(output, spans, width);
                for child in &item.blocks {
                    render_block(
                        child,
                        width,
                        theme,
                        highlighter,
                        output,
                        &format!("{prefix}  "),
                    );
                }
            }
        }
        MarkdownBlockKind::BlockQuote { blocks } => {
            for child in blocks {
                render_block(
                    child,
                    width,
                    theme,
                    highlighter,
                    output,
                    &format!("{prefix}│ "),
                );
            }
        }
        MarkdownBlockKind::CodeBlock(code) => {
            let lines = code
                .lines
                .iter()
                .map(|line| line.text.as_str())
                .collect::<Vec<_>>();
            let highlights = highlighter
                .with_theme(&theme.syntax)
                .highlight_document_lines(
                    SourceSequenceId::from_lines(lines.iter().copied()),
                    LanguageHint::InfoString(code.highlight_hint()),
                    lines.iter().copied(),
                );
            output.extend(code_rows(&lines, &highlights, theme, width, prefix));
        }
        MarkdownBlockKind::Table(table) => {
            for row in &table.rows {
                let base = Style::new().fg(color(theme.diff.foreground));
                let border = Style::new().fg(color(theme.diff.border));
                let mut spans = vec![Span::styled(format!("{prefix}│ "), border)];
                for (index, cell) in row.cells.iter().enumerate() {
                    if index > 0 {
                        spans.push(Span::styled(" │ ", border));
                    }
                    let cell_base = if row.header {
                        base.add_modifier(Modifier::BOLD)
                    } else {
                        base
                    };
                    spans.extend(inline_spans(&cell.content, cell_base, theme));
                }
                spans.push(Span::styled(" │", border));
                push_wrapped_spans(output, spans, width);
            }
        }
        MarkdownBlockKind::Rule => output.push(Line::styled(
            "─".repeat(usize::from(width)),
            Style::new().fg(color(theme.diff.border)),
        )),
    }
}

fn inline_spans(
    inlines: &[MarkdownInline],
    style: Style,
    theme: &ReviewTheme,
) -> Vec<Span<'static>> {
    fn append(
        inline: &MarkdownInline,
        style: Style,
        theme: &ReviewTheme,
        output: &mut Vec<Span<'static>>,
    ) {
        match inline {
            MarkdownInline::Text(text) => output.push(Span::styled(text.clone(), style)),
            MarkdownInline::Code(text) => output.push(Span::styled(
                text.clone(),
                style
                    .fg(color(theme.markdown.code))
                    .bg(color(theme.diff.border)),
            )),
            MarkdownInline::Strong(children) => children.iter().for_each(|child| {
                append(child, style.add_modifier(Modifier::BOLD), theme, output);
            }),
            MarkdownInline::Emphasis(children) => children.iter().for_each(|child| {
                append(child, style.add_modifier(Modifier::ITALIC), theme, output);
            }),
            MarkdownInline::Strikethrough(children) => children.iter().for_each(|child| {
                append(
                    child,
                    style.add_modifier(Modifier::CROSSED_OUT),
                    theme,
                    output,
                );
            }),
            MarkdownInline::Link { content, .. } => content.iter().for_each(|child| {
                append(
                    child,
                    style
                        .fg(color(theme.markdown.link))
                        .add_modifier(Modifier::UNDERLINED),
                    theme,
                    output,
                );
            }),
            MarkdownInline::SoftBreak => output.push(Span::styled(" ", style)),
            MarkdownInline::HardBreak => output.push(Span::styled("\n", style)),
            MarkdownInline::ImageAlt(text) => output.push(Span::styled(
                format!("Image: {text}"),
                style
                    .fg(color(theme.markdown.link))
                    .add_modifier(Modifier::ITALIC),
            )),
        }
    }

    let mut output = Vec::new();
    for inline in inlines {
        append(inline, style, theme, &mut output);
    }
    output
}

fn push_wrapped_spans(output: &mut Vec<Line<'static>>, spans: Vec<Span<'static>>, width: u16) {
    let width = usize::from(width.max(1));
    let mut rows = vec![Line::default()];
    let mut used = 0usize;
    for span in spans {
        let style = span.style;
        for character in span.content.chars() {
            if character == '\n' {
                rows.push(Line::default());
                used = 0;
                continue;
            }
            let character_width = character.width().unwrap_or(0);
            if used > 0 && used.saturating_add(character_width) > width {
                rows.push(Line::default());
                used = 0;
            }
            let row = rows.last_mut().expect("wrapping always retains one row");
            if let Some(last) = row.spans.last_mut().filter(|last| last.style == style) {
                last.content.to_mut().push(character);
            } else {
                row.spans.push(Span::styled(character.to_string(), style));
            }
            used = used.saturating_add(character_width);
        }
    }
    output.extend(rows);
}

const fn color(value: Rgba) -> Color {
    Color::Rgb(value.r, value.g, value.b)
}

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

    #[test]
    fn finished_stream_matches_one_shot_for_every_utf8_chunk_boundary() {
        let source = "# Héading\n\nText with 世界.\n\n```rust\n/* open\nstill comment */\n```\n";
        let options = MarkdownRenderOptions {
            width: 36,
            block_spacing: true,
        };
        let theme = ReviewTheme::default();
        let expected = MarkdownRenderer::new().render_lines(
            &MarkdownDocument::parse(source),
            options,
            &theme,
            &mut SyntaxHighlighter::default(),
        );

        for split in source
            .char_indices()
            .map(|(offset, _)| offset)
            .chain(std::iter::once(source.len()))
        {
            let mut stream = MarkdownStream::new();
            stream.push(&source[..split]);
            stream.push(&source[split..]);
            stream.finish();
            let actual = MarkdownRenderer::new().render_stream_lines(
                &mut StreamingMarkdownState::default(),
                &stream,
                options,
                &theme,
                &mut SyntaxHighlighter::default(),
            );
            assert_eq!(actual, expected, "split at byte {split}");
        }
    }

    #[test]
    fn open_fence_stream_matches_the_current_one_shot_snapshot() {
        let source = "Settled prose.\n\n```rust\n/* open\nstill open";
        let mut stream = MarkdownStream::new();
        for chunk in ["Settled prose.\n\n```rust\n", "/* open\n", "still open"] {
            stream.push(chunk);
        }
        assert!(!stream.is_finished());
        let options = MarkdownRenderOptions {
            width: 48,
            block_spacing: true,
        };
        let theme = ReviewTheme::default();
        let expected = MarkdownRenderer::new().render_lines(
            &MarkdownDocument::parse(source),
            options,
            &theme,
            &mut SyntaxHighlighter::default(),
        );
        let actual = MarkdownRenderer::new().render_stream_lines(
            &mut StreamingMarkdownState::default(),
            &stream,
            options,
            &theme,
            &mut SyntaxHighlighter::default(),
        );
        assert_eq!(actual, expected);
    }

    #[test]
    fn finishing_an_open_fence_commits_its_partial_final_line() {
        let source = "```rust\n/* open\nstill open";
        let mut stream = MarkdownStream::new();
        stream.push(source);
        let mut state = StreamingMarkdownState::default();
        let renderer = MarkdownRenderer::new();
        let options = MarkdownRenderOptions::default();
        let theme = ReviewTheme::default();
        let mut highlighter = SyntaxHighlighter::default();
        renderer.render_stream_lines(&mut state, &stream, options, &theme, &mut highlighter);
        stream.finish();
        let actual =
            renderer.render_stream_lines(&mut state, &stream, options, &theme, &mut highlighter);
        let expected = renderer.render_lines(
            &MarkdownDocument::parse(source),
            options,
            &theme,
            &mut SyntaxHighlighter::default(),
        );
        assert_eq!(actual, expected);
    }

    #[test]
    fn unchanged_stream_reuses_rendered_lines() {
        let mut stream = MarkdownStream::new();
        stream.push("settled\n\n");
        let mut state = StreamingMarkdownState::default();
        let renderer = MarkdownRenderer::new();
        let mut highlighter = SyntaxHighlighter::default();
        let theme = ReviewTheme::default();
        let first = renderer.render_stream_lines(
            &mut state,
            &stream,
            MarkdownRenderOptions::default(),
            &theme,
            &mut highlighter,
        );
        let second = renderer.render_stream_lines(
            &mut state,
            &stream,
            MarkdownRenderOptions::default(),
            &theme,
            &mut highlighter,
        );
        assert!(Arc::ptr_eq(&first, &second));
    }

    #[test]
    fn read_only_rendering_preserves_nested_inline_styles() {
        let source = "**bold and *both***, ~~gone~~, [`link`](https://example.com), `code`.";
        let lines = MarkdownRenderer::new().render_lines(
            &MarkdownDocument::parse(source),
            MarkdownRenderOptions::default(),
            &ReviewTheme::default(),
            &mut SyntaxHighlighter::default(),
        );
        let spans = &lines[0].spans;
        let both = spans
            .iter()
            .find(|span| span.content.contains("both"))
            .unwrap();
        assert!(both.style.add_modifier.contains(Modifier::BOLD));
        assert!(both.style.add_modifier.contains(Modifier::ITALIC));
        let gone = spans
            .iter()
            .find(|span| span.content.contains("gone"))
            .unwrap();
        assert!(gone.style.add_modifier.contains(Modifier::CROSSED_OUT));
        let link = spans
            .iter()
            .find(|span| span.content.contains("link"))
            .unwrap();
        assert!(link.style.add_modifier.contains(Modifier::UNDERLINED));
        let code = spans
            .iter()
            .find(|span| span.content.contains("code"))
            .unwrap();
        assert!(code.style.bg.is_some());
    }

    #[test]
    fn stream_stats_reset_and_changed_snapshots_parse_complete_context() {
        let renderer = MarkdownRenderer::new();
        let theme = ReviewTheme::default();
        let options = MarkdownRenderOptions::default();
        let mut stream = MarkdownStream::new();
        let mut state = StreamingMarkdownState::default();
        let mut highlighter = SyntaxHighlighter::default();
        stream.push("first paragraph\n\n");
        renderer.render_stream_lines(&mut state, &stream, options, &theme, &mut highlighter);
        let first = state.take_stats();
        assert_eq!(first.parsed_bytes, "first paragraph\n\n".len());
        stream.push("second paragraph\n\n");
        renderer.render_stream_lines(&mut state, &stream, options, &theme, &mut highlighter);
        let second = state.take_stats();
        assert_eq!(second.parsed_bytes, stream.source().len());
        assert_eq!(state.take_stats(), MarkdownRenderStats::default());

        stream.replace("replacement is longer than both prior paragraphs\n\n");
        let replaced =
            renderer.render_stream_lines(&mut state, &stream, options, &theme, &mut highlighter);
        let expected = renderer.render_lines(
            &MarkdownDocument::parse(stream.source()),
            options,
            &theme,
            &mut SyntaxHighlighter::default(),
        );
        assert_eq!(replaced, expected);
        assert_eq!(state.take_stats().parsed_bytes, stream.source().len());
    }

    #[test]
    fn stream_cache_invalidates_for_spacing_and_markdown_palette_changes() {
        let mut stream = MarkdownStream::new();
        stream.push("# Heading\n\nParagraph\n\n");
        let mut state = StreamingMarkdownState::default();
        let renderer = MarkdownRenderer::new();
        let mut highlighter = SyntaxHighlighter::default();
        let theme = ReviewTheme::default();
        let spaced = renderer.render_stream_lines(
            &mut state,
            &stream,
            MarkdownRenderOptions {
                width: 80,
                block_spacing: true,
            },
            &theme,
            &mut highlighter,
        );
        let compact = renderer.render_stream_lines(
            &mut state,
            &stream,
            MarkdownRenderOptions {
                width: 80,
                block_spacing: false,
            },
            &theme,
            &mut highlighter,
        );
        assert_ne!(spaced, compact);

        let mut changed = theme.clone();
        changed.markdown.heading = Rgba::new(1, 2, 3, 255);
        let recolored = renderer.render_stream_lines(
            &mut state,
            &stream,
            MarkdownRenderOptions {
                width: 80,
                block_spacing: false,
            },
            &changed,
            &mut highlighter,
        );
        assert_ne!(compact, recolored);
    }
}