pub struct Buffer { /* private fields */ }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
impl Buffer
Sourcepub fn new(
language: Language,
source: impl ToString,
) -> Result<Self, HighlightError>
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() {}");Sourcepub fn replace(&mut self, source: impl ToString) -> Result<(), HighlightError>
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() {}");Sourcepub fn edit(
&mut self,
edit: SourceEdit,
new_source: impl ToString,
) -> Result<(), HighlightError>
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 }");Sourcepub fn set_language(&mut self, language: Language) -> Result<(), HighlightError>
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);Sourcepub fn spans(&self) -> &[HighlightSpan]
pub fn spans(&self) -> &[HighlightSpan]
Highlight spans covering the current source.
Sourcepub fn segments(&self) -> Vec<HighlightSegment<'_>>
pub fn segments(&self) -> Vec<HighlightSegment<'_>>
Split the source into renderable highlighted segments.
Sourcepub fn lines(&self) -> Vec<Vec<HighlightSegment<'_>>>
pub fn lines(&self) -> Vec<Vec<HighlightSegment<'_>>>
Split the source into highlighted lines, preserving trailing empty lines.
Sourcepub fn highlighted(&self) -> HighlightedSource
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.