CodeBlock
可选择、无插入光标的代码视图,支持 Tree-sitter 着色和逻辑行虚拟化。
默认不包含任何语言语法。Rust 可注册静态链接的语法;Go 和 TypeScript 可通过 LoadLanguagePack / loadLanguagePack 加载单个跨平台包,其中包含所选 Tree-sitter Wasm 语法与查询。
组件默认不带视觉样式:文本和行号继承颜色,不添加背景、边框、圆角、内边距或语法/差异配色。外观由应用显式提供样式与颜色。
#导入
启用 QuickGUI 可选的 editor crate feature,并导入保留式代码块类型。
use quickgui::{CodeBlock, CodeBlockStyle, SyntaxLanguage};#用法
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)#语法高亮
SyntaxLanguage 选择已注册的 Tree-sitter 语法和高亮查询。捕获结果会归并为八种稳定的 SyntaxTheme 类别,并以只影响前景色的 HighlightStyle 区间应用,因此颜色不会改变文本尺寸。源码或语言变化时才解析;滚动只挂载可见行并延迟创建对应的 StyledText。解析最多处理 2 MiB,超过 32 KiB 的单行保持普通颜色,捕获数量也有上限。
#API 参考
展开条目查看说明和完整签名。短横线表示声明中未记录默认值。
CodeBlock
查看源码pub 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) -> Self查看源码with_stylepub fn with_style(mut self, style: CodeBlockStyle) -> Self—
textpub 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—
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查看源码set_stylepub fn set_style(&mut self, style: CodeBlockStyle) -> bool—
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查看源码CodeBlock::element
查看源码pub fn element(&self, id: impl Into<ElementId>) -> ElementBuild a selectable, caret-free code surface with a virtualized logical-line window.
