Skip to main content

Highlighter

Struct Highlighter 

Source
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>

Source

pub fn new(backend: B) -> Self

Create an empty registry using the supplied backend.

Source

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.

Source

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.

Source

pub fn contains(&self, name: &str) -> bool

Return whether a language ID or alias is registered.

Source

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.

Source

pub fn create_document( &self, name: &str, source: &str, backend: B, ) -> Result<Document<B>, Error>

Create an independent document using a fresh backend and shared compiled queries.

Auto Trait Implementations§

§

impl<B> Forget for Highlighter<B>

§

impl<B> Freeze for Highlighter<B>
where B: Freeze, Registry<B>: Freeze,

§

impl<B> Move for Highlighter<B>

§

impl<B> RefUnwindSafe for Highlighter<B>
where B: RefUnwindSafe, Registry<B>: RefUnwindSafe,

§

impl<B> Send for Highlighter<B>
where B: Send, Registry<B>: Send,

§

impl<B> Sync for Highlighter<B>
where B: Sync, Registry<B>: Sync,

§

impl<B> Unpin for Highlighter<B>
where B: Unpin, Registry<B>: Unpin,

§

impl<B> UnsafeUnpin for Highlighter<B>
where B: UnsafeUnpin, Registry<B>: UnsafeUnpin,

§

impl<B> UnwindSafe for Highlighter<B>
where B: UnwindSafe, Registry<B>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.