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: PerfBreakdownLast perf breakdown received via take_all_results. Updated on every
successful drain.
Implementations§
Source§impl SyntaxLayer
impl SyntaxLayer
Sourcepub fn new(
theme: Arc<dyn Theme + Send + Sync>,
directory: Arc<LanguageDirectory>,
) -> Self
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);Sourcepub fn set_language_for_path(
&mut self,
id: BufferId,
path: &Path,
) -> SetLanguageOutcome
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 untilpoll_pending_loadsfires the companionLoadEvent::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());Sourcepub fn poll_pending_loads(&mut self) -> Vec<LoadEvent>
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.
Sourcepub fn set_theme(&mut self, theme: Arc<dyn Theme + Send + Sync>)
pub fn set_theme(&mut self, theme: Arc<dyn Theme + Send + Sync>)
Swap the active theme. Next render call will use the new theme.
Sourcepub fn preview_render(
&self,
id: BufferId,
buffer: &impl Query,
viewport_top: usize,
viewport_height: usize,
) -> Option<RenderOutput>
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.
Sourcepub fn reset(&mut self, id: BufferId)
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.
Sourcepub fn apply_edits(&mut self, id: BufferId, edits: &[ContentEdit])
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.
Sourcepub fn submit_render(
&mut self,
id: BufferId,
buffer: &impl Query,
viewport_top: usize,
viewport_height: usize,
kind: ParseKind,
) -> Option<u128>
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.
Sourcepub fn take_result(&mut self) -> Option<RenderOutput>
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.
Sourcepub fn take_all_results(&mut self) -> Vec<RenderOutput>
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.
Sourcepub fn wait_result(&mut self, timeout: Duration) -> Option<RenderOutput>
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.
Sourcepub fn wait_all_results(&mut self, timeout: Duration) -> Vec<RenderOutput>
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.
Sourcepub fn wait_for_initial_result(
&mut self,
timeout: Duration,
) -> Option<RenderOutput>
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.
Sourcepub fn dispatch_load_event(
event: &LoadEvent,
handler: impl FnMut(LoadEventKind<'_>),
) -> bool
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);Sourcepub fn dispatch_parse_kind(
kind: ParseKind,
handler: impl FnMut(ParseKindKind),
) -> bool
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);