diff: Support custom grammars
4b4a734b989c5613429858dc64834c085976cd15
1 parent
331dcf85
crates/diff/src/builtins.rs
+41 -0
| 3 | 3 | //! This module bundles a default syntax theme and a small set of common |
|
| 4 | 4 | //! TextMate grammars, compiling them into a [`BuiltinHighlighter`] selected for |
|
| 5 | 5 | //! the files in a diff. It is gated behind the `builtins` feature so callers |
|
| 6 | 6 | //! that bring their own highlighting do not pay for the embedded assets. |
|
| 7 | 7 | ||
| 8 | + | use std::fs; |
|
| 8 | 9 | use std::path::{Path, PathBuf}; |
|
| 9 | 10 | ||
| 10 | 11 | use rustik_highlight as syntax; |
|
| 11 | 12 | ||
| 12 | 13 | use crate::{BuiltinHighlighter, DiffDocument, DiffPalette}; |
|
| 13 | 14 | ||
| 14 | 15 | /// Theme name used for built-in diff highlighting. |
|
| 15 | 16 | const THEME_NAME: &str = "rustik-catppuccin-mocha"; |
|
| 16 | 17 | /// Built-in syntax theme JSON. |
|
| 17 | 18 | const THEME: &str = include_str!("themes/rustik-catppuccin-mocha.json"); |
|
| 19 | + | /// Relative directory containing user-defined TextMate grammars. |
|
| 20 | + | const CUSTOM_SYNTAX_DIR: &str = "rustik/highlight/grammars"; |
|
| 18 | 21 | /// Common syntax grammars loaded by the interactive pager. |
|
| 19 | 22 | const SYNTAXES: [SyntaxAsset; 20] = [ |
|
| 20 | 23 | SyntaxAsset { |
|
| 21 | 24 | source: syntax::syntaxes::C, |
|
| 22 | 25 | file_types: &["c", "h"], |
| 164 | 167 | .iter() |
|
| 165 | 168 | .filter(|syntax| paths.iter().any(|path| syntax.matches_path(path))) |
|
| 166 | 169 | { |
|
| 167 | 170 | registry.add_grammar(syntax.grammar()?); |
|
| 168 | 171 | } |
|
| 172 | + | for grammar in custom_grammars() { |
|
| 173 | + | registry.add_grammar(grammar); |
|
| 174 | + | } |
|
| 169 | 175 | Ok(BuiltinHighlighter::new(registry, THEME_NAME)) |
|
| 170 | 176 | } |
|
| 171 | 177 | ||
| 172 | 178 | /// Returns the old and new paths of every file in `document`. |
|
| 173 | 179 | pub(crate) fn document_paths(document: &DiffDocument) -> Vec<PathBuf> { |
| 194 | 200 | } |
|
| 195 | 201 | } |
|
| 196 | 202 | Ok(()) |
|
| 197 | 203 | } |
|
| 198 | 204 | } |
|
| 205 | + | /// Loads valid user-defined TextMate grammars from the configuration directory. |
|
| 206 | + | fn 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. |
|
| 235 | + | fn 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 | + | } |
|
| 199 | 240 | ||
| 200 | 241 | /// Built-in syntax source and path aliases. |
|
| 201 | 242 | struct SyntaxAsset { |
|
| 202 | 243 | /// TextMate grammar JSON source. |
|
| 203 | 244 | source: &'static str, |
script\n\
+0 -0
{path}\n\
+0 -0