bigtext 0.16.1

Free editor for huge text and log files (1-50+ GB): opens them without loading them into memory, 4 GB/s search, atomic save
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
//! Search → Go to symbol (Ctrl+R): the functions, classes and types defined in a code file,
//! found line by line on a worker (comments and strings are blanked first, using the
//! highlighter's state, so a `def` in a docstring is not one), filtered by a fuzzy match of the
//! name. At most [`SCAN_MAX`] of the file is read.

use std::sync::mpsc;
use std::sync::OnceLock;

use egui::{Key, Modifiers};
use regex_automata::meta::Regex;

use super::i18n::{tr, trf};
use super::BigTextApp;
use crate::view::highlight::{self, Kind, Lang};

/// How much of a file is read for its symbols.
pub const SCAN_MAX: u64 = 32 * 1024 * 1024;
const SHOWN: usize = 300;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SymKind {
    Function,
    Class,
    Type,
    Macro,
}

impl SymKind {
    fn badge(self) -> &'static str {
        match self {
            SymKind::Function => "ƒ",
            SymKind::Class => "C",
            SymKind::Type => "T",
            SymKind::Macro => "#",
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Symbol {
    pub name: String,
    pub kind: SymKind,
    /// 0-based line.
    pub line: u64,
    /// Indentation level (methods under their class).
    pub depth: u8,
}

struct Rule {
    re: Regex,
    kind: SymKind,
    /// Put before the name ("impl ").
    prefix: &'static str,
}

/// Words that look like a function name before `(` but are not one.
const NOT_NAMES: &[&str] = &[
    "if", "for", "foreach", "while", "switch", "catch", "return", "new", "delete", "throw",
    "sizeof", "else", "case", "do", "using", "typeof", "await", "elif", "with", "match",
    "function", "lock", "fixed", "when", "assert", "defined",
];

fn rules(lang: Lang) -> &'static [Rule] {
    fn build(list: &[(&str, SymKind, &'static str)]) -> Vec<Rule> {
        list.iter()
            .map(|&(p, kind, prefix)| Rule {
                re: Regex::new(p).expect("symbol pattern"),
                kind,
                prefix,
            })
            .collect()
    }
    use SymKind::*;
    const ID: &str = r"[A-Za-z_][A-Za-z0-9_]*";
    static CELLS: OnceLock<Vec<(Lang, Vec<Rule>)>> = OnceLock::new();
    let all = CELLS.get_or_init(|| {
        let id = |s: &str| s.replace("ID", ID);
        let c_func = id(r"^\s*(?:[\w:<>,\*&~\[\]]+\s+)+[\*&]*(~?[A-Za-z_][\w:]*)\s*\(");
        let c_types = id(r"^\s*(?:(?:public|private|protected|internal|static|abstract|sealed|final|partial|readonly|export|typedef)\s+)*(?:class|struct|union|interface|enum(?:\s+class)?|record|namespace)\s+(ID)");
        vec![
            (Lang::Python, build(&[(&id(r"^\s*(?:async\s+)?def\s+(ID)"), Function, ""), (&id(r"^\s*class\s+(ID)"), Class, "")])),
            (
                Lang::JavaScript,
                build(&[
                    (r"^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)", Function, ""),
                    (r"^\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)", Class, ""),
                    (r"^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*(?::[^=]+)?=>|[A-Za-z_$][\w$]*\s*=>)", Function, ""),
                    (r"^\s+(?:(?:public|private|protected|static|readonly|async|override|get|set)\s+)*([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\([^)]*\)\s*(?::\s*[^{=;]+)?\s*\{\s*$", Function, ""),
                ]),
            ),
            (
                Lang::TypeScript,
                build(&[
                    (r"^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)", Function, ""),
                    (r"^\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)", Class, ""),
                    (r"^\s*(?:export\s+)?(?:declare\s+)?(?:interface|enum|type)\s+([A-Za-z_$][\w$]*)", Type, ""),
                    (r"^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*(?::[^=]+)?=>|[A-Za-z_$][\w$]*\s*=>)", Function, ""),
                    (r"^\s+(?:(?:public|private|protected|static|readonly|async|override|get|set|abstract)\s+)*([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\([^)]*\)\s*(?::\s*[^{=;]+)?\s*\{\s*$", Function, ""),
                ]),
            ),
            (Lang::Go, build(&[(&id(r"^func\s+(?:\([^)]*\)\s*)?(ID)"), Function, ""), (&id(r"^type\s+(ID)"), Type, "")])),
            (
                Lang::Rust,
                build(&[
                    (&id(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:(?:const|async|unsafe|extern|default)\s+)*fn\s+(ID)"), Function, ""),
                    (&id(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum|trait|union|type|mod)\s+(ID)"), Type, ""),
                    (r"^\s*(?:unsafe\s+)?impl(?:<[^{]*?>)?\s+([^{]+?)\s*(?:\{|\bwhere\b|$)", Class, "impl "),
                    (&id(r"^\s*macro_rules!\s*(ID)"), Macro, ""),
                ]),
            ),
            (
                Lang::Php,
                build(&[
                    (&id(r"^\s*(?:(?:abstract|final|public|private|protected|static|readonly)\s+)*function\s+&?\s*(ID)"), Function, ""),
                    (&id(r"^\s*(?:(?:abstract|final|readonly)\s+)*(?:class|interface|trait|enum)\s+(ID)"), Class, ""),
                ]),
            ),
            (Lang::Shell, build(&[(r"^\s*function\s+([A-Za-z_][\w.-]*)", Function, ""), (r"^\s*([A-Za-z_][\w.-]*)\s*\(\s*\)", Function, "")])),
            (Lang::C, build(&[(&c_types, Class, ""), (&id(r"^#\s*define\s+(ID)"), Macro, ""), (&c_func, Function, "")])),
            (Lang::CSharp, build(&[(&c_types, Class, ""), (&c_func, Function, "")])),
            (
                Lang::Kotlin,
                build(&[
                    (&id(r"^\s*(?:(?:public|private|protected|internal|open|override|abstract|suspend|inline|operator|infix|tailrec|external|actual|expect)\s+)*fun\s+(?:<[^>]*>\s*)?(?:[\w.]+\.)?(ID)"), Function, ""),
                    (&id(r"^\s*(?:(?:public|private|protected|internal|open|abstract|data|sealed|enum|annotation|inner|value|companion)\s+)*(?:class|interface|object|typealias)\s+(ID)"), Class, ""),
                ]),
            ),
            (
                Lang::Swift,
                build(&[
                    (&id(r"^\s*(?:(?:public|private|fileprivate|internal|open|static|class|final|override|mutating|nonisolated|@\w+)\s+)*func\s+(ID)"), Function, ""),
                    (&id(r"^\s*(?:(?:public|private|fileprivate|internal|open|final|indirect|@\w+)\s+)*(?:class|struct|enum|protocol|extension|actor|typealias)\s+(ID)"), Class, ""),
                ]),
            ),
            (Lang::Markdown, build(&[(r"^#{1,6}\s+(.+?)\s*#*\s*$", Class, "")])),
            (Lang::Makefile, build(&[(r"^([A-Za-z0-9_./%$()-][^:=#\t]*?)\s*::?(?:[^=]|$)", Function, "")])),
            (Lang::Dockerfile, build(&[(r"(?i)^\s*FROM\s+\S+\s+AS\s+(\S+)", Class, "")])),
            (Lang::Yaml, build(&[(r#"^([A-Za-z_"'][^:#]*?)\s*:(?:\s|$)"#, Type, "")])),
            (Lang::Css, build(&[(r"^\s*(@(?:media|supports|container|layer|keyframes)[^{]*?)\s*\{", Type, ""), (r"^\s*([^{};/@\s][^{};]*?)\s*\{", Class, "")])),
            (Lang::Ruby, build(&[(r"^\s*def\s+(?:self\.)?([A-Za-z_]\w*[?!=]?)", Function, ""), (r"^\s*(?:class|module)\s+([A-Z][\w:]*)", Class, "")])),
            (Lang::Lua, build(&[(r"^\s*(?:local\s+)?function\s+([\w.:]+)", Function, ""), (r"^\s*(?:local\s+)?([\w.]+)\s*=\s*function\b", Function, "")])),
            (Lang::PowerShell, build(&[(r"(?i)^\s*(?:function|filter|workflow)\s+([\w-]+)", Function, ""), (r"(?i)^\s*class\s+(\w+)", Class, ""), (r"(?i)^\s*enum\s+(\w+)", Type, "")])),
            (Lang::Dart, build(&[(&id(r"^\s*(?:(?:abstract|sealed|base|final|interface)\s+)*(?:class|mixin|enum|extension|typedef)\s+(ID)"), Class, ""), (&c_func, Function, "")])),
            (Lang::Java, build(&[(&c_types, Class, ""), (&c_func, Function, "")])),
        ]
    });
    all.iter()
        .find(|(l, _)| *l == lang)
        .map_or(&[], |(_, r)| r.as_slice())
}

/// True if `lang` has symbols to list.
pub fn supported(lang: Lang) -> bool {
    !rules(lang).is_empty()
}

/// The symbols of `text` (code in `lang`), with depths from the indentation step `unit`.
pub fn scan(lang: Lang, text: &str, unit: u32) -> Vec<Symbol> {
    let rules = rules(lang);
    let mut out = Vec::new();
    if rules.is_empty() {
        return out;
    }
    let mut st = highlight::State::start(lang, true);
    let mut spans = Vec::new();
    let mut caps = rules[0].re.create_captures();
    for (n, raw) in text.split('\n').enumerate() {
        let line = raw.strip_suffix('\r').unwrap_or(raw);
        spans.clear();
        let before = st;
        st = highlight::spans_from(lang, st, line, true, &mut spans);
        // A line that starts inside a comment or string holds no definition at its start.
        if before.in_comment() || before.in_string() || line.trim().is_empty() {
            continue;
        }
        let mut code = line.as_bytes().to_vec();
        for &(s, e, k) in &spans {
            if matches!(k, Kind::Comment | Kind::Str) {
                for b in &mut code[s.min(line.len())..e.min(line.len())] {
                    *b = b' ';
                }
            }
        }
        for r in rules {
            r.re.captures(code.as_slice(), &mut caps);
            let Some(g) = caps.get_group(1) else { continue };
            let name = line[g.range()].trim();
            if NOT_NAMES.contains(&name) {
                continue;
            }
            // A C-style call or declaration is not a definition.
            if r.kind == SymKind::Function
                && matches!(lang, Lang::C | Lang::CSharp | Lang::Java | Lang::Dart)
                && r.prefix.is_empty()
            {
                let t = String::from_utf8_lossy(&code);
                let t = t.trim_end();
                let first = t.split_whitespace().next().unwrap_or("");
                if t.ends_with(';')
                    || NOT_NAMES.contains(&first)
                    || t.contains('=') && t.find('=') < t.find('(')
                {
                    continue;
                }
            }
            let indent: u32 = line
                .chars()
                .take_while(|c| *c == ' ' || *c == '\t')
                .map(|c| {
                    if c == '\t' {
                        bigtext::text::rows::TAB_WIDTH as u32
                    } else {
                        1
                    }
                })
                .sum();
            // A Markdown heading's depth is its level; YAML lists only the top level.
            let depth = match lang {
                Lang::Markdown => line
                    .bytes()
                    .take_while(|&b| b == b'#')
                    .count()
                    .saturating_sub(1) as u32,
                Lang::Yaml if indent > 0 => continue,
                _ => indent / unit.max(1),
            };
            // Make's special targets (.PHONY) are not symbols.
            if lang == Lang::Makefile && name.starts_with('.') {
                continue;
            }
            out.push(Symbol {
                name: format!("{}{name}", r.prefix),
                kind: r.kind,
                line: n as u64,
                depth: depth.min(8) as u8,
            });
            break;
        }
    }
    out
}

/// Go to symbol's state.
#[derive(Default)]
pub struct SymbolsUi {
    pub doc: u64,
    pub version: u64,
    pub list: Option<Vec<Symbol>>,
    /// The file was longer than [`SCAN_MAX`].
    pub cut: bool,
    pub query: String,
    pub selected: usize,
    /// Shown (closed, the list is kept for the next Ctrl+R on the same version).
    pub open: bool,
    focus: bool,
    rx: Option<mpsc::Receiver<(Vec<Symbol>, bool)>>,
}

impl BigTextApp {
    /// Search → Go to symbol.
    pub(crate) fn open_symbols(&mut self) {
        let Some(d) = self.docs.get(self.active) else {
            return;
        };
        let lang = d.effective_lang();
        if !supported(lang) || !d.is_ready() {
            self.notify(
                tr("Go to symbol lists the functions and classes of code files.").into(),
                None,
            );
            return;
        }
        if let Some(s) = self
            .symbols
            .as_mut()
            .filter(|s| s.doc == d.id && s.version == d.version && s.list.is_some())
        {
            s.query.clear();
            s.selected = 0;
            s.focus = true;
            s.open = true;
            return;
        }
        let (snap, len) = (d.buf.snapshot(), d.buf.len());
        let unit = d.view.indent_unit.map_or(4, |u| u.1);
        let (tx, rx) = mpsc::channel();
        let waker = self.waker.clone();
        bigtext::jobs::spawn("symbols", move || {
            let n = len.min(SCAN_MAX);
            let list = snap
                .read(0..n)
                .map(|b| scan(lang, &snap.codec.decode_lossy(&b), unit))
                .unwrap_or_default();
            let _ = tx.send((list, len > SCAN_MAX));
            waker();
        });
        self.symbols = Some(SymbolsUi {
            doc: d.id,
            version: d.version,
            focus: true,
            open: true,
            rx: Some(rx),
            ..Default::default()
        });
    }

    pub(super) fn symbols_window(&mut self, ctx: &egui::Context) {
        let Some(s) = &mut self.symbols else { return };
        if let Some(rx) = &s.rx {
            if let Ok((list, cut)) = rx.try_recv() {
                s.list = Some(list);
                s.cut = cut;
                s.rx = None;
            }
        }
        if !s.open {
            return;
        }
        let (up, down, enter, esc) = ctx.input_mut(|i| {
            (
                i.consume_key(Modifiers::NONE, Key::ArrowUp),
                i.consume_key(Modifiers::NONE, Key::ArrowDown),
                i.consume_key(Modifiers::NONE, Key::Enter),
                i.consume_key(Modifiers::NONE, Key::Escape),
            )
        });
        let list = s.list.clone().unwrap_or_default();
        let names: Vec<String> = list.iter().map(|x| x.name.clone()).collect();
        let found = |q: &str| -> Vec<usize> {
            if q.trim().is_empty() {
                (0..list.len()).collect()
            } else {
                super::quick_open::best(q, &names, SHOWN)
                    .into_iter()
                    .map(|(i, _)| i)
                    .collect()
            }
        };
        let mut shown = found(&s.query);
        s.selected = s.selected.min(shown.len().saturating_sub(1));
        if up {
            s.selected = s.selected.saturating_sub(1);
        }
        if down && s.selected + 1 < shown.len() {
            s.selected += 1;
        }
        let mut chosen = None;
        let reading = s.rx.is_some();
        egui::Window::new("symbols")
            .title_bar(false)
            .anchor(egui::Align2::CENTER_TOP, egui::vec2(0.0, 60.0))
            .fixed_size(egui::vec2(520.0, 0.0))
            .show(ctx, |ui| {
                ui.horizontal(|ui| {
                    let r = ui.add(
                        egui::TextEdit::singleline(&mut s.query)
                            .hint_text(tr("Go to symbol"))
                            .desired_width(ui.available_width() - if reading { 24.0 } else { 0.0 }),
                    );
                    if s.focus {
                        r.request_focus();
                        s.focus = false;
                    }
                    if r.changed() {
                        s.selected = 0;
                    }
                    if reading {
                        ui.spinner().on_hover_text(tr("Reading…"));
                    }
                });
                ui.separator();
                let weak = ui.visuals().weak_text_color();
                let info = super::theme::info_text(ui);
                egui::ScrollArea::vertical()
                    .max_height(420.0)
                    .show(ui, |ui| {
                        for (k, &i) in shown.iter().enumerate() {
                            let sym = &list[i];
                            let r = ui
                                .horizontal(|ui| {
                                    ui.add_space(sym.depth as f32 * 12.0);
                                    ui.colored_label(info, sym.kind.badge());
                                    let b = egui::Button::selectable(k == s.selected, &sym.name)
                                        .shortcut_text(
                                            egui::RichText::new(format!("{}", sym.line + 1))
                                                .color(weak),
                                        );
                                    ui.add(b.min_size(egui::vec2(ui.available_width(), 0.0)))
                                })
                                .inner;
                            if k == s.selected {
                                r.scroll_to_me(None);
                            }
                            if r.clicked() {
                                chosen = Some(i);
                            }
                        }
                        if shown.is_empty() && !reading {
                            ui.weak(if list.is_empty() {
                                tr("No functions or classes were found.")
                            } else {
                                tr("No symbol matches.")
                            });
                        }
                    });
                if s.cut {
                    ui.weak(trf(
                        "Only the first {size} of the file are read.",
                        &[("size", &bigtext::jobs::human_bytes(SCAN_MAX))],
                    ));
                }
            });
        // Enter takes the text typed in this frame too.
        if enter && chosen.is_none() {
            shown = found(&s.query);
            chosen = shown
                .get(s.selected.min(shown.len().saturating_sub(1)))
                .copied();
        }
        let doc = s.doc;
        if let Some(i) = chosen {
            s.open = false;
            let line = list[i].line + 1;
            if let Some(d) = self.docs.iter_mut().find(|d| d.id == doc) {
                d.goto_line(line);
            }
        } else if esc {
            s.open = false;
        }
    }

    /// Go to symbol is open (it takes the keys).
    pub(crate) fn symbols_open(&self) -> bool {
        self.symbols.as_ref().is_some_and(|s| s.open)
    }
}

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

    fn names(lang: Lang, text: &str) -> Vec<(String, SymKind, u64, u8)> {
        scan(lang, text, 4)
            .into_iter()
            .map(|s| (s.name, s.kind, s.line, s.depth))
            .collect()
    }

    #[test]
    fn python_and_javascript() {
        let py = "class User:\n    \"\"\"Docs.\n    def not_this(): ...\n    \"\"\"\n    def greet(self):\n        pass\n\nasync def main():\n    x = 'def nope():'\n";
        assert_eq!(
            names(Lang::Python, py),
            vec![
                ("User".into(), SymKind::Class, 0, 0),
                ("greet".into(), SymKind::Function, 4, 1),
                ("main".into(), SymKind::Function, 7, 0)
            ]
        );
        let js = "export async function load(url) {\n}\nconst add = (a, b) => a + b;\nclass View {\n  render() {\n    if (x) {\n    }\n  }\n}\n// function commented() {}\n";
        let got: Vec<String> = names(Lang::JavaScript, js)
            .into_iter()
            .map(|s| s.0)
            .collect();
        assert_eq!(got, ["load", "add", "View", "render"]);
    }

    #[test]
    fn rust_go_c_family() {
        let rs = "pub struct A;\nimpl Display for A {\n    pub(crate) async fn fmt(&self) {}\n}\nmacro_rules! m { }\n";
        let got: Vec<(String, SymKind)> = names(Lang::Rust, rs)
            .into_iter()
            .map(|s| (s.0, s.1))
            .collect();
        assert_eq!(
            got,
            [
                ("A".into(), SymKind::Type),
                ("impl Display for A".into(), SymKind::Class),
                ("fmt".into(), SymKind::Function),
                ("m".into(), SymKind::Macro)
            ]
        );
        let go =
            "func (s *Server) Handle(w http.ResponseWriter) error {\n}\ntype Server struct {}\n";
        assert_eq!(
            names(Lang::Go, go)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["Handle", "Server"]
        );
        let java = "public class Demo {\n    @Override\n    public String toString() {\n        return format(x);\n    }\n    int n = count(1);\n    void run();\n    } else if (x) {\n}\n";
        assert_eq!(
            names(Lang::Java, java)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["Demo", "toString"]
        );
        let c = "#define MAX 10\nstatic int count(const char *s) {\n    printf(\"x\");\n}\nstruct point {\n";
        assert_eq!(
            names(Lang::C, c)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["MAX", "count", "point"]
        );
        assert!(!supported(Lang::Json) && supported(Lang::Shell));
        let kt = "data class User(val id: Int)\nobject Registry {\n    suspend fun <T> load(x: T): T = x\n    fun String.shout() = uppercase()\n}\n";
        assert_eq!(
            names(Lang::Kotlin, kt)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["User", "Registry", "load", "shout"]
        );
        let sw =
            "public struct Point {\n    mutating func move() {}\n}\nextension Point: Codable {}\n";
        assert_eq!(
            names(Lang::Swift, sw)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["Point", "move", "Point"]
        );
        let rb = "module Shop\n  class Cart < Base\n    def empty?\n    end\n    def self.build\n    end\n  end\nend\n";
        assert_eq!(
            names(Lang::Ruby, rb)
                .into_iter()
                .map(|s| (s.0, s.3))
                .collect::<Vec<_>>(),
            [
                ("Shop".into(), 0),
                ("Cart".into(), 0),
                ("empty?".into(), 1),
                ("build".into(), 1)
            ]
        );
        let lua = "local function helper(x)\nend\nfunction M.run()\nend\nM.stop = function() end\n";
        assert_eq!(
            names(Lang::Lua, lua)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["helper", "M.run", "M.stop"]
        );
        let ps = "Function Get-Thing {\n}\nclass Point {\n}\n# function Not-This\n";
        assert_eq!(
            names(Lang::PowerShell, ps)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["Get-Thing", "Point"]
        );
        let md = "# Guide\nText\n## Install\n```sh\n# not this\n```\n### Linux ###\n";
        assert_eq!(
            names(Lang::Markdown, md)
                .into_iter()
                .map(|s| (s.0, s.3))
                .collect::<Vec<_>>(),
            [
                ("Guide".into(), 0),
                ("Install".into(), 1),
                ("Linux".into(), 2)
            ]
        );
        let mk = "CC := gcc\n.PHONY: all\nall: app\napp: main.o\n\t$(CC) -o $@ $^\n";
        assert_eq!(
            names(Lang::Makefile, mk)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["all", "app"]
        );
        let df = "FROM rust AS build\nRUN cargo build\nFROM debian\n";
        assert_eq!(
            names(Lang::Dockerfile, df)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["build"]
        );
        let yaml = "name: ci\non:\n  push:\njobs:\n  test: x\n";
        assert_eq!(
            names(Lang::Yaml, yaml)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["name", "on", "jobs"]
        );
        let css = "@media print {\n  .card, a:hover {\n    color: red;\n  }\n}\n";
        assert_eq!(
            names(Lang::Css, css)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["@media print", ".card, a:hover"]
        );
        let dart =
            "abstract class Shape {}\nFuture<void> main() async {\n  print(1);\n}\nmixin Fly {}\n";
        assert_eq!(
            names(Lang::Dart, dart)
                .into_iter()
                .map(|s| s.0)
                .collect::<Vec<_>>(),
            ["Shape", "main", "Fly"]
        );
    }
}