跳到正文

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() -> Self
with_textpub fn with_text(source: &str) -> Self—

Configures with text for this part.

pub 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—

Configures with style for this part.

pub fn with_style(mut self, style: CodeBlockStyle) -> Self
查看源码
textpub fn text(&self) -> &str—

Configures text for this part.

pub fn text(&self) -> &str
查看源码
languagepub const fn language(&self) -> SyntaxLanguage—

Configures language for this part.

pub const fn language(&self) -> SyntaxLanguage
查看源码
stylepub const fn style(&self) -> CodeBlockStyle—

Configures style for this part.

pub const fn style(&self) -> CodeBlockStyle
查看源码
is_truncatedpub const fn is_truncated(&self) -> bool—

Configures is truncated for this part.

pub const fn is_truncated(&self) -> bool
查看源码
line_countpub fn line_count(&self) -> usize—

Configures line count for this part.

pub fn line_count(&self) -> usize
查看源码
visible_rowspub fn visible_rows(&self) -> Range<usize>—

Configures visible rows for this part.

pub fn visible_rows(&self) -> Range<usize>
查看源码
list_statepub const fn list_state(&self) -> &ListState—

Configures list state for this part.

pub 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) -> 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—

Configures set style for this part.

pub 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>) -> Element

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