Skip to main content

SyntaxHandle

Struct SyntaxHandle 

Source
pub struct SyntaxHandle { /* private fields */ }
Expand description

Editor-facing handle. Cheap to clone; cloning gives a reference to the same underlying snapshot cell + the same reparse-request channel.

Implementations§

Source§

impl SyntaxHandle

Source

pub fn spawn( lang: Lang, registry: Arc<LangRegistry>, ) -> Result<Option<Self>, SyntaxError>

Build a handle for lang and start the worker task. Returns Ok(None) for Lang::Plain or any language not registered in the supplied registry – the App treats None as “no syntax highlighting for this buffer”.

Source

pub fn seeded(syntax: Syntax) -> Self

Wrap a pre-built Syntax (already parsed or not) in a handle. Used by tests that want to drive parses synchronously and then read the result, and by callers that already constructed a Syntax for other reasons (one-shot help-buffer markdown highlighting).

Production callers must use Self::seeded_with_runtime instead. This method falls back to Handle::try_current() which silently fails when the caller hasn’t entered a tokio context (notably: editor startup runs from a synchronous main(), before the runtime is set up). When that fails, the worker is never spawned and request_reparse calls go to a dropped channel – the snapshot stays at the seeded state forever and no reparses ever run. LSP solved the same problem with an explicit runtime handle (see app.rs:96-100); this constructor remains for tests that already run inside a tokio context.

Source

pub fn seeded_with_runtime( syntax: Syntax, runtime: &Handle, on_publish: Option<Arc<dyn Fn() + Send + Sync + 'static>>, ) -> Self

Wrap a pre-built Syntax and spawn the worker on the supplied tokio runtime handle. This is the production constructor. Mirrors what lattice_lsp::supervisor::LspSupervisor::spawn does: take an explicit handle so the worker actually starts even when the caller hasn’t entered a tokio context yet (editor startup runs from synchronous main() and constructs the handle before the main loop enters tokio).

Without this, Handle::try_current() in Self::seeded silently fails, the worker is never spawned, and Option B’s entire incremental-reparse pipeline is dead – request_reparse sends to a dropped channel, the snapshot stays at the seeded state, and no edit ever produces a fresh tree. The user-visible symptom is “syntax highlighting is stuck to byte positions and never tracks document edits” – which was the bug originally reported against indent (>>) and backspace flows. on_publish, when Some, is called after every snapshot publish (both the intermediate edit-shift and the final reparse). Production passes a closure that fires the host’s async_landed Notify (waking the actor) and publishes a SyntaxReparsed event on the event bus; the event subscriber then fires cells_wake so idle reparses repaint without a keystroke. Tests pass None.

Source

pub fn snapshot(&self) -> Arc<SyntaxSnapshot> ⓘ

Wait-free read of the latest parse snapshot. Hot path; renderer / fold provider / completion call this on every frame.

Source

pub fn with_snapshot<R>(&self, f: impl FnOnce(&SyntaxSnapshot) -> R) -> R

Borrow the snapshot via ArcSwap’s short-lived guard. Cheaper than Self::snapshot when the caller only needs to read a single field (no Arc::clone).

Source

pub fn lang(&self) -> Lang

Convenience: latest Lang. Wait-free.

Source

pub fn request_reparse( &self, from_version: u64, text_version: u64, buffer: Buffer, edits: Vec<EditDelta>, )

Request that the worker reparse buffer and stamp the resulting snapshot with text_version. Fire-and-forget; the new snapshot is observable via Self::snapshot once the worker completes the parse.

Slice B.5: takes Buffer (clones in O(1) via ropey’s internal Arc) rather than String. The full source materialization moves to the worker, off the input thread.

from_version is the version-baseline the worker’s tree is expected to be at BEFORE applying edits. The worker uses it to detect mismatches (dropped requests, file load, document replace) and falls back to full reparse when the cached tree’s version doesn’t match.

edits carries the tree-sitter-shaped deltas in apply order, taking the buffer from from_version to text_version. Empty edits = “do a full reparse” (file load / cold-start path).

Coalesced: queued requests’ edits are accumulated in order; the latest queued request’s buffer and text_version win; the earliest queued request’s from_version survives so the burst’s baseline is preserved.

Trait Implementations§

Source§

impl Clone for SyntaxHandle

Source§

fn clone(&self) -> SyntaxHandle

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SyntaxHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ExcerptHighlighter for SyntaxHandle

Source§

fn highlight_lines(&self, lo: u32, hi: u32) -> Option<Vec<Vec<StyledSpan>>>

Return per-line styled spans for source rows lo..hi (exclusive). The returned Vec has exactly (hi - lo) entries; an empty inner Vec means “no spans on that line” (fall back to default fg). Returns None when the highlight snapshot is stale or unavailable.
Source§

fn highlight_version(&self) -> u64

Monotonic version of the last-published parse result. Used to build MatrixVersion::syntax without taking a dep on SyntaxHandle internals.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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