pub struct Highlighter<B: Backend> { /* private fields */ }Expand description
A highlighter with a parser backend and a language registry.
Queries are compiled during registration or on first use, then reused. Results borrow the source and capture names, not the temporary syntax trees. One-shot highlighting releases each layer’s tree before parsing the next layer.
Implementations§
Source§impl<B: Backend> Highlighter<B>
impl<B: Backend> Highlighter<B>
Sourcepub fn load(
&mut self,
id: &str,
aliases: &[&str],
language: B::Language,
queries: Queries<'_>,
) -> Result<(), Error>
pub fn load( &mut self, id: &str, aliases: &[&str], language: B::Language, queries: Queries<'_>, ) -> Result<(), Error>
Register a language and compile its queries.
IDs and aliases share one namespace. Registration leaves the registry unchanged if validation or compilation fails. Query sources are borrowed only during loading.
§Errors
Returns Error::DuplicateLanguage for empty or conflicting names, or a
backend/query error if compilation fails.
Sourcepub fn load_lazy(
&mut self,
id: &str,
aliases: &[&str],
language: B::Language,
queries: Queries<'static>,
) -> Result<(), Error>
pub fn load_lazy( &mut self, id: &str, aliases: &[&str], language: B::Language, queries: Queries<'static>, ) -> Result<(), Error>
Register static queries to compile when the language is first used. Documents share uncompiled query sources and compiled queries with this registry. Query compilation errors are reported when the language is first used.
§Errors
Returns Error::DuplicateLanguage for empty or conflicting names.
Sourcepub fn contains(&self, name: &str) -> bool
pub fn contains(&self, name: &str) -> bool
Return whether a language ID or alias is registered.
Sourcepub fn highlight<'a>(
&'a mut self,
source: &'a str,
name: &str,
config: &HighlightConfig,
) -> Result<Highlighted<'a>, Error>
pub fn highlight<'a>( &'a mut self, source: &'a str, name: &str, config: &HighlightConfig, ) -> Result<Highlighted<'a>, Error>
Highlight source text using a registered language ID or alias.
Injections into unregistered languages are skipped. Each syntax layer is parsed once. Results borrow the source and capture names from this registry.
§Errors
Returns an error for an unloaded root language, a parser or query failure, an invalid UTF-8 range, or injection nesting beyond 32 layers.