Skip to content

CodeBlock

Selectable, caret-free source code with Tree-sitter coloring and virtualized logical rows.

No language grammars are included by default. Register a linked grammar in Rust, or load one portable pack of selected Tree-sitter Wasm grammars and queries using loadLanguagePack (TypeScript) / LoadLanguagePack (Go).

Unstyled by default: text and line-number colors inherit, and the component adds no background, border, rounding, padding, or syntax/diff palette. Supply application styles and explicit colors to opt into an appearance.

#Import

Enable QuickGUI's opt-in editor crate feature and import the retained code-block types.

use quickgui::{CodeBlock, CodeBlockStyle, SyntaxLanguage};

#Usage

let code = CodeBlock::with_text("pub fn answer() -> u8 { 42 }\n")
    .with_language(SyntaxLanguage::Rust)
    .with_style(CodeBlockStyle::default().line_numbers(true));

code.element("example-code").h(280.0)

#Syntax highlighting

SyntaxLanguage selects a registered Tree-sitter grammar and its highlight query. Captures are flattened into eight stable SyntaxTheme classes and applied as foreground-only HighlightStyle spans, so color does not change text metrics. Parsing happens when source or language changes; scrolling only mounts rows and lazily creates their StyledText. Parsing is limited to 2 MiB, individual lines over 32 KiB stay plain, and the capture count is capped.

#API reference

Expand an entry for its description and complete signature. A dash means no declaration default is documented.

CodeBlock

View source
pub fn new() -> Self
with_textpub fn with_text(source: &str) -> Self—

Configures with text for this part.

pub fn with_text(source: &str) -> Self
View source
with_languagepub fn with_language(mut self, language: SyntaxLanguage) -> Self—

Configures with language for this part.

pub fn with_language(mut self, language: SyntaxLanguage) -> Self
View source
with_stylepub fn with_style(mut self, style: CodeBlockStyle) -> Self—

Configures with style for this part.

pub fn with_style(mut self, style: CodeBlockStyle) -> Self
View source
textpub fn text(&self) -> &str—

Configures text for this part.

pub fn text(&self) -> &str
View source
languagepub const fn language(&self) -> SyntaxLanguage—

Configures language for this part.

pub const fn language(&self) -> SyntaxLanguage
View source
stylepub const fn style(&self) -> CodeBlockStyle—

Configures style for this part.

pub const fn style(&self) -> CodeBlockStyle
View source
is_truncatedpub const fn is_truncated(&self) -> bool—

Configures is truncated for this part.

pub const fn is_truncated(&self) -> bool
View source
line_countpub fn line_count(&self) -> usize—

Configures line count for this part.

pub fn line_count(&self) -> usize
View source
visible_rowspub fn visible_rows(&self) -> Range<usize>—

Configures visible rows for this part.

pub fn visible_rows(&self) -> Range<usize>
View source
list_statepub const fn list_state(&self) -> &ListState—

Configures list state for this part.

pub const fn list_state(&self) -> &ListState
View source
set_textpub fn set_text(&mut self, source: &str) -> CodeBlockUpdate—

Configures set text for this part.

pub fn set_text(&mut self, source: &str) -> CodeBlockUpdate
View source
set_languagepub fn set_language(&mut self, language: SyntaxLanguage) -> bool—

Configures set language for this part.

pub fn set_language(&mut self, language: SyntaxLanguage) -> bool
View source
set_stylepub fn set_style(&mut self, style: CodeBlockStyle) -> bool—

Configures set style for this part.

pub fn set_style(&mut self, style: CodeBlockStyle) -> bool
View source
refresh_syntax_languagespub fn refresh_syntax_languages(&mut self) -> bool—

Refresh newly registered injection grammars without resetting scroll state.

pub fn refresh_syntax_languages(&mut self) -> bool
View source

CodeBlock::element

View source
pub fn element(&self, id: impl Into<ElementId>) -> Element

Build a selectable, caret-free code surface with a virtualized logical-line window.