crates/highlight/src/registry.rs 3.0 KiB raw
1
//! Grammar and theme registry.
2
//!
3
//! A registry is the lookup layer between user-facing names, file paths, head
4
//! lines, and the compiled grammars/themes used by the highlighter. It keeps
5
//! inference policy out of tokenization so callers can load syntaxes once and
6
//! cheaply select the right one for each buffer.
7
8
use std::path::Path;
9
10
use crate::Error;
11
use crate::grammar::{Grammar, PLAIN_TEXT_NAME};
12
use crate::theme::Theme;
13
14
/// A reusable registry of compiled grammars and themes.
15
#[derive(Debug)]
16
pub struct Registry {
17
    /// Registered grammars.
18
    pub grammars: Vec<Grammar>,
19
    /// Registered themes.
20
    pub themes: Vec<Theme>,
21
}
22
23
/// Inputs used to select a grammar from a registry.
24
#[derive(Clone, Copy, Debug, Default)]
25
pub struct GrammarQuery<'a> {
26
    /// Explicit syntax name, root scope, extension, or file-type alias.
27
    pub syntax: Option<&'a str>,
28
    /// Source path used for file-name and extension inference.
29
    pub path: Option<&'a Path>,
30
    /// Head source line used for shebang and modeline inference.
31
    pub head: Option<&'a str>,
32
}
33
34
impl Registry {
35
    /// Creates an empty registry.
36
    pub fn new() -> Self {
37
        Self {
38
            grammars: Vec::new(),
39
            themes: Vec::new(),
40
        }
41
    }
42
43
    /// Adds a compiled grammar.
44
    pub fn add_grammar(&mut self, grammar: Grammar) {
45
        self.grammars.push(grammar);
46
    }
47
48
    /// Adds a compiled theme.
49
    pub fn add_theme(&mut self, theme: Theme) {
50
        self.themes.push(theme);
51
    }
52
53
    /// Finds a grammar by name, root scope, extension, or file-type alias.
54
    pub fn grammar_by_name(&self, name: &str) -> Result<&Grammar, Error> {
55
        self.grammars
56
            .iter()
57
            .find(|grammar| grammar.matches_name(name))
58
            .ok_or_else(|| Error::UnknownSyntax(name.to_owned()))
59
    }
60
61
    /// Finds a theme by name.
62
    pub fn theme_by_name(&self, name: &str) -> Result<&Theme, Error> {
63
        self.themes
64
            .iter()
65
            .find(|theme| theme.name.eq_ignore_ascii_case(name))
66
            .ok_or_else(|| Error::UnknownTheme(name.to_owned()))
67
    }
68
69
    /// Infers a grammar from explicit syntax, path, extension, and head line.
70
    pub fn query(&self, query: GrammarQuery<'_>) -> Result<&Grammar, Error> {
71
        if let Some(syntax) = query.syntax {
72
            return self.grammar_by_name(syntax);
73
        }
74
        if let Some(path) = query.path
75
            && let Some(grammar) = self
76
                .grammars
77
                .iter()
78
                .find(|grammar| grammar.matches_path(path))
79
        {
80
            return Ok(grammar);
81
        }
82
        if let Some(head) = query.head
83
            && let Some(grammar) = self
84
                .grammars
85
                .iter()
86
                .find(|grammar| grammar.matches_head(head))
87
        {
88
            return Ok(grammar);
89
        }
90
        self.grammar_by_name(PLAIN_TEXT_NAME)
91
    }
92
}
93
94
impl Default for Registry {
95
    fn default() -> Self {
96
        let mut registry = Self::new();
97
        registry.add_grammar(Grammar::plain_text());
98
        registry.add_grammar(Grammar::json());
99
        registry
100
    }
101
}