Skip to main content

SyntaxLayer

Struct SyntaxLayer 

Source
pub struct SyntaxLayer {
    pub directory: Arc<LanguageDirectory>,
    /* private fields */
}
Expand description

Per-App syntax highlighting layer. Multiplexes per-buffer state. Fully synchronous — no background thread.

§Examples

use std::sync::Arc;
use hjkl_syntax::SyntaxLayer;
use hjkl_bonsai::DotFallbackTheme;
use hjkl_lang::LanguageDirectory;

let theme = Arc::new(DotFallbackTheme::dark());
let dir = Arc::new(LanguageDirectory::new().unwrap());
let layer = SyntaxLayer::new(theme, dir);

Fields§

§directory: Arc<LanguageDirectory>

Shared grammar resolver.

Implementations§

Source§

impl SyntaxLayer

Source

pub fn new( theme: Arc<dyn Theme + Send + Sync>, directory: Arc<LanguageDirectory>, ) -> Self

Create a new layer with no buffers attached.

§Examples
use std::sync::Arc;
use hjkl_syntax::SyntaxLayer;
use hjkl_bonsai::DotFallbackTheme;
use hjkl_lang::LanguageDirectory;

let theme = Arc::new(DotFallbackTheme::dark());
let dir = Arc::new(LanguageDirectory::new().unwrap());
let layer = SyntaxLayer::new(theme, dir);
Source

pub fn set_rainbow_brackets(&mut self, enabled: bool)

Update rainbow bracket settings. Pass enabled = false to disable the rainbow overlay globally. No-op when the value is unchanged so per-frame pushes from the app stay cheap. Caches invalidate only on actual change.

Source

pub fn set_colorizer(&mut self, enabled: bool, filetypes: Vec<String>)

Update colorizer settings. Pass enabled = false to disable the color-literal overlay globally. filetypes is the allowlist of language names (e.g. "css", "toml"); an empty slice means no filetype is allowed (same effect as enabled = false).

No-op when the values are unchanged so per-frame pushes from the app stay cheap. Caches invalidate only on actual change.

Source

pub fn directory(&self) -> &Arc<LanguageDirectory>

Borrow the shared language directory.

Source

pub fn set_language_for_path( &mut self, id: BufferId, path: &Path, ) -> SetLanguageOutcome

Detect the language for path and attach a grammar.

  • Ready — grammar cached; highlighter installed immediately.
  • Loading — grammar compiling; renders as plain text until poll_pending_loads fires LoadEvent::Ready.
  • Unknown — unrecognized extension; plain text only.
§Examples
use std::sync::Arc;
use std::path::Path;
use hjkl_syntax::{SyntaxLayer, SetLanguageOutcome};
use hjkl_bonsai::DotFallbackTheme;
use hjkl_lang::LanguageDirectory;

let theme = Arc::new(DotFallbackTheme::dark());
let dir = Arc::new(LanguageDirectory::new().unwrap());
let mut layer = SyntaxLayer::new(theme, dir);
let outcome = layer.set_language_for_path(0, Path::new("a.zzz_not_real"));
assert!(!outcome.is_known());
Source

pub fn set_language_by_name( &mut self, id: BufferId, name: &str, ) -> SetLanguageOutcome

Attach a grammar by its canonical language name, bypassing path detection entirely. The name may come from content detection (shebang, modeline ft=) or a manual :set filetype=.

Identical semantics to Self::set_language_for_path once the name is known; an unrecognised name resolves to SetLanguageOutcome::Unknown without attaching anything.

Source

pub fn poll_pending_loads(&mut self) -> Vec<LoadEvent>

Poll all in-flight grammar loads. Call once per tick.

Returns one LoadEvent per handle that resolved during this tick.

Source

pub fn forget(&mut self, id: BufferId)

Drop all state for a buffer. Call on close.

Source

pub fn set_theme(&mut self, theme: Arc<dyn Theme + Send + Sync>)

Swap the active theme. Next render_viewport call uses the new theme.

Source

pub fn apply_edits(&mut self, id: BufferId, edits: &[ContentEdit])

Apply a batch of engine ContentEdits to the buffer’s retained tree synchronously. The cache will be invalidated on the next render_viewport call via dirty_gen mismatch.

No-op when no grammar is attached.

Source

pub fn reset(&mut self, id: BufferId)

Drop the buffer’s retained tree. Next render_viewport reparses from scratch.

Call on :e! / content reset.

Source

pub fn extract_fold_ranges( &mut self, id: BufferId, buffer: &impl Query, ) -> Option<Vec<(usize, usize)>>

Extract fold ranges from the buffer’s retained tree using the bundled folds.scm for this grammar, plus one for each injected region using ITS language’s query.

Returns Some(ranges) when the grammar is attached and the tree has been parsed — ranges may be empty when the grammar has no bundled folds.scm or the file contains no foldable nodes.

Injected regions (a ```rust block in markdown, a <script> body in HTML) are parsed with their own grammar, folded with that language’s query, and their rows offset into this document — see hjkl_bonsai::extract_fold_ranges_rope_with_injections. Resolving an injected grammar goes through the shared LanguageDirectory, which may build one on first use; only languages with a bundled fold query can trigger that.

Returns None when:

  • No grammar is attached yet (grammar still loading or unknown extension).
  • No highlighter has been created for this buffer.
  • The tree has not been parsed yet (call render_viewport first).

Callers must treat None as “not ready — retry later” and must NOT record the dirty_gen as processed when None is returned. Returning Some(empty) is the signal that the grammar ran but produced no folds (e.g. no folds.scm for this language).

NOT viewport-bounded — runs over the full tree (once per reparse). Do not call this per-frame; call it only when dirty_gen has changed.

Source

pub fn render_viewport( &mut self, id: BufferId, buffer: &impl Query, viewport_top: usize, viewport_height: usize, ) -> Option<RenderOutput>

Render spans for the visible viewport, returning an owned span table.

Thin wrapper over Self::render_viewport_ref that deep-copies the cached rows. Callers that immediately convert the table into their own style type (every renderer adapter) should use render_viewport_ref instead and skip the copy.

Source

pub fn render_viewport_ref( &mut self, id: BufferId, buffer: &impl Query, viewport_top: usize, viewport_height: usize, ) -> Option<RenderOutputRef<'_>>

Render spans for the visible viewport. Fully synchronous.

  1. Returns None when no grammar is attached.
  2. Clears the cache when buffer.dirty_gen() has advanced.
  3. Returns cached rows when the request is fully inside the cached range.
  4. Walks only rows outside the cache (extend prefix/suffix), splices into cache_spans, extends cache_rows.

The returned RenderOutputRef borrows the viewport slice of cache_spans — no per-call copy of the span table.

Source

pub fn name_for_path(&self, path: &Path) -> Option<String>

Resolve a path to its language name without loading a grammar.

Source

pub fn dispatch_load_event( event: &LoadEvent, handler: impl FnMut(LoadEventKind<'_>), ) -> bool

Dispatch a LoadEvent through a caller-supplied handler.

§Examples
use hjkl_syntax::{LoadEvent, SyntaxLayer};

let event = LoadEvent::Ready { id: 0, name: "rust".into() };
let mut got_ready = false;
let handled = SyntaxLayer::dispatch_load_event(&event, |ev| {
    use hjkl_syntax::LoadEventKind;
    match ev {
        LoadEventKind::Ready { id, name } => { got_ready = true; }
        LoadEventKind::Failed { .. } => {}
    }
});
assert!(handled);
assert!(got_ready);

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more