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 sourcepub fn new() -> Selfwith_textpub fn with_text(source: &str) -> Self—
with_languagepub fn with_language(mut self, language: SyntaxLanguage) -> Self—
Configures with language for this part.
pub fn with_language(mut self, language: SyntaxLanguage) -> SelfView sourcewith_stylepub fn with_style(mut self, style: CodeBlockStyle) -> Self—
Configures with style for this part.
pub fn with_style(mut self, style: CodeBlockStyle) -> SelfView sourcetextpub fn text(&self) -> &str—
languagepub const fn language(&self) -> SyntaxLanguage—
stylepub const fn style(&self) -> CodeBlockStyle—
is_truncatedpub const fn is_truncated(&self) -> bool—
line_countpub fn line_count(&self) -> usize—
visible_rowspub fn visible_rows(&self) -> Range<usize>—
list_statepub const fn list_state(&self) -> &ListState—
set_textpub fn set_text(&mut self, source: &str) -> CodeBlockUpdate—
Configures set text for this part.
pub fn set_text(&mut self, source: &str) -> CodeBlockUpdateView sourceset_languagepub fn set_language(&mut self, language: SyntaxLanguage) -> bool—
Configures set language for this part.
pub fn set_language(&mut self, language: SyntaxLanguage) -> boolView sourceset_stylepub fn set_style(&mut self, style: CodeBlockStyle) -> bool—
Configures set style for this part.
pub fn set_style(&mut self, style: CodeBlockStyle) -> boolView sourcerefresh_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) -> boolView sourceCodeBlock::element
View sourcepub fn element(&self, id: impl Into<ElementId>) -> ElementBuild a selectable, caret-free code surface with a virtualized logical-line window.
