Skip to main content

SyntaxLayer

Struct SyntaxLayer 

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

Per-App syntax highlighting layer. Multiplexes per-buffer state (helix-style): each open buffer carries its own retained tree (worker-side) plus source-cache and edit queue (here). One worker thread serves all buffers.

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

§last_perf: PerfBreakdown

Last perf breakdown received via take_all_results. Updated on every successful drain.

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, the given theme, and the given language directory.

§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_language_for_path( &mut self, id: BufferId, path: &Path, ) -> SetLanguageOutcome

Detect the language for path and ship it to the worker.

Non-blocking: uses the async grammar loader so opening a file with an uninstalled grammar no longer freezes the UI.

  • Ready — grammar cached/found on disk; installed immediately.
  • Loading — clone+compile kicked off; buffer renders as plain text until poll_pending_loads fires the companion 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 this once per tick from the main loop (alongside take_all_results) so completed loads install immediately without waiting for the next file open.

Returns one LoadEvent per handle that resolved during this tick. Non-empty results should trigger a redraw and re-submit render.

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 call will use the new theme.

Source

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

Synchronous viewport-only preview render. Builds a String containing only the visible rows, parses it from scratch with a one-shot Highlighter, runs highlight_range over the slice, and returns a RenderOutput whose spans table is padded with empty rows above the viewport so the install path indexes the right rows.

Returns None when no language is attached or when the viewport is empty.

Source

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

Ask the worker to drop this buffer’s retained tree on the next parse so the next submission is cold.

Source

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

Buffer a batch of engine ContentEdits to be shipped to the worker on the next submit_render. Translates the engine’s position pairs into tree_sitter::InputEdits up front.

Source

pub fn submit_render( &mut self, id: BufferId, buffer: &impl Query, viewport_top: usize, viewport_height: usize, kind: ParseKind, ) -> Option<u128>

Build (or reuse) the cached (source, row_starts) for the current buffer state and submit a parse + render job to the worker. Returns immediately. Drain the result with Self::take_all_results.

kind tags the request so the App can route the result to the correct per-slot cache field. Pass ParseKind::Viewport for normal scroll-driven parses.

Returns None and submits nothing when no language is attached. Returns Some(source_build_us) when a request was submitted — 0 means the cache was reused.

Source

pub fn take_result(&mut self) -> Option<RenderOutput>

Drain the most recent render result the worker has produced (if any). Older results are discarded — only the latest matters for install. Updates last_perf as a side effect.

Kept for use in perf-smoke tests; production code drains via Self::take_all_results so multi-buffer results are routed.

Source

pub fn take_all_results(&mut self) -> Vec<RenderOutput>

Drain all render results the worker has produced since the last drain (one per (buffer_id, kind) that completed). Updates last_perf from the last result if any are present.

Source

pub fn wait_result(&mut self, timeout: Duration) -> Option<RenderOutput>

Block up to timeout for the worker’s next result, then drain any others that arrived after it.

Source

pub fn wait_all_results(&mut self, timeout: Duration) -> Vec<RenderOutput>

Block up to timeout for the first result, then drain ALL available results in arrival order (per-(buffer_id, kind) coalesced). Used for big-jump paths that submit the active buffer’s parse AND pre-warms for other open buffers in the same tick.

Source

pub fn wait_for_initial_result( &mut self, timeout: Duration, ) -> Option<RenderOutput>

Synchronously drain the next result, blocking up to timeout. Returns None on timeout. Used at startup so the very first frame can paint with highlights when the worker is fast enough.

Source

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

Dispatch a LoadEvent through a caller-supplied handler.

The handler receives each known variant as an exhaustive inner enum so consumers never need a _ => {} wildcard arm for LoadEvent’s #[non_exhaustive] restriction. Unknown future variants are silently ignored (this method is updated when new variants land).

Returns true when the event was dispatched to a known variant, false when it was an unknown future variant and the handler was not called.

§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);
Source

pub fn dispatch_parse_kind( kind: ParseKind, handler: impl FnMut(ParseKindKind), ) -> bool

Dispatch a ParseKind value through a caller-supplied handler.

Eliminates _ => {} wildcards in consumer match arms by providing an exhaustive inner enum. Unknown future variants fall back to the ParseKind::Viewport path (conservative: treat unknown as viewport).

Returns true for known variants, false for unknown ones (and calls the handler with ParseKindKind::Viewport as the fallback).

§Examples
use hjkl_syntax::{ParseKind, ParseKindKind, SyntaxLayer};

let known = SyntaxLayer::dispatch_parse_kind(ParseKind::Top, |k| {
    assert_eq!(k, ParseKindKind::Top);
});
assert!(known);

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