Skip to main content

rustik_diff/
builtins.rs

1//! Built-in highlighting assets for diff viewers.
2//!
3//! This module bundles a default syntax theme and a small set of common
4//! TextMate grammars, compiling them into a [`BuiltinHighlighter`] selected for
5//! the files in a diff. It is gated behind the `builtins` feature so callers
6//! that bring their own highlighting do not pay for the embedded assets.
7
8use std::fs;
9use std::path::{Path, PathBuf};
10
11use rustik_highlight as syntax;
12
13use crate::{BuiltinHighlighter, DiffDocument, DiffPalette};
14
15/// Theme name used for built-in diff highlighting.
16const THEME_NAME: &str = "rustik-catppuccin-mocha";
17/// Built-in syntax theme JSON.
18const THEME: &str = include_str!("themes/rustik-catppuccin-mocha.json");
19/// Relative directory containing user-defined TextMate grammars.
20const CUSTOM_SYNTAX_DIR: &str = "rustik/highlight/grammars";
21/// Common syntax grammars loaded by the interactive pager.
22const SYNTAXES: [SyntaxAsset; 20] = [
23    SyntaxAsset {
24        source: syntax::syntaxes::C,
25        file_types: &["c", "h"],
26        dependencies: &[],
27    },
28    SyntaxAsset {
29        source: syntax::syntaxes::CSS,
30        file_types: &["css"],
31        dependencies: &[],
32    },
33    SyntaxAsset {
34        source: syntax::syntaxes::ERB,
35        file_types: &["erb", "rhtml", "html.erb"],
36        dependencies: &[
37            syntax::syntaxes::HTML,
38            syntax::syntaxes::RUBY,
39            syntax::syntaxes::CSS,
40            syntax::syntaxes::JAVASCRIPT,
41        ],
42    },
43    SyntaxAsset {
44        source: syntax::syntaxes::FISH,
45        file_types: &["fish"],
46        dependencies: &[],
47    },
48    SyntaxAsset {
49        source: syntax::syntaxes::GO,
50        file_types: &["go"],
51        dependencies: &[],
52    },
53    SyntaxAsset {
54        source: syntax::syntaxes::HTML,
55        file_types: &["html", "htm", "xhtml"],
56        dependencies: &[syntax::syntaxes::CSS, syntax::syntaxes::JAVASCRIPT],
57    },
58    SyntaxAsset {
59        source: syntax::syntaxes::JAVASCRIPT,
60        file_types: &["js", "cjs", "mjs"],
61        dependencies: &[],
62    },
63    SyntaxAsset {
64        source: syntax::syntaxes::LUA,
65        file_types: &["lua"],
66        dependencies: &[],
67    },
68    SyntaxAsset {
69        source: syntax::syntaxes::MAKE,
70        file_types: &["makefile", "mk"],
71        dependencies: &[],
72    },
73    SyntaxAsset {
74        source: syntax::syntaxes::MARKDOWN,
75        file_types: &["md", "markdown", "mdown"],
76        dependencies: &[
77            syntax::syntaxes::YAML,
78            syntax::syntaxes::HTML,
79            syntax::syntaxes::CSS,
80            syntax::syntaxes::JAVASCRIPT,
81            syntax::syntaxes::LUA,
82            syntax::syntaxes::TSX,
83        ],
84    },
85    SyntaxAsset {
86        source: syntax::syntaxes::PYTHON,
87        file_types: &["py", "pyw"],
88        dependencies: &[],
89    },
90    SyntaxAsset {
91        source: syntax::syntaxes::RUBY,
92        file_types: &["rb", "rake", "gemspec"],
93        dependencies: &[],
94    },
95    SyntaxAsset {
96        source: syntax::syntaxes::RUST,
97        file_types: &["rs"],
98        dependencies: &[],
99    },
100    SyntaxAsset {
101        source: syntax::syntaxes::SHELL,
102        file_types: &["sh", "bash", "zsh"],
103        dependencies: &[],
104    },
105    SyntaxAsset {
106        source: syntax::syntaxes::SQL,
107        file_types: &["sql"],
108        dependencies: &[],
109    },
110    SyntaxAsset {
111        source: syntax::syntaxes::TOML,
112        file_types: &["toml"],
113        dependencies: &[],
114    },
115    SyntaxAsset {
116        source: syntax::syntaxes::TYPESCRIPT,
117        file_types: &["ts", "cts", "mts"],
118        dependencies: &[],
119    },
120    SyntaxAsset {
121        source: syntax::syntaxes::TSX,
122        file_types: &["tsx"],
123        dependencies: &[],
124    },
125    SyntaxAsset {
126        source: syntax::syntaxes::XML,
127        file_types: &["svg", "xml"],
128        dependencies: &[],
129    },
130    SyntaxAsset {
131        source: syntax::syntaxes::YAML,
132        file_types: &["yaml", "yml"],
133        dependencies: &[],
134    },
135];
136
137/// Built-in highlight assets loaded for one diff document.
138pub struct Builtins {
139    /// Diff chrome palette derived from the built-in theme.
140    pub palette: DiffPalette,
141}
142
143impl Builtins {
144    /// Loads the built-in diff chrome assets.
145    pub fn load() -> Result<Self, syntax::Error> {
146        let raw_theme = THEME.parse::<syntax::RawTheme>()?;
147
148        Ok(Self {
149            palette: (&raw_theme).into(),
150        })
151    }
152
153    /// Loads a highlighter with grammars relevant to `document`.
154    pub fn highlighter(document: &DiffDocument) -> Result<BuiltinHighlighter, syntax::Error> {
155        Self::highlighter_for_paths(&Self::document_paths(document))
156    }
157
158    /// Loads a highlighter with grammars relevant to the given changed paths.
159    pub(crate) fn highlighter_for_paths(
160        paths: &[PathBuf],
161    ) -> Result<BuiltinHighlighter, syntax::Error> {
162        let raw_theme = THEME.parse::<syntax::RawTheme>()?;
163        let mut registry = syntax::Registry::default();
164
165        registry.add_theme(syntax::Theme::compile(&raw_theme));
166        for syntax in SYNTAXES
167            .iter()
168            .filter(|syntax| paths.iter().any(|path| syntax.matches_path(path)))
169        {
170            registry.add_grammar(syntax.grammar()?);
171        }
172        for grammar in custom_grammars() {
173            registry.add_grammar(grammar);
174        }
175        Ok(BuiltinHighlighter::new(registry, THEME_NAME))
176    }
177
178    /// Returns the old and new paths of every file in `document`.
179    pub(crate) fn document_paths(document: &DiffDocument) -> Vec<PathBuf> {
180        document
181            .files
182            .iter()
183            .flat_map(|file| [&file.header.old_path, &file.header.new_path])
184            .flatten()
185            .cloned()
186            .collect()
187    }
188
189    /// Loads grammars selected by a source file's head line.
190    pub fn load_head_grammars(
191        highlighter: &mut BuiltinHighlighter,
192        head: &str,
193    ) -> Result<(), syntax::Error> {
194        if !head.starts_with("#!") {
195            return Ok(());
196        }
197        for syntax in &SYNTAXES {
198            if let Some(grammar) = syntax.head_grammar(head)? {
199                highlighter.ensure_grammar(grammar);
200            }
201        }
202        Ok(())
203    }
204}
205/// Loads valid user-defined TextMate grammars from the configuration directory.
206fn custom_grammars() -> Vec<syntax::Grammar> {
207    let Some(directory) = config_dir().map(|directory| directory.join(CUSTOM_SYNTAX_DIR)) else {
208        return Vec::new();
209    };
210    let Ok(entries) = fs::read_dir(directory) else {
211        return Vec::new();
212    };
213    let paths = entries
214        .flatten()
215        .map(|entry| entry.path())
216        .filter(|path| {
217            path.extension()
218                .and_then(|extension| extension.to_str())
219                .is_some_and(|extension| extension.eq_ignore_ascii_case("json"))
220        })
221        .collect::<Vec<_>>();
222
223    let raw = paths
224        .iter()
225        .filter_map(|path| fs::read_to_string(path).ok())
226        .filter_map(|source| source.parse::<syntax::RawGrammar>().ok())
227        .collect::<Vec<_>>();
228
229    raw.iter()
230        .filter_map(|grammar| syntax::Grammar::compile_with(grammar, &raw).ok())
231        .collect()
232}
233
234/// Returns the user's configuration directory.
235fn config_dir() -> Option<PathBuf> {
236    std::env::var_os("XDG_CONFIG_HOME")
237        .map(PathBuf::from)
238        .or_else(|| Some(PathBuf::from(std::env::var_os("HOME")?).join(".config")))
239}
240
241/// Built-in syntax source and path aliases.
242struct SyntaxAsset {
243    /// TextMate grammar JSON source.
244    source: &'static str,
245    /// File names and extensions recognized by the registry.
246    file_types: &'static [&'static str],
247    /// Raw grammars required by cross-grammar includes in this syntax.
248    dependencies: &'static [&'static str],
249}
250
251impl SyntaxAsset {
252    /// Returns whether this syntax matches a file path.
253    fn matches_path(&self, path: &Path) -> bool {
254        let file_name = path.file_name().and_then(|name| name.to_str());
255        let extension = path.extension().and_then(|extension| extension.to_str());
256
257        self.file_types.iter().any(|file_type| {
258            file_name.is_some_and(|name| name.eq_ignore_ascii_case(file_type))
259                || extension.is_some_and(|extension| extension.eq_ignore_ascii_case(file_type))
260        })
261    }
262
263    /// Parses this grammar and attaches local path aliases.
264    fn grammar(&self) -> Result<syntax::Grammar, syntax::Error> {
265        let mut raw: syntax::RawGrammar = self.source.parse()?;
266        self.compile(&mut raw)
267    }
268
269    /// Parses this grammar when its first-line pattern matches `head`.
270    fn head_grammar(&self, head: &str) -> Result<Option<syntax::Grammar>, syntax::Error> {
271        let mut raw: syntax::RawGrammar = self.source.parse()?;
272        if raw.first_line_match.is_none() {
273            return Ok(None);
274        }
275        let grammar = self.compile(&mut raw)?;
276        Ok(grammar.matches_head(head).then_some(grammar))
277    }
278
279    /// Compiles `raw` with local path aliases and cross-grammar dependencies.
280    fn compile(&self, raw: &mut syntax::RawGrammar) -> Result<syntax::Grammar, syntax::Error> {
281        for file_type in self.file_types {
282            if !raw
283                .file_types
284                .iter()
285                .any(|existing| existing.eq_ignore_ascii_case(file_type))
286            {
287                raw.file_types.push((*file_type).to_owned());
288            }
289        }
290        let dependencies = self
291            .dependencies
292            .iter()
293            .map(|source| source.parse())
294            .collect::<Result<Vec<syntax::RawGrammar>, syntax::Error>>()?;
295        syntax::Grammar::compile_with(raw, &dependencies)
296    }
297}
298
299#[cfg(test)]
300#[allow(clippy::unwrap_used)]
301mod tests {
302    use super::*;
303    use crate::{Decode as _, DiffSide, HighlightKey, HighlightKeyInput, HighlightRequest};
304    use rustik_tui::Color;
305
306    #[test]
307    fn syntax_assets_match_common_source_paths() {
308        for path in [
309            "main.go",
310            "app.js",
311            "server.mjs",
312            "Makefile",
313            "rules.mk",
314            "index.html.erb",
315            "config.fish",
316            "init.lua",
317            "script.py",
318            "task.rb",
319            "build.sh",
320            "Cargo.toml",
321            "schema.sql",
322            "component.tsx",
323            "document.xml",
324            "icon.svg",
325            "page.xhtml",
326            "config.yml",
327        ] {
328            assert!(
329                SYNTAXES
330                    .iter()
331                    .any(|syntax| syntax.matches_path(path.as_ref())),
332                "no built-in syntax matched {path}"
333            );
334        }
335    }
336
337    #[test]
338    fn scripting_assets_match_shebangs() {
339        for (head, source) in [
340            ("#!/usr/bin/env ruby3.3", syntax::syntaxes::RUBY),
341            ("#!/usr/bin/env python3.11", syntax::syntaxes::PYTHON),
342            ("#!/bin/sh", syntax::syntaxes::SHELL),
343            ("#!/usr/bin/env -S bash -e", syntax::syntaxes::SHELL),
344            ("#!/usr/bin/env fish", syntax::syntaxes::FISH),
345        ] {
346            assert!(
347                SYNTAXES.iter().any(|syntax| syntax.source == source
348                    && syntax.head_grammar(head).unwrap().is_some()),
349                "no built-in syntax matched {head}"
350            );
351        }
352    }
353
354    #[test]
355    fn scripting_assets_highlight_extensionless_full_source_shebangs() {
356        for (head, line, token) in [
357            ("#!/usr/bin/env ruby", "if true", "if"),
358            ("#!/usr/bin/env python3", "if True:", "if"),
359            ("#!/bin/sh", "if true; then", "if"),
360            ("#!/usr/bin/env fish", "if test -n \"$argv\"", "if"),
361        ] {
362            let result = highlight_extensionless_full_source(head, line);
363
364            assert!(
365                result
366                    .line
367                    .labels()
368                    .iter()
369                    .any(|label| label.content() == token
370                        && label.paint.style.fg_color() != Color::Unset),
371                "{:?}",
372                result.line
373            );
374        }
375    }
376
377    fn highlight_extensionless_full_source(head: &str, line: &str) -> crate::HighlightResult {
378        let patch = format!(
379            "diff --git a/script b/script\n\
380             index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100755\n\
381             --- a/script\n\
382             +++ b/script\n\
383             @@ -10,1 +10,1 @@\n\
384             -old\n\
385             +{line}\n"
386        );
387        let document = DiffDocument::decode_str(&patch).unwrap();
388        let file = &document.files[0];
389        let source = crate::HighlightSource::new(format!("{head}\n\n\n\n\n\n\n\n\n{line}"), 1);
390        let text = source.line(10).unwrap();
391        let request = HighlightRequest {
392            key: HighlightKey::new(HighlightKeyInput {
393                file: file.id,
394                side: DiffSide::New,
395                source_line: 10,
396                text,
397                syntax: None,
398                theme: "auto",
399            }),
400            hunk: file.hunks[0].id,
401            row: file.hunks[0].lines[1].id,
402            path: file.source_path(DiffSide::New),
403            source,
404        };
405        let mut highlighter = Builtins::highlighter(&document).unwrap();
406
407        Builtins::load_head_grammars(&mut highlighter, head).unwrap();
408        highlighter.highlight(request)
409    }
410
411    #[test]
412    fn added_sources_highlight_by_path() {
413        for (path, line, token) in [
414            ("init.lua", "local ok = true", "local"),
415            ("config.fish", "if test -n \"$argv\"", "if"),
416            (
417                "component.tsx",
418                "const view = <Button disabled />;",
419                "Button",
420            ),
421            ("document.xml", "<item enabled=\"true\" />", "item"),
422            ("icon.svg", "<circle cx=\"8\" cy=\"8\" r=\"4\" />", "circle"),
423            ("page.xhtml", "<strong>hello</strong>", "strong"),
424        ] {
425            let result = highlight_path_source(path, line);
426
427            assert!(
428                result
429                    .line
430                    .labels()
431                    .iter()
432                    .any(|label| label.content() == token
433                        && label.paint.style.fg_color() != Color::Unset),
434                "{:?}",
435                result.line
436            );
437        }
438    }
439
440    fn highlight_path_source(path: &str, line: &str) -> crate::HighlightResult {
441        let patch = format!(
442            "diff --git a/{path} b/{path}\n\
443             index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644\n\
444             --- a/{path}\n\
445             +++ b/{path}\n\
446             @@ -1,1 +1,1 @@\n\
447             -old\n\
448             +{line}\n"
449        );
450        let document = DiffDocument::decode_str(&patch).unwrap();
451        let file = &document.files[0];
452        let source = crate::HighlightSource::new(line.to_owned(), 1);
453        let text = source.line(1).unwrap();
454        let request = HighlightRequest {
455            key: HighlightKey::new(HighlightKeyInput {
456                file: file.id,
457                side: DiffSide::New,
458                source_line: 1,
459                text,
460                syntax: None,
461                theme: "auto",
462            }),
463            hunk: file.hunks[0].id,
464            row: file.hunks[0].lines[1].id,
465            path: file.source_path(DiffSide::New),
466            source,
467        };
468        let mut highlighter = Builtins::highlighter(&document).unwrap();
469
470        highlighter.highlight(request)
471    }
472}