//! Grammar and theme registry. //! //! A registry is the lookup layer between user-facing names, file paths, head //! lines, and the compiled grammars/themes used by the highlighter. It keeps //! inference policy out of tokenization so callers can load syntaxes once and //! cheaply select the right one for each buffer. use std::path::Path; use crate::Error; use crate::grammar::{Grammar, PLAIN_TEXT_NAME}; use crate::theme::Theme; /// A reusable registry of compiled grammars and themes. #[derive(Debug)] pub struct Registry { /// Registered grammars. pub grammars: Vec, /// Registered themes. pub themes: Vec, } /// Inputs used to select a grammar from a registry. #[derive(Clone, Copy, Debug, Default)] pub struct GrammarQuery<'a> { /// Explicit syntax name, root scope, extension, or file-type alias. pub syntax: Option<&'a str>, /// Source path used for file-name and extension inference. pub path: Option<&'a Path>, /// Head source line used for shebang and modeline inference. pub head: Option<&'a str>, } impl Registry { /// Creates an empty registry. pub fn new() -> Self { Self { grammars: Vec::new(), themes: Vec::new(), } } /// Adds a compiled grammar. pub fn add_grammar(&mut self, grammar: Grammar) { self.grammars.push(grammar); } /// Adds a compiled theme. pub fn add_theme(&mut self, theme: Theme) { self.themes.push(theme); } /// Finds a grammar by name, root scope, extension, or file-type alias. pub fn grammar_by_name(&self, name: &str) -> Result<&Grammar, Error> { self.grammars .iter() .find(|grammar| grammar.matches_name(name)) .ok_or_else(|| Error::UnknownSyntax(name.to_owned())) } /// Finds a theme by name. pub fn theme_by_name(&self, name: &str) -> Result<&Theme, Error> { self.themes .iter() .find(|theme| theme.name.eq_ignore_ascii_case(name)) .ok_or_else(|| Error::UnknownTheme(name.to_owned())) } /// Infers a grammar from explicit syntax, path, extension, and head line. pub fn query(&self, query: GrammarQuery<'_>) -> Result<&Grammar, Error> { if let Some(syntax) = query.syntax { return self.grammar_by_name(syntax); } if let Some(path) = query.path && let Some(grammar) = self .grammars .iter() .find(|grammar| grammar.matches_path(path)) { return Ok(grammar); } if let Some(head) = query.head && let Some(grammar) = self .grammars .iter() .find(|grammar| grammar.matches_head(head)) { return Ok(grammar); } self.grammar_by_name(PLAIN_TEXT_NAME) } } impl Default for Registry { fn default() -> Self { let mut registry = Self::new(); registry.add_grammar(Grammar::plain_text()); registry.add_grammar(Grammar::json()); registry } }