diffler 0.10.1

Terminal code review for AI coding agents
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
//! Minimal `CommonMark` rendering for comment and reply bodies. Parses to
//! theme-independent styled runs so the pure app layer stays free of ratatui
//! and the theme; [`crate::ui`] maps the flags to concrete styles at draw time.
//! Raw HTML is dropped rather than shown, so a stray tag in an agent reply does
//! not leak into the card.

use diffler_core::highlight::{Highlighter, StyledRange};
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
use unicode_width::UnicodeWidthStr;

/// A styled text run with no line breaks. Flags compose (bold + italic), so
/// nested emphasis survives; `code`/`link`/`muted` additionally recolor. `fg`
/// carries a fenced code block's syntax color, overriding the flag styling.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)] // flags compose; an enum cannot
pub struct MdSpan {
    pub text: String,
    pub bold: bool,
    pub italic: bool,
    pub code: bool,
    pub strike: bool,
    pub link: bool,
    pub muted: bool,
    pub fg: Option<(u8, u8, u8)>,
}

impl MdSpan {
    fn plain(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            ..Self::default()
        }
    }

    fn width(&self) -> usize {
        self.text.width()
    }
}

#[derive(Default)]
struct Flags {
    bold: usize,
    italic: usize,
    strike: usize,
    link: bool,
    heading: bool,
}

/// Parse markdown into logical lines of styled runs (unwrapped). Line breaks,
/// block boundaries, list items, and code-block lines each start a new logical
/// line; a comment's own newlines are kept (GitHub renders them).
#[allow(clippy::too_many_lines)] // one arm per markdown event; a flat match reads best
pub fn parse(src: &str, highlighter: Option<&Highlighter>) -> Vec<Vec<MdSpan>> {
    let mut opts = Options::empty();
    opts.insert(Options::ENABLE_STRIKETHROUGH);
    opts.insert(Options::ENABLE_TASKLISTS);
    let mut lines: Vec<Vec<MdSpan>> = Vec::new();
    let mut line: Vec<MdSpan> = Vec::new();
    let mut flags = Flags::default();
    let mut list_depth: usize = 0;
    let mut code_block: Option<String> = None;
    let mut code_lang: Option<String> = None;
    let mut link_url: Option<String> = None;
    // a list item's own paragraph must not flush the bullet onto its own line
    let mut item_paragraph = false;

    let flush = |line: &mut Vec<MdSpan>, lines: &mut Vec<Vec<MdSpan>>| {
        if !line.is_empty() {
            lines.push(std::mem::take(line));
        }
    };

    for event in Parser::new_ext(src, opts) {
        match event {
            Event::Start(Tag::Strong) => flags.bold += 1,
            Event::End(TagEnd::Strong) => flags.bold = flags.bold.saturating_sub(1),
            Event::Start(Tag::Emphasis) => flags.italic += 1,
            Event::End(TagEnd::Emphasis) => flags.italic = flags.italic.saturating_sub(1),
            Event::Start(Tag::Strikethrough) => flags.strike += 1,
            Event::End(TagEnd::Strikethrough) => flags.strike = flags.strike.saturating_sub(1),
            Event::Start(Tag::Heading { .. }) => {
                flush(&mut line, &mut lines);
                flags.heading = true;
            }
            Event::End(TagEnd::Heading(_)) => {
                flush(&mut line, &mut lines);
                flags.heading = false;
            }
            Event::Start(Tag::List(_)) => list_depth += 1,
            Event::End(TagEnd::List(_)) => list_depth = list_depth.saturating_sub(1),
            Event::Start(Tag::Item) => {
                flush(&mut line, &mut lines);
                let indent = "  ".repeat(list_depth.saturating_sub(1));
                line.push(MdSpan {
                    text: format!("{indent}• "),
                    muted: true,
                    ..MdSpan::default()
                });
                item_paragraph = true;
            }
            Event::End(TagEnd::Item) => {
                item_paragraph = false;
                flush(&mut line, &mut lines);
            }
            Event::Start(Tag::Paragraph) => {
                if item_paragraph {
                    item_paragraph = false;
                } else {
                    flush(&mut line, &mut lines);
                }
            }
            Event::Start(Tag::CodeBlock(kind)) => {
                flush(&mut line, &mut lines);
                code_block = Some(String::new());
                code_lang = match kind {
                    CodeBlockKind::Fenced(lang) if !lang.is_empty() => Some(lang.into_string()),
                    _ => None,
                };
            }
            Event::End(TagEnd::CodeBlock) => {
                if let Some(buf) = code_block.take() {
                    let body = buf.trim_end_matches('\n');
                    let colors = code_lang
                        .take()
                        .zip(highlighter)
                        .map(|(lang, hl)| hl.highlight_lang(&lang, body));
                    for (i, text) in body.split('\n').enumerate() {
                        let ranges = colors
                            .as_ref()
                            .and_then(|per_line| per_line.get(i))
                            .map_or(&[][..], Vec::as_slice);
                        lines.push(code_line_spans(text, ranges));
                    }
                }
            }
            Event::Start(Tag::Link { dest_url, .. }) => {
                flags.link = true;
                link_url = Some(dest_url.into_string());
            }
            Event::End(TagEnd::Link) => {
                flags.link = false;
                if let Some(url) = link_url.take() {
                    let shown: String = line.iter().map(|s| s.text.as_str()).collect();
                    if !url.is_empty() && !shown.contains(&url) {
                        line.push(MdSpan {
                            text: format!(" ({url})"),
                            muted: true,
                            ..MdSpan::default()
                        });
                    }
                }
            }
            Event::Text(text) => {
                if let Some(buf) = code_block.as_mut() {
                    buf.push_str(&text);
                } else {
                    line.push(MdSpan {
                        text: text.into_string(),
                        bold: flags.bold > 0 || flags.heading,
                        italic: flags.italic > 0,
                        strike: flags.strike > 0,
                        link: flags.link,
                        ..MdSpan::default()
                    });
                }
            }
            Event::Code(text) => line.push(MdSpan {
                text: text.into_string(),
                code: true,
                bold: flags.bold > 0 || flags.heading,
                italic: flags.italic > 0,
                link: flags.link,
                ..MdSpan::default()
            }),
            Event::TaskListMarker(checked) => line.push(MdSpan {
                text: if checked { "[x] " } else { "[ ] " }.to_owned(),
                muted: true,
                ..MdSpan::default()
            }),
            // a review comment's own line breaks are meaningful (GitHub renders
            // them), so a soft break starts a new line rather than a space
            Event::End(TagEnd::Paragraph) | Event::SoftBreak | Event::HardBreak => {
                flush(&mut line, &mut lines);
            }
            _ => {}
        }
    }
    flush(&mut line, &mut lines);
    lines
}

/// Word-wrap the runs of one logical line to `first`/`rest` column budgets,
/// keeping every run's style. Breaks at spaces; a single token wider than the
/// budget is hard-split at character boundaries. Always yields at least one
/// (possibly empty) line.
pub fn wrap(runs: &[MdSpan], first: usize, rest: usize) -> Vec<Vec<MdSpan>> {
    let words = split_words(runs);
    let mut out: Vec<Vec<MdSpan>> = Vec::new();
    let mut line: Vec<MdSpan> = Vec::new();
    let mut used = 0usize;
    let mut budget = first;
    let mut flush = |line: &mut Vec<MdSpan>, used: &mut usize, budget: &mut usize| {
        out.push(std::mem::take(line));
        *used = 0;
        *budget = rest;
    };
    for word in words {
        let wwidth: usize = word.iter().map(MdSpan::width).sum();
        let sep = usize::from(!line.is_empty());
        if used + sep + wwidth > budget && !line.is_empty() {
            flush(&mut line, &mut used, &mut budget);
        }
        if !line.is_empty() {
            line.push(MdSpan::plain(" "));
            used += 1;
        }
        if wwidth <= budget {
            line.extend(word);
            used += wwidth;
            continue;
        }
        let mut pending: Vec<(char, &MdSpan)> = Vec::new();
        let mut w = 0usize;
        let mut avail = budget
            .saturating_sub(used)
            .max(usize::from(line.is_empty()));
        for (c, span) in flatten(&word) {
            let cw = c.to_string().width();
            if w + cw > avail && !pending.is_empty() {
                line.extend(collect_spans(&pending));
                pending.clear();
                used += w;
                flush(&mut line, &mut used, &mut budget);
                w = 0;
                avail = budget;
            }
            pending.push((c, span));
            w += cw;
        }
        if !pending.is_empty() {
            line.extend(collect_spans(&pending));
            used += w;
        }
    }
    out.push(line);
    out.into_iter().map(coalesce).collect()
}

/// Merge adjacent same-style runs so a wrapped line is one run per style span,
/// not one per word.
fn coalesce(line: Vec<MdSpan>) -> Vec<MdSpan> {
    let mut merged: Vec<MdSpan> = Vec::new();
    for span in line {
        match merged.last_mut() {
            Some(last) if same_style(last, &span) => last.text.push_str(&span.text),
            _ => merged.push(span),
        }
    }
    merged
}

fn flatten(word: &[MdSpan]) -> Vec<(char, &MdSpan)> {
    word.iter()
        .flat_map(|span| span.text.chars().map(move |c| (c, span)))
        .collect()
}

/// Split runs into words (contiguous non-space styled pieces); every space,
/// including run-internal ones, is a break opportunity. Adjacent code runs join
/// into one word so a highlighted code line's whitespace (indentation, the gaps
/// between colored tokens) survives the wrap intact.
fn split_words(runs: &[MdSpan]) -> Vec<Vec<MdSpan>> {
    let mut words: Vec<Vec<MdSpan>> = Vec::new();
    let mut cur: Vec<MdSpan> = Vec::new();
    let mut cur_code = false;
    for run in runs {
        if run.code {
            if !cur.is_empty() && !cur_code {
                words.push(std::mem::take(&mut cur));
            }
            cur.push(run.clone());
            cur_code = true;
            continue;
        }
        if cur_code && !cur.is_empty() {
            words.push(std::mem::take(&mut cur));
        }
        cur_code = false;
        for (i, part) in run.text.split(' ').enumerate() {
            if i > 0 && !cur.is_empty() {
                words.push(std::mem::take(&mut cur));
            }
            if !part.is_empty() {
                cur.push(MdSpan {
                    text: part.to_owned(),
                    ..run.clone()
                });
            }
        }
    }
    if !cur.is_empty() {
        words.push(cur);
    }
    words
}

fn collect_spans(chars: &[(char, &MdSpan)]) -> Vec<MdSpan> {
    let mut out: Vec<MdSpan> = Vec::new();
    for (c, span) in chars {
        match out.last_mut() {
            Some(last) if same_style(last, span) => last.text.push(*c),
            _ => out.push(MdSpan {
                text: c.to_string(),
                ..(*span).clone()
            }),
        }
    }
    out
}

fn same_style(a: &MdSpan, b: &MdSpan) -> bool {
    a.bold == b.bold
        && a.italic == b.italic
        && a.code == b.code
        && a.strike == b.strike
        && a.link == b.link
        && a.muted == b.muted
        && a.fg == b.fg
}

/// One code-block line as `code` runs, carrying each highlighted token's color
/// and leaving the gaps between them uncolored.
fn code_line_spans(text: &str, ranges: &[StyledRange]) -> Vec<MdSpan> {
    let mut sorted: Vec<&StyledRange> = ranges.iter().collect();
    sorted.sort_by_key(|r| r.range.start);
    let mut spans: Vec<MdSpan> = Vec::new();
    let mut pos = 0;
    for r in sorted {
        let start = r.range.start.max(pos).min(text.len());
        let end = r.range.end.min(text.len());
        if start >= end {
            continue;
        }
        if let Some(gap) = text.get(pos..start).filter(|g| !g.is_empty()) {
            spans.push(code_span(gap, None));
        }
        if let Some(seg) = text.get(start..end) {
            spans.push(code_span(seg, Some(r.fg)));
        }
        pos = end;
    }
    if let Some(rest) = text.get(pos..).filter(|g| !g.is_empty()) {
        spans.push(code_span(rest, None));
    }
    if spans.is_empty() {
        spans.push(code_span(text, None));
    }
    spans
}

fn code_span(text: &str, fg: Option<(u8, u8, u8)>) -> MdSpan {
    MdSpan {
        text: text.to_owned(),
        code: true,
        fg,
        ..MdSpan::default()
    }
}

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

    fn parse(src: &str) -> Vec<Vec<MdSpan>> {
        super::parse(src, None)
    }

    fn text(lines: &[Vec<MdSpan>]) -> String {
        lines
            .iter()
            .map(|line| line.iter().map(|s| s.text.as_str()).collect::<String>())
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn inline_styles_tag_their_runs() {
        let lines = parse("use **bold** and `code` and *em* and ~~gone~~");
        let runs = &lines[0];
        let find = |t: &str| runs.iter().find(|s| s.text == t).cloned().unwrap();
        assert!(find("bold").bold);
        assert!(find("code").code);
        assert!(find("em").italic);
        assert!(find("gone").strike);
        assert!(!find("use ").bold);
    }

    #[test]
    fn soft_break_starts_a_new_line() {
        assert_eq!(text(&parse("first\nsecond")), "first\nsecond");
    }

    #[test]
    fn blank_line_separates_paragraphs() {
        assert_eq!(text(&parse("one\n\ntwo")), "one\ntwo");
    }

    #[test]
    fn code_block_lines_are_each_a_run() {
        let lines = parse("```\nlet x = 1;\nlet y = 2;\n```");
        assert_eq!(text(&lines), "let x = 1;\nlet y = 2;");
        assert!(lines.iter().all(|l| l.iter().all(|s| s.code)));
    }

    #[test]
    fn fenced_code_block_gets_syntax_colors() {
        let hl = Highlighter::default();
        let lines = super::parse("```rust\nfn f() {}\n```", Some(&hl));
        assert_eq!(text(&lines), "fn f() {}");
        assert!(lines[0].iter().all(|s| s.code), "still code runs");
        let colors: std::collections::HashSet<_> = lines[0].iter().filter_map(|s| s.fg).collect();
        assert!(
            colors.len() > 1,
            "keyword and identifier differ: {colors:?}"
        );
        // whitespace and layout survive the wrap
        assert_eq!(wrap(&lines[0], 40, 40).len(), 1);
        assert_eq!(
            wrap(&lines[0], 40, 40)[0]
                .iter()
                .map(|s| s.text.as_str())
                .collect::<String>(),
            "fn f() {}"
        );
    }

    #[test]
    fn code_block_without_highlighter_stays_plain() {
        let lines = super::parse("```rust\nfn f() {}\n```", None);
        assert!(lines[0].iter().all(|s| s.code && s.fg.is_none()));
    }

    #[test]
    fn bullets_get_a_muted_marker() {
        let lines = parse("- one\n- two");
        assert_eq!(text(&lines), "• one\n• two");
        assert!(lines[0][0].muted);
    }

    #[test]
    fn loose_list_keeps_the_bullet_with_its_text() {
        // blank lines between items make pulldown wrap each item in a paragraph
        assert_eq!(text(&parse("- one\n\n- two")), "• one\n• two");
    }

    #[test]
    fn task_list_markers_render() {
        let lines = parse("- [ ] todo\n- [x] done");
        assert_eq!(text(&lines), "• [ ] todo\n• [x] done");
    }

    #[test]
    fn code_block_indentation_survives_the_wrap() {
        let logical = parse("```\n    return 1\n```");
        let wrapped: Vec<Vec<MdSpan>> =
            logical.iter().flat_map(|line| wrap(line, 40, 40)).collect();
        assert_eq!(text(&wrapped), "    return 1");
    }

    #[test]
    fn raw_html_is_dropped() {
        // an agent reply that ends with a stray tag must not leak it
        assert_eq!(text(&parse("keep this</body>")), "keep this");
    }

    #[test]
    fn link_shows_text_then_muted_url() {
        let lines = parse("[docs](https://example.invalid)");
        let joined = text(&lines);
        assert!(joined.contains("docs"));
        assert!(joined.contains("https://example.invalid"));
        assert!(lines[0].iter().find(|s| s.text == "docs").unwrap().link);
    }

    #[test]
    fn wrap_breaks_at_spaces_and_keeps_style() {
        let runs = vec![
            MdSpan::plain("alpha "),
            MdSpan {
                text: "beta".to_owned(),
                bold: true,
                ..MdSpan::default()
            },
            MdSpan::plain(" gamma"),
        ];
        let wrapped = wrap(&runs, 10, 10);
        assert!(wrapped.len() >= 2);
        let bold = wrapped.iter().flatten().find(|s| s.text == "beta").unwrap();
        assert!(bold.bold, "style survives the wrap");
    }

    #[test]
    fn wrap_hard_splits_an_overlong_token() {
        let runs = vec![MdSpan::plain("abcdefghijklmnopqrstuvwxyz")];
        let wrapped = wrap(&runs, 8, 8);
        assert!(wrapped.len() > 1);
        for line in &wrapped {
            let width: usize = line.iter().map(MdSpan::width).sum();
            assert!(width <= 8, "{line:?}");
        }
        let joined: String = wrapped.iter().flatten().map(|s| s.text.as_str()).collect();
        assert_eq!(joined, "abcdefghijklmnopqrstuvwxyz");
    }
}