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 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 render_viewport( &mut self, id: BufferId, buffer: &impl Query, viewport_top: usize, viewport_height: usize, ) -> Option<RenderOutput>

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.
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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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