plannotator-tui 0.15.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! Rendering through the real pipeline: parse, render, align, decorate, wrap, draw colours.

#![allow(clippy::expect_used, clippy::indexing_slicing, reason = "tests assert by panicking")]

use ratatui::style::{Color, Modifier};

use super::code::BAR;
use super::*;
use crate::highlight::Role;

#[test]
fn rendered_text_strips_markup_and_joins_blocks_without_separator() {
    let doc = Document::parse("Ship the **login page**\nby Friday.\n\nNext para.\n".to_owned());
    let layout = DocLayout::build(&doc, 80);
    let whole = 0..doc.source.len();
    assert_eq!(layout.rendered_in_range(&doc.source, &whole), "Ship the login page by Friday.Next para.");
    let bold = doc.source.find("**login").expect("present");
    let bold_range = bold..bold + "**login page**".len();
    assert_eq!(layout.rendered_in_range(&doc.source, &bold_range), "login page");
}

#[test]
fn every_cell_of_a_long_table_maps_to_its_source() {
    let doc = Document::parse(
        "| Item | Status | Next step |\n|---|---|---|\n\
| A. Four changes from 3/7 (release checklist handoff, the 3/4 planning call, the 3/6 search demo, \
the 3/4-3/7 progress review and rollout advice) | Scope partly confirmed; never added to the \
draft | 1.0d design + 1.0d build + 1.0d review + 2.5d rollout = **5.5d** |\n\
| B. 3/14 cache troubleshooting and the `api.example.com` proxy | Owner and priority not set | Your call |\n\
| C. 3/19-20 export access in the admin panel (read and write) | Agreed at 1.0d, done once it's \
tested; it's live, but QA hasn't finished acceptance testing | Close it now, or wait for the next sprint |\n\
| D. 3/21 lookup and report tools in the CLI; 3/23 plugin consolidation | Marked \"not confirmed\" | Your call |\n"
            .to_owned(),
    );
    let layout = DocLayout::build(&doc, 100);
    let table = layout.blocks.iter().find(|b| b.kind == BlockKind::Table).expect("a table");
    let mut cells = 0;
    for row in &table.rows {
        let text: String = row.line.spans.iter().map(|s| s.content.as_ref()).collect();
        for (ch, offset) in text.chars().zip(&row.cells) {
            if ch.is_alphanumeric() {
                let source = offset.and_then(|o| doc.source.get(o..));
                assert!(source.is_some_and(|s| s.starts_with(ch)), "{ch:?} is unmapped in {text:?}");
                cells += 1;
            }
        }
    }
    assert!(cells > 300, "only {cells} cells checked");
}

fn rows(layout: &DocLayout, block: usize) -> Vec<String> {
    layout.blocks[block].rows.iter().map(|r| r.line.to_string()).collect()
}

/// Every document row as drawn, gap rows empty.
fn screen(layout: &DocLayout) -> Vec<String> {
    (0..layout.total_rows).map(|r| layout.row(r).map_or(String::new(), |row| row.line.to_string())).collect()
}

/// A list's items read as one list, each its own block, tight or loose: the whole list
/// draws no row between items either. A list written `1.` on every item still counts up.
#[test]
fn items_of_a_list_sit_row_after_row_and_keep_their_numbers() {
    let doc = Document::parse_into("1. a\n1. b\n1. c\n\n- d\n\n- e\n".to_owned(), crate::doc::Blocks::SPLIT);
    let layout = DocLayout::build(&doc, 40);
    assert_eq!(layout.blocks.len(), 5);
    assert_eq!(screen(&layout), ["1. a", "2. b", "3. c", "", "∙ d", "∙ e"]);
}

/// Every document row as drawn, colours included, with the source byte of each cell; a gap
/// row is an empty line with no cells.
fn drawn(layout: &DocLayout) -> Vec<(Line<'static>, Vec<Option<usize>>)> {
    (0..layout.total_rows)
        .map(|r| match layout.row(r) {
            Some(row) => (layout.drawn_line(r).expect("a row").into_owned(), row.cells.clone()),
            None => (Line::default(), Vec::new()),
        })
        .collect()
}

/// Cut into items by default, lists and definition lists draw exactly as they do whole: the
/// same rows, gap rows included, the same styles, the same source under every cell.
#[test]
fn split_lists_and_definition_lists_draw_row_for_row_as_whole_ones() {
    let documents = [
        "Intro\n\n- a\n- b\n- c\n\nAfter\n",
        "Intro\n\n- a\n\n- b\n\n- c\n\nAfter\n",
        "- a first item long enough to wrap\n\n  and its second paragraph\n- b\n\n- c\n",
        "- a\n  ```rs\n  let x = 1;\n  ```\n- b\n",
        "- a\n\n  ```\n  x\n  ```\n\n- b\n\nAfter\n",
        "- a\n- b\n  ```rust\n  let n = 42;\n  ```\n\nAfter\n",
        "- a\n\n  | x | y |\n  |---|---|\n  | 1 | 2 |\n- b\n",
        "- a\n\n  <div>block</div>\n\n- b\n",
        "- a\n  - a1\n    - a11\n  - a2\n- b\n  1. b1\n  2. b2\n",
        "1. one\n1. two\n1. three\n\nText.\n\n9. nine\n10. ten\n11. eleven, wrapped at a narrow width\n",
        "- [ ] open\n- [x] done\n  - [ ] sub\n\n1. [x] first\n2. [ ] second\n",
        "Intro\n\nTerm\n: def one\n\nTerm2\n: def two, long enough to wrap at a narrow width\n: another\n",
        "> - a\n> - b\n>\n> para\n\n- after the quote\n- and more\n",
        // A block right under an item's first line, in a loose list and in a tight one: drawn
        // alone, the item is a list of its own, which must be as loose as the list it is in.
        "- a\n  > q\n  > r\n\n- b\n",
        "- a\n  | x | y |\n  |---|---|\n  | 1 | 2 |\n\n- b\n",
        "- a\n  <div>block</div>\n\n- b\n",
        "- a\n  ```\n  x\n  ```\n\n- b\n",
        "- a\n  - n1\n  - n2\n\n- b\n",
        "- a\n  > q\n  > r\n- b\n",
        "- a\n  | x | y |\n  |---|---|\n  | 1 | 2 |\n- b\n",
        "- a\n  <div>block</div>\n- b\n",
        "- a\n  ```\n  x\n  ```\n- b\n",
        "- a\n  - n1\n  - n2\n- b\n",
        // Loose for another item's sake, the last item, other markers, ordered lists.
        "- a\n  > q\n- b\n\n  second paragraph of b\n",
        "- a\n\n- b\n  > q\n",
        "* a\n  > q\n\n* b\n\nText.\n\n+ a\n  > q\n\n+ b\n",
        "1. a\n   > q\n\n2. b\n\nText.\n\n3) a\n   ```\n   x\n   ```\n\n4) b\n",
        "1. a\n1. b\n   > q\n\n1. c\n",
    ];
    for source in documents {
        let split = Document::parse_into(source.to_owned(), crate::doc::Blocks::default());
        let whole = Document::parse_into(source.to_owned(), crate::doc::Blocks::WHOLE);
        assert!(split.blocks.len() > whole.blocks.len(), "{source:?} is not split");
        for width in [40, 12] {
            let (split, whole) = (DocLayout::build(&split, width), DocLayout::build(&whole, width));
            assert_eq!(drawn(&split), drawn(&whole), "{source:?} at {width} columns");
        }
    }
}

const TABLE: &str = "Before.\n\n| Item | Status of the work |\n|---|---|\n| a | done and shipped |\n\
| | |\n| b long name | waiting on review from the team |\n\nAfter.\n";

fn table_rows() -> crate::doc::Blocks {
    crate::doc::Blocks { tables: crate::doc::Cut::Split, ..crate::doc::Blocks::default() }
}

/// Split into rows, a table draws as it does whole, at any width and after a reflow, styles
/// and source cells included: the columns are fitted to the whole table and the header
/// keeps its rules.
#[test]
fn a_table_cut_into_rows_draws_as_it_does_whole() {
    let split = Document::parse_into(TABLE.to_owned(), table_rows());
    let whole = Document::parse_into(TABLE.to_owned(), crate::doc::Blocks::default());
    assert_eq!(split.blocks.len(), 6);
    let mut reflowed = DocLayout::build(&split, 80);
    for width in [80, 30, 20, 8] {
        let (cut, one) = (DocLayout::build(&split, width), DocLayout::build(&whole, width));
        assert_eq!(drawn(&cut), drawn(&one), "at {width}");
        reflowed.reflow(width);
        assert_eq!(drawn(&reflowed), drawn(&one), "reflowed to {width}");
        let all = 0..TABLE.len();
        assert_eq!(cut.rendered_in_range(&split.source, &all), one.rendered_in_range(&whole.source, &all));
    }
    let layout = DocLayout::build(&split, 30);
    let header = rows(&layout, 1);
    assert!(header.first().is_some_and(|r| r.starts_with('┌')), "{header:?}");
    assert!(header.last().is_some_and(|r| r.starts_with('├')), "{header:?}");
    let last = rows(&layout, 4);
    assert!(last.last().is_some_and(|r| r.starts_with('└')), "{last:?}");
    let wide = DocLayout::build(&split, 80);
    assert_eq!(rows(&wide, 3).len(), 1, "a row of empty cells still has its row");
}

/// Rows in odd shapes still draw as the whole table: an empty last cell, a missing cell,
/// an escaped pipe, wide characters, outer pipes left out.
#[test]
fn odd_tables_cut_into_rows_draw_as_they_do_whole() {
    for source in [
        "| a | b |\n|---|---|\n| x | y |\n| c | |\n",
        "| a | b | c |\n|---|---|---|\n| x |\n| 1 | 2 | 3 | 4 |\n",
        "| a | b |\n|---|---|\n| `x \\| y` | 漢字の長いセル |\nno | pipes\n",
        "a | b\n---|---\nx | y\nz | w\n",
    ] {
        let split = Document::parse_into(source.to_owned(), table_rows());
        let whole = Document::parse_into(source.to_owned(), crate::doc::Blocks::default());
        assert!(split.blocks.len() > 1, "{source:?}");
        for width in 8..=40 {
            let (cut, one) = (DocLayout::build(&split, width), DocLayout::build(&whole, width));
            assert_eq!(drawn(&cut), drawn(&one), "{source:?} at {width}");
        }
    }
}

/// The first screens never stop inside a table split into rows: its rows are laid out together.
#[test]
fn a_long_table_cut_into_rows_lays_out_as_one_across_the_first_screens() {
    let mut source = "| n | word |\n|---|---|\n".to_owned();
    for n in 0..FIRST_ROWS + 100 {
        source.push_str(&["| ", &n.to_string(), " | ", &"w".repeat(n % 7 + 1), " |\n"].concat());
    }
    let split = Document::parse_into(source.clone(), table_rows());
    let mut started = DocLayout::start(&split, 12);
    started.finish();
    assert_eq!(started.blocks.len(), FIRST_ROWS + 101);
    let whole = DocLayout::build(&Document::parse_into(source, crate::doc::Blocks::default()), 12);
    assert_eq!(drawn(&started), drawn(&whole));
}

const RUST: &str = "Intro.\n\n```rust\nfn main() {\n    let n = 42; // answer\n}\n```\n\nAfter.\n";

#[test]
fn a_code_block_drops_its_fences_and_draws_a_bar_and_its_language() {
    let doc = Document::parse(RUST.to_owned());
    let layout = DocLayout::build(&doc, 40);
    assert_eq!(
        rows(&layout, 1),
        [
            format!("{BAR} fn main() {{{}rust", " ".repeat(40 - 2 - 11 - 4)),
            format!("{BAR}     let n = 42; // answer"),
            format!("{BAR} }}"),
        ]
    );
    // The bar and the label are drawn, not written: no cell of theirs has a source byte, and
    // every code character maps to its own.
    for row in &layout.blocks[1].rows {
        assert_eq!(row.cells[..2], [None, None]);
        let text: String = row.line.to_string();
        for (ch, cell) in text.chars().zip(&row.cells).skip(2) {
            if let Some(offset) = cell {
                assert!(doc.source[*offset..].starts_with(ch), "{ch:?} in {text:?}");
            }
        }
    }
    let label = layout.blocks[1].rows[0].cells.iter().rev().take(4);
    assert!(label.into_iter().all(Option::is_none));
}

#[test]
fn the_text_under_a_whole_code_block_is_its_code_not_its_fences() {
    let doc = Document::parse(RUST.to_owned());
    let layout = DocLayout::build(&doc, 40);
    let block = doc.blocks[1].range.clone();
    let code = "fn main() {\n    let n = 42; // answer\n}";
    assert_eq!(layout.rendered_in_range(&doc.source, &block), code);
    let cells = layout.blocks[1].rows.iter().flat_map(|r| r.cells.iter().flatten());
    let (lo, hi) = cells.fold((usize::MAX, 0), |(lo, hi), &o| (lo.min(o), hi.max(o)));
    assert_eq!(&doc.source[lo..=hi], code);
}

#[test]
fn a_known_language_is_coloured_only_when_drawn_and_names_keep_the_terminal_colour() {
    let doc = Document::parse(RUST.to_owned());
    let layout = DocLayout::build(&doc, 40);
    let first = layout.blocks[1].first_row;
    let colour = |row: usize, text: &str| {
        let line = layout.drawn_line(row).expect("a row");
        line.spans.iter().find(|s| s.content.contains(text)).map(|s| s.style.patch(Style::default()))
    };
    assert_eq!(colour(first, "fn").and_then(|s| s.fg), Role::Keyword.style().fg);
    assert_eq!(colour(first, "main").and_then(|s| s.fg), Role::Function.style().fg);
    assert_eq!(colour(first + 1, "42").and_then(|s| s.fg), Role::Number.style().fg);
    let comment = colour(first + 1, "answer").expect("comment");
    assert_eq!(comment.fg, Role::Comment.style().fg);
    assert!(comment.add_modifier.contains(Modifier::ITALIC));
    assert_eq!(colour(first + 1, "n").and_then(|s| s.fg), Some(Color::Reset));
    // The bar keeps its own colour.
    assert_eq!(colour(first, &BAR.to_string()).and_then(|s| s.fg), Some(palette().code_comment));
    // Laid-out rows never carry the colours, so a reflow cannot lose or repeat them.
    assert!(layout.blocks[1].rows[0].line.spans.iter().all(|s| s.style.fg != Role::Keyword.style().fg));
}

#[test]
fn an_unknown_language_keeps_the_one_code_colour_and_its_label() {
    let doc = Document::parse("```klingon\nqapla' batlh\n```\n".to_owned());
    let layout = DocLayout::build(&doc, 40);
    assert_eq!(rows(&layout, 0), [format!("{BAR} qapla' batlh{}klingon", " ".repeat(40 - 2 - 12 - 7))]);
    let line = layout.drawn_line(0).expect("a row");
    assert_eq!(*line, layout.blocks[0].rows[0].line, "drawn as laid out");
    assert_eq!(line.style.fg, Some(palette().code));
}

#[test]
fn a_narrow_column_drops_the_label_and_wraps_long_code_beside_the_bar() {
    let doc = Document::parse(RUST.to_owned());
    let layout = DocLayout::build(&doc, 14);
    assert_eq!(
        rows(&layout, 1),
        [
            format!("{BAR} fn main() {{"),
            format!("{BAR}     let n ="),
            format!("{BAR} 42; // answe"),
            format!("{BAR} r"),
            format!("{BAR} }}"),
        ]
    );
    assert!(layout.blocks.iter().flat_map(|b| &b.rows).all(|r| r.cells.len() <= 14));
    // Nothing is lost: the rows still carry every byte of the code.
    let block = doc.blocks[1].range.clone();
    assert_eq!(layout.rendered_in_range(&doc.source, &block), "fn main() {\n    let n = 42; // answer\n}");
    // Colour still follows the source, on the continuation rows too.
    let first = layout.blocks[1].first_row;
    let line = layout.drawn_line(first + 2).expect("a row");
    let number = line.spans.iter().find(|s| s.content.contains("42")).expect("42");
    assert_eq!(number.style.fg, Role::Number.style().fg);
}

#[test]
fn a_title_has_a_rule_under_it_and_sections_get_room_above() {
    let doc = Document::parse("# Title\n\nText.\n\n## Section\n\n### Sub\n".to_owned());
    let layout = DocLayout::build(&doc, 20);
    assert_eq!(rows(&layout, 0), ["Title".to_owned(), TITLE_RULE.to_string().repeat(20)]);
    assert_eq!(layout.blocks[0].rows[1].cells, vec![None; 20], "the rule has no source");
    // Title, its rule, a gap, the text; two blank rows before a section; one before a sub-heading.
    let starts: Vec<usize> = layout.blocks.iter().map(|b| b.first_row).collect();
    assert_eq!(starts, [0, 3, 6, 8]);
    let row = &layout.blocks[0].rows[0].line;
    let title = row.style.patch(row.spans[0].style);
    assert!(title.add_modifier.contains(Modifier::BOLD) && title.fg == Some(palette().heading));
}

#[test]
fn a_definition_list_indents_its_definitions_under_their_terms() {
    for blocks in [crate::doc::Blocks::default(), crate::doc::Blocks::WHOLE] {
        let doc = Document::parse_into(
            "Term\n: A definition long enough to wrap onto a second row.\n\nOther\n: Short.\n".to_owned(),
            blocks,
        );
        assert_eq!(doc.blocks[0].kind, BlockKind::DefinitionList);
        let layout = DocLayout::build(&doc, 30);
        assert_eq!(
            screen(&layout),
            ["Term", "    A definition long enough", "    to wrap onto a second row.", "Other", "    Short."]
        );
        for row in 1..3 {
            assert_eq!(layout.row(row).expect("a row").cells[..4], [None; 4], "the indent has no source");
        }
        let whole = 0..doc.source.len();
        assert_eq!(
            layout.rendered_in_range(&doc.source, &whole),
            "Term A definition long enough to wrap onto a second row. Other Short."
        );
        let term = &layout.row(0).expect("the term").line;
        assert!(term.spans.iter().all(|s| term.style.patch(s.style).add_modifier.contains(Modifier::BOLD)));
    }
}

#[test]
fn the_bar_and_the_title_rule_are_one_column_wide_even_in_cjk_terminals() {
    use unicode_width::UnicodeWidthChar as _;
    for glyph in [BAR, TITLE_RULE] {
        assert_eq!(glyph.width_cjk(), Some(1), "{glyph:?}");
    }
}

fn all_rows(layout: &DocLayout) -> Vec<(usize, String, Vec<Option<usize>>)> {
    layout
        .blocks
        .iter()
        .flat_map(|b| b.rows.iter().map(|r| (b.first_row, r.line.to_string(), r.cells.clone())))
        .collect()
}

/// A long document opens on its first screens only, and the rest, laid out in the
/// background (here for a width the first frame changed), is exactly a full build: section
/// gaps, title rules and code bars included.
#[test]
fn a_long_document_lays_out_its_first_screens_first_and_the_rest_matches_a_full_build() {
    let section = "# Part\n\n## Step\n\nSome **bold** words that wrap at a narrow width, and more.\n\n\
- one\n- two\n\n| a | b |\n|---|---|\n| x | y |\n\n```\ncode\n```\n\n```rust\nlet n = 42;\n```\n\n\
Term\n: Its definition.\n\n---\n\n";
    let doc = Document::parse(section.repeat(400));
    let mut lazy = DocLayout::start(&doc, 70);
    assert!(!lazy.is_complete());
    assert!(lazy.blocks.len() < doc.blocks.len() / 10, "laid out {} blocks up front", lazy.blocks.len());
    assert!(lazy.total_rows >= FIRST_ROWS);
    lazy.reflow(50);
    lazy.finish();
    assert!(lazy.is_complete());
    let full = DocLayout::build(&doc, 50);
    assert_eq!(lazy.blocks.len(), doc.blocks.len());
    assert_eq!(lazy.total_rows, full.total_rows);
    assert!(all_rows(&lazy) == all_rows(&full), "rows differ from a full build");
}

/// Polling says when it added the background blocks, once, so the event loop redraws for
/// them without a key.
#[test]
fn polling_reports_the_background_blocks_once() {
    let doc = Document::parse("## Step\n\nSome words.\n\n".repeat(2000));
    let mut lazy = DocLayout::start(&doc, 70);
    let started = std::time::Instant::now();
    while !lazy.poll() {
        assert!(started.elapsed() < std::time::Duration::from_secs(20), "the layout never finished");
        std::thread::sleep(std::time::Duration::from_millis(5));
    }
    assert!(lazy.is_complete());
    assert_eq!(lazy.blocks.len(), doc.blocks.len());
    assert!(!lazy.poll());
}

/// Code laid out on a background thread is coloured when drawn, like code on the first screen.
#[test]
fn code_laid_out_in_the_background_is_coloured_when_drawn() {
    let section =
        "Some words before the code, enough to make a long document.\n\n```rust\nlet n = 42;\n```\n\n";
    let doc = Document::parse(section.repeat(1000));
    let mut layout = DocLayout::start(&doc, 60);
    let first = layout.blocks.len();
    layout.finish();
    let last = layout.blocks.len() - 1;
    assert!(last > first, "the last block came from the background");
    let row = layout.blocks[last].first_row;
    let line = layout.drawn_line(row).expect("the code row");
    let keyword = line.spans.iter().find(|span| span.content == "let").expect("let is a span of its own");
    assert_eq!(keyword.style.fg, Role::Keyword.style().fg);
}

const NESTED: &str = "- Step one:\n\n  ```rust\n  let n = 42; // answer\n  ```\n- Step two\n\n\
> Quoted:\n>\n> ```klingon\n> qapla' batlh\n> ```\n";

/// Code in a list or a quote looks like top-level code: no fences, a bar inside the bullet's
/// indent or the quote bar, colours when drawn, and its cells still map to the code.
#[test]
fn code_inside_a_list_or_quote_gets_the_bar_and_colours_without_fences() {
    for blocks in [crate::doc::Blocks::default(), crate::doc::Blocks::WHOLE] {
        let doc = Document::parse_into(NESTED.to_owned(), blocks);
        let layout = DocLayout::build(&doc, 40);
        assert_eq!(
            screen(&layout),
            [
                "∙ Step one:".to_owned(),
                String::new(),
                format!("  {BAR} let n = 42; // answer"),
                String::new(),
                "∙ Step two".to_owned(),
                String::new(),
                "╎ Quoted:".to_owned(),
                "╎".to_owned(),
                format!("╎ {BAR} qapla' batlh"),
            ]
        );

        let code_row = 2;
        let line = layout.drawn_line(code_row).expect("the code row");
        let style_of = |text: &str| line.spans.iter().find(|s| s.content.contains(text)).map(|s| s.style.fg);
        assert_eq!(style_of("let"), Some(Role::Keyword.style().fg));
        assert_eq!(style_of("42"), Some(Role::Number.style().fg));
        assert_eq!(style_of(&BAR.to_string()), Some(Some(palette().code_comment)));
        // The bullet's line is not code and keeps its look.
        let bullet = layout.drawn_line(0).expect("bullet row");
        assert_eq!(bullet.as_ref(), &layout.row(0).expect("bullet row").line);
        // A language without a grammar keeps the one code colour.
        let quote_row = 8;
        assert_eq!(
            layout.drawn_line(quote_row).expect("quote row").as_ref(),
            &layout.row(quote_row).expect("row").line
        );

        // Selecting the whole code line gives the code, not the bar or the fences.
        let start = doc.source.find("let n").expect("code");
        let end = doc.source.find("answer").expect("comment") + "answer".len();
        assert_eq!(layout.rendered_in_range(&doc.source, &(start..end)), "let n = 42; // answer");
        let row = layout.row(code_row).expect("the code row");
        let first = row.cells.iter().flatten().next().copied();
        assert_eq!(first, Some(start), "the bar maps to nothing, the code to its bytes");
    }
}