diffler-core 0.17.0

Core review engine for diffler: diffs, sessions, comments, viewed marks
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Language registry: maps a file path to its tree-sitter grammar, a configured
//! highlight configuration, and (where the grammar ships one) a tags query used
//! for scope/definition lookup. Built once and reused.

use std::borrow::Cow;
use std::collections::HashMap;
use std::ops::Range;
use std::path::Path;
use std::sync::{LazyLock, OnceLock};

use tree_sitter::{Language, Parser, Query, QueryCursor, StreamingIterator, Tree};
use tree_sitter_highlight::HighlightConfiguration;

use crate::syntax::MAX_PARSE_BYTES;

/// Capture names recognized during highlighting. A grammar capture like
/// `function.method` resolves to the longest matching prefix here (`function`),
/// so listing the general categories is enough to color every grammar.
pub const HIGHLIGHT_NAMES: &[&str] = &[
    "attribute",
    "boolean",
    "comment",
    "conditional",
    "constant",
    "constant.builtin",
    "constructor",
    "field",
    "function",
    "function.builtin",
    "keyword",
    "label",
    "number",
    "operator",
    "parameter",
    "property",
    "punctuation",
    "punctuation.bracket",
    "punctuation.delimiter",
    "spell",
    "storageclass",
    "string",
    "string.escape",
    "string.special",
    "tag",
    "text.emphasis",
    "text.literal",
    "text.reference",
    "text.strong",
    "text.title",
    "text.uri",
    "type",
    "type.builtin",
    "variable",
    "variable.builtin",
    "variable.parameter",
];

pub struct LangEntry {
    pub name: &'static str,
    pub language: Language,
    highlights: Cow<'static, str>,
    injections: Cow<'static, str>,
    tags_query: Option<Cow<'static, str>>,
    /// This language's indentation or layout is syntax: re-indenting a line
    /// there moves it between blocks, so the structural diff algorithm never
    /// calls it a reformat, whatever the AST diff reports.
    pub layout_significant: bool,
    /// Compiling a query costs ~15ms, so a grammar pays only once someone opens
    /// a file in it. Both stay `None` when the grammar's query fails to
    /// compile: the file renders plain instead of erroring.
    config: OnceLock<Option<HighlightConfiguration>>,
    tags: OnceLock<Option<Query>>,
}

impl LangEntry {
    pub fn config(&self) -> Option<&HighlightConfiguration> {
        self.config
            .get_or_init(|| {
                HighlightConfiguration::new(
                    self.language.clone(),
                    self.name,
                    &self.highlights,
                    &self.injections,
                    "",
                )
                .ok()
                .map(|mut config| {
                    config.configure(HIGHLIGHT_NAMES);
                    config
                })
            })
            .as_ref()
    }

    pub fn tags(&self) -> Option<&Query> {
        self.tags
            .get_or_init(|| Query::new(&self.language, self.tags_query.as_deref()?).ok())
            .as_ref()
    }
}

/// The grammars, built once for the process. Registration only records the
/// grammar and its query text, so this costs microseconds; a theme switch
/// rebuilds the palette and reuses these.
pub static REGISTRY: LazyLock<LanguageRegistry> = LazyLock::new(LanguageRegistry::build);

pub struct LanguageRegistry {
    entries: Vec<LangEntry>,
    by_ext: HashMap<&'static str, usize>,
    by_name: HashMap<&'static str, usize>,
    by_filename: HashMap<&'static str, usize>,
    /// The inline markdown highlight query, applied by hand over the block
    /// grammar's `(inline)` nodes: tree-sitter's generic injection does not
    /// drive the split markdown grammar's inline pass.
    markdown_inline_query: Option<Query>,
}

impl LanguageRegistry {
    /// Build the registry with every bundled grammar, reused for the session.
    // flat per-language registration table
    #[allow(clippy::too_many_lines)]
    pub fn build() -> Self {
        let mut r = Self {
            entries: Vec::new(),
            by_ext: HashMap::new(),
            by_name: HashMap::new(),
            by_filename: HashMap::new(),
            markdown_inline_query: None,
        };

        r.add(
            "rust",
            &["rs"],
            tree_sitter_rust::LANGUAGE.into(),
            tree_sitter_rust::HIGHLIGHTS_QUERY,
            Some(tree_sitter_rust::TAGS_QUERY),
        );
        r.add(
            "python",
            &["py", "pyi"],
            tree_sitter_python::LANGUAGE.into(),
            tree_sitter_python::HIGHLIGHTS_QUERY,
            Some(tree_sitter_python::TAGS_QUERY),
        );
        r.layout_significant();
        r.add(
            "javascript",
            &["js", "jsx", "mjs", "cjs"],
            tree_sitter_javascript::LANGUAGE.into(),
            format!(
                "{}\n{}",
                tree_sitter_javascript::HIGHLIGHT_QUERY,
                tree_sitter_javascript::JSX_HIGHLIGHT_QUERY
            ),
            Some(tree_sitter_javascript::TAGS_QUERY),
        );
        r.add(
            "typescript",
            &["ts", "mts", "cts"],
            tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
            format!(
                "{}\n{}",
                tree_sitter_javascript::HIGHLIGHT_QUERY,
                tree_sitter_typescript::HIGHLIGHTS_QUERY
            ),
            None,
        );
        r.tags_over_javascript(tree_sitter_typescript::TAGS_QUERY);
        r.add(
            "tsx",
            &["tsx"],
            tree_sitter_typescript::LANGUAGE_TSX.into(),
            format!(
                "{}\n{}\n{}",
                tree_sitter_javascript::HIGHLIGHT_QUERY,
                tree_sitter_javascript::JSX_HIGHLIGHT_QUERY,
                tree_sitter_typescript::HIGHLIGHTS_QUERY
            ),
            None,
        );
        r.tags_over_javascript(tree_sitter_typescript::TAGS_QUERY);
        r.add(
            "go",
            &["go"],
            tree_sitter_go::LANGUAGE.into(),
            tree_sitter_go::HIGHLIGHTS_QUERY,
            Some(tree_sitter_go::TAGS_QUERY),
        );
        r.add(
            "c",
            &["c", "h"],
            tree_sitter_c::LANGUAGE.into(),
            tree_sitter_c::HIGHLIGHT_QUERY,
            Some(tree_sitter_c::TAGS_QUERY),
        );
        r.add(
            "cpp",
            &["cpp", "cc", "cxx", "hpp", "hh", "hxx"],
            tree_sitter_cpp::LANGUAGE.into(),
            // the C++ query extends C's; alone it matches only C++ constructs
            format!(
                "{}\n{}",
                tree_sitter_c::HIGHLIGHT_QUERY,
                tree_sitter_cpp::HIGHLIGHT_QUERY
            ),
            Some(tree_sitter_cpp::TAGS_QUERY),
        );
        r.add(
            "java",
            &["java"],
            tree_sitter_java::LANGUAGE.into(),
            tree_sitter_java::HIGHLIGHTS_QUERY,
            Some(tree_sitter_java::TAGS_QUERY),
        );
        r.add(
            "c-sharp",
            &["cs"],
            tree_sitter_c_sharp::LANGUAGE.into(),
            tree_sitter_c_sharp::HIGHLIGHTS_QUERY,
            Some(tree_sitter_c_sharp::TAGS_QUERY),
        );
        r.add(
            "ruby",
            &["rb"],
            tree_sitter_ruby::LANGUAGE.into(),
            tree_sitter_ruby::HIGHLIGHTS_QUERY,
            Some(tree_sitter_ruby::TAGS_QUERY),
        );
        r.add(
            "php",
            &["php"],
            tree_sitter_php::LANGUAGE_PHP.into(),
            tree_sitter_php::HIGHLIGHTS_QUERY,
            Some(tree_sitter_php::TAGS_QUERY),
        );
        r.add(
            "bash",
            &["sh", "bash", "zsh"],
            tree_sitter_bash::LANGUAGE.into(),
            tree_sitter_bash::HIGHLIGHT_QUERY,
            None,
        );
        r.add(
            "json",
            &["json"],
            tree_sitter_json::LANGUAGE.into(),
            tree_sitter_json::HIGHLIGHTS_QUERY,
            None,
        );
        r.register(
            "html",
            &["html", "htm"],
            tree_sitter_html::LANGUAGE.into(),
            tree_sitter_html::HIGHLIGHTS_QUERY,
            tree_sitter_html::INJECTIONS_QUERY,
            None,
        );
        r.add(
            "css",
            &["css"],
            tree_sitter_css::LANGUAGE.into(),
            tree_sitter_css::HIGHLIGHTS_QUERY,
            None,
        );
        r.add(
            "yaml",
            &["yml", "yaml"],
            tree_sitter_yaml::LANGUAGE.into(),
            tree_sitter_yaml::HIGHLIGHTS_QUERY,
            None,
        );
        r.layout_significant();
        // the grammar's own numeric patterns are guarded by Lua-style `%d`
        // predicates that tree-sitter's regex engine never matches, leaving
        // every number styled as a string; a later pattern wins, so this one
        // restores number coloring
        r.add(
            "sql",
            &["sql"],
            tree_sitter_sequel::LANGUAGE.into(),
            format!(
                "{}\n((literal) @number (#match? @number \"^[-+]?[0-9][0-9.]*$\"))\n",
                tree_sitter_sequel::HIGHLIGHTS_QUERY
            ),
            None,
        );
        // The block grammar highlights headings/markers and injects fenced code
        // into its own language; inline emphasis, code spans, and links come from
        // the by-hand inline pass below.
        r.register(
            "markdown",
            &["md", "markdown"],
            tree_sitter_md::LANGUAGE.into(),
            tree_sitter_md::HIGHLIGHT_QUERY_BLOCK,
            tree_sitter_md::INJECTION_QUERY_BLOCK,
            None,
        );
        let md_inline: Language = tree_sitter_md::INLINE_LANGUAGE.into();
        r.markdown_inline_query =
            Query::new(&md_inline, tree_sitter_md::HIGHLIGHT_QUERY_INLINE).ok();
        r.register(
            "markdown_inline",
            &[],
            md_inline,
            tree_sitter_md::HIGHLIGHT_QUERY_INLINE,
            "",
            None,
        );

        r.add(
            "toml",
            &["toml"],
            tree_sitter_toml_ng::LANGUAGE.into(),
            tree_sitter_toml_ng::HIGHLIGHTS_QUERY,
            None,
        );
        // the grammar crate ships a parser only; the query is vendored
        r.add(
            "hcl",
            &["tf", "tfvars", "hcl"],
            tree_sitter_hcl::LANGUAGE.into(),
            include_str!("../../queries/hcl/highlights.scm"),
            None,
        );
        r.add(
            "dockerfile",
            &["dockerfile", "containerfile"],
            arborium_dockerfile::language().into(),
            arborium_dockerfile::HIGHLIGHTS_QUERY,
            None,
        );
        r.add(
            "make",
            &["mk"],
            tree_sitter_make::LANGUAGE.into(),
            tree_sitter_make::HIGHLIGHTS_QUERY,
            None,
        );
        r.layout_significant();
        r.add(
            "lua",
            &["lua"],
            tree_sitter_lua::LANGUAGE.into(),
            tree_sitter_lua::HIGHLIGHTS_QUERY,
            Some(tree_sitter_lua::TAGS_QUERY),
        );
        r.add(
            "nix",
            &["nix"],
            tree_sitter_nix::LANGUAGE.into(),
            tree_sitter_nix::HIGHLIGHTS_QUERY,
            None,
        );
        r.add(
            "xml",
            &["xml", "xsd", "xslt", "svg"],
            tree_sitter_xml::LANGUAGE_XML.into(),
            tree_sitter_xml::XML_HIGHLIGHT_QUERY,
            None,
        );
        r.add(
            "swift",
            &["swift"],
            tree_sitter_swift::LANGUAGE.into(),
            tree_sitter_swift::HIGHLIGHTS_QUERY,
            Some(tree_sitter_swift::TAGS_QUERY),
        );
        r.add(
            "scala",
            &["scala", "sbt"],
            tree_sitter_scala::LANGUAGE.into(),
            tree_sitter_scala::HIGHLIGHTS_QUERY,
            None,
        );
        r.layout_significant();
        r.add(
            "elixir",
            &["ex", "exs"],
            tree_sitter_elixir::LANGUAGE.into(),
            tree_sitter_elixir::HIGHLIGHTS_QUERY,
            Some(tree_sitter_elixir::TAGS_QUERY),
        );
        r.add(
            "zig",
            &["zig", "zon"],
            tree_sitter_zig::LANGUAGE.into(),
            tree_sitter_zig::HIGHLIGHTS_QUERY,
            None,
        );
        r.add(
            "haskell",
            &["hs"],
            tree_sitter_haskell::LANGUAGE.into(),
            tree_sitter_haskell::HIGHLIGHTS_QUERY,
            None,
        );
        r.layout_significant();
        r.add(
            "dart",
            &["dart"],
            tree_sitter_dart::LANGUAGE.into(),
            tree_sitter_dart::HIGHLIGHTS_QUERY,
            Some(tree_sitter_dart::TAGS_QUERY),
        );
        r.add(
            "powershell",
            &["ps1", "psm1", "psd1"],
            tree_sitter_powershell::LANGUAGE.into(),
            tree_sitter_powershell::HIGHLIGHTS_QUERY,
            None,
        );
        r.register(
            "svelte",
            &["svelte"],
            tree_sitter_svelte_ng::LANGUAGE.into(),
            // the svelte query extends html's, and its injections carry
            // `<script>` and `<style>` into the js and css grammars
            format!(
                "{}\n{}",
                tree_sitter_html::HIGHLIGHTS_QUERY,
                tree_sitter_svelte_ng::HIGHLIGHTS_QUERY
            ),
            tree_sitter_svelte_ng::INJECTIONS_QUERY,
            None,
        );
        r.name_files("make", &["makefile", "gnumakefile"]);
        r.name_files("dockerfile", &["dockerfile", "containerfile"]);

        r
    }

    /// Route files a build tool names outright (`Makefile`, `Dockerfile`).
    fn name_files(&mut self, name: &'static str, filenames: &'static [&'static str]) {
        let Some(&idx) = self.by_name.get(name) else {
            return;
        };
        for file in filenames {
            self.by_filename.insert(file, idx);
        }
    }

    fn add(
        &mut self,
        name: &'static str,
        extensions: &'static [&'static str],
        language: Language,
        highlights: impl Into<Cow<'static, str>>,
        tags: Option<&'static str>,
    ) {
        self.register(name, extensions, language, highlights, "", tags);
    }

    fn register(
        &mut self,
        name: &'static str,
        extensions: &'static [&'static str],
        language: Language,
        highlights: impl Into<Cow<'static, str>>,
        injections: impl Into<Cow<'static, str>>,
        tags: Option<&'static str>,
    ) {
        let idx = self.entries.len();
        self.entries.push(LangEntry {
            name,
            language,
            highlights: highlights.into(),
            injections: injections.into(),
            tags_query: tags.map(Cow::Borrowed),
            layout_significant: false,
            config: OnceLock::new(),
            tags: OnceLock::new(),
        });
        self.by_name.insert(name, idx);
        for ext in extensions {
            self.by_ext.insert(ext, idx);
        }
    }

    /// Flags the language just registered as layout-significant (see
    /// [`LangEntry::layout_significant`]); called right after its `add`/
    /// `register` so the flag travels with the entry itself.
    fn layout_significant(&mut self) {
        if let Some(entry) = self.entries.last_mut() {
            entry.layout_significant = true;
        }
    }

    /// Gives the language just registered JavaScript's tags plus its own
    /// `tags`. TypeScript's tags query covers only what TypeScript adds to
    /// JavaScript, so on its own it finds no plain function at all.
    fn tags_over_javascript(&mut self, tags: &'static str) {
        if let Some(entry) = self.entries.last_mut() {
            entry.tags_query = Some(Cow::Owned(format!(
                "{}\n{tags}",
                tree_sitter_javascript::TAGS_QUERY
            )));
        }
    }

    /// The entry whose grammar handles `path`, by extension or, for the files
    /// a build tool names outright (`Makefile`, `Dockerfile`), by basename.
    pub fn for_path(&self, path: &str) -> Option<&LangEntry> {
        let name = Path::new(path).file_name()?.to_str()?.to_ascii_lowercase();
        if let Some(&idx) = self.by_filename.get(name.as_str()) {
            return self.entries.get(idx);
        }
        let ext = Path::new(path).extension()?.to_str()?;
        let &idx = self.by_ext.get(ext)?;
        self.entries.get(idx)
    }

    /// The entry for a markdown fence token (`rust`, `py`, `c++`, ...), matched
    /// by grammar name then extension.
    pub fn for_token(&self, token: &str) -> Option<&LangEntry> {
        let token = token.trim().to_ascii_lowercase();
        let token = match token.as_str() {
            "c++" => "cpp",
            "c#" | "csharp" => "cs",
            "shell" => "bash",
            "golang" => "go",
            "terraform" => "hcl",
            "docker" => "dockerfile",
            "makefile" => "make",
            "pwsh" | "ps" => "powershell",
            other => other,
        };
        let &idx = self.by_name.get(token).or_else(|| self.by_ext.get(token))?;
        self.entries.get(idx)
    }

    /// Highlight config for a tree-sitter injection language name (the inline
    /// markdown grammar, or a fenced code block's language). `None` leaves the
    /// injected region plain.
    pub fn config_for_injection(&self, lang: &str) -> Option<&HighlightConfiguration> {
        self.for_token(lang)?.config()
    }

    /// Inline markdown captures (emphasis, code spans, links) as byte range plus
    /// the recognized highlight name, narrowest span first so a first-match
    /// renderer picks the most specific. The inline grammar parses only the
    /// block grammar's `(inline)` node ranges, so block markers stay untouched.
    pub fn markdown_inline_spans(&self, content: &str) -> Vec<(Range<usize>, &'static str)> {
        if content.len() > MAX_PARSE_BYTES {
            return Vec::new();
        }
        let (Some(query), Some(block), Some(inline)) = (
            self.markdown_inline_query.as_ref(),
            self.by_name
                .get("markdown")
                .and_then(|&i| self.entries.get(i)),
            self.by_name
                .get("markdown_inline")
                .and_then(|&i| self.entries.get(i)),
        ) else {
            return Vec::new();
        };

        let mut bp = Parser::new();
        if bp.set_language(&block.language).is_err() {
            return Vec::new();
        }
        let Some(block_tree) = bp.parse(content, None) else {
            return Vec::new();
        };
        let ranges = inline_node_ranges(&block_tree);
        if ranges.is_empty() {
            return Vec::new();
        }

        let mut ip = Parser::new();
        if ip.set_included_ranges(&ranges).is_err() || ip.set_language(&inline.language).is_err() {
            return Vec::new();
        }
        let Some(inline_tree) = ip.parse(content, None) else {
            return Vec::new();
        };

        let names = query.capture_names();
        let mut cursor = QueryCursor::new();
        let mut spans = Vec::new();
        let mut matches = cursor.matches(query, inline_tree.root_node(), content.as_bytes());
        while let Some(m) = matches.next() {
            for cap in m.captures {
                let cname = names.get(cap.index as usize).copied().unwrap_or("");
                if let Some(name) = recognized_highlight(cname) {
                    spans.push((cap.node.byte_range(), name));
                }
            }
        }
        spans.sort_by_key(|(r, _)| r.end - r.start);
        spans
    }
}

/// Byte ranges of every `(inline)` node in a markdown block tree, in document
/// order. These are the regions the inline grammar reparses.
fn inline_node_ranges(tree: &Tree) -> Vec<tree_sitter::Range> {
    let mut ranges = Vec::new();
    let mut cursor = tree.walk();
    loop {
        let node = cursor.node();
        if node.kind() == "inline" && node.end_byte() > node.start_byte() {
            ranges.push(node.range());
        }
        if cursor.goto_first_child() {
            continue;
        }
        loop {
            if cursor.goto_next_sibling() {
                break;
            }
            if !cursor.goto_parent() {
                return ranges;
            }
        }
    }
}

/// The longest `HIGHLIGHT_NAMES` entry that is a dotted prefix of `capture`,
/// matching how tree-sitter resolves capture names to recognized highlights.
fn recognized_highlight(capture: &str) -> Option<&'static str> {
    HIGHLIGHT_NAMES
        .iter()
        .copied()
        .filter(|name| {
            capture == *name
                || capture
                    .strip_prefix(name)
                    .is_some_and(|r| r.starts_with('.'))
        })
        .max_by_key(|name| name.len())
}

impl Default for LanguageRegistry {
    fn default() -> Self {
        Self::build()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn exactly_the_layout_languages_are_layout_significant() {
        let flagged: Vec<_> = super::REGISTRY
            .entries
            .iter()
            .filter(|entry| entry.layout_significant)
            .map(|entry| entry.name)
            .collect();
        assert_eq!(flagged, ["python", "yaml", "make", "scala", "haskell"]);
    }
}