Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer { /* private fields */ }
Available on crate feature runtime only.
Expand description

A live (text, grammar) pair you can edit, reparse, and snapshot.

Buffer owns the source string, the parse tree, and the highlight spans as a single coherent unit. edit applies an incremental edit (reusing the cached parse tree); replace swaps the source wholesale; set_language switches grammars and reparses. After any successful mutation, source, spans, and lines reflect the new state.

Available with the runtime feature. Hold one per editor instance (e.g. inside use_hook). Highlighting operations return HighlightError when grammar setup or parsing fails.

use dioxus_code::Language;
use dioxus_code::advanced::Buffer;
let buffer = Buffer::new(Language::Rust, "fn main() {}").expect("rust grammar loads");
assert_eq!(buffer.language(), Language::Rust);
assert!(!buffer.spans().is_empty());

Implementations§

Source§

impl Buffer

Source

pub fn new( language: Language, source: impl ToString, ) -> Result<Self, HighlightError>

Create a buffer for language, parse source, and collect spans.

use dioxus_code::Language;
use dioxus_code::advanced::Buffer;
let buffer = Buffer::new(Language::Rust, "fn main() {}").expect("rust grammar loads");
assert_eq!(buffer.source(), "fn main() {}");
Source

pub fn replace(&mut self, source: impl ToString) -> Result<(), HighlightError>

Replace the source wholesale and reparse from scratch.

Use this when the new text is unrelated to the old (e.g. loading a different file). For keystroke-level edits, prefer edit — it reuses the cached parse tree.

use dioxus_code::Language;
use dioxus_code::advanced::Buffer;
let mut buffer = Buffer::new(Language::Rust, "fn old() {}").expect("rust grammar loads");
buffer.replace("fn new() {}").expect("rust parses");
assert_eq!(buffer.source(), "fn new() {}");
Source

pub fn edit( &mut self, edit: SourceEdit, new_source: impl ToString, ) -> Result<(), HighlightError>

Apply an incremental edit and reparse, reusing the cached parse tree.

new_source must be the full text after the edit. edit describes the byte range that changed — its start_byte / old_end_byte index into the buffer’s previous source, and new_end_byte indexes into new_source. If the edit is malformed, HighlightError::InvalidEdit is returned and the buffer is left unchanged. Validation is limited to bounds and UTF-8 character boundaries; callers are responsible for passing an edit range that matches the unchanged prefix and suffix.

use dioxus_code::Language;
use dioxus_code::advanced::{Buffer, SourceEdit};
let mut buffer = Buffer::new(Language::Rust, "fn main() { 1 }").expect("rust grammar loads");
buffer.edit(
    SourceEdit { start_byte: 12, old_end_byte: 13, new_end_byte: 14 },
    "fn main() { 22 }",
).expect("rust parses");
assert_eq!(buffer.source(), "fn main() { 22 }");
Source

pub fn set_language(&mut self, language: Language) -> Result<(), HighlightError>

Switch grammars and reparse the current source.

No-op when the new language matches the current one.

use dioxus_code::Language;
use dioxus_code::advanced::Buffer;
let mut buffer = Buffer::new(Language::Rust, "fn main() {}").expect("rust grammar loads");
buffer.set_language(Language::Rust).expect("rust grammar loads");
assert_eq!(buffer.language(), Language::Rust);
Source

pub fn source(&self) -> &str

The current source text.

Source

pub const fn language(&self) -> Language

The grammar this buffer is parsing with.

Source

pub fn spans(&self) -> &[HighlightSpan]

Highlight spans covering the current source.

Source

pub fn segments(&self) -> Vec<HighlightSegment<'_>>

Split the source into renderable highlighted segments.

Source

pub fn lines(&self) -> Vec<Vec<HighlightSegment<'_>>>

Split the source into highlighted lines, preserving trailing empty lines.

Source

pub fn highlighted(&self) -> HighlightedSource

Snapshot the buffer as an immutable HighlightedSource.

Useful for handing off to Code() or any consumer that takes the frozen snapshot type.

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

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
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<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
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