本文へスキップ

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 文法とハイライトクエリを選択します。キャプチャは 8 種類の安定した SyntaxTheme クラスへ平坦化され、前景色だけの HighlightStyle 範囲として適用されるため、色はテキスト寸法を変えません。解析はソースまたは言語が変わったときに行い、スクロール時は行をマウントして対応する StyledText を遅延生成するだけです。解析は最大 2 MiB、32 KiB を超える単一行は通常色のまま、キャプチャ数にも上限があります。

#API リファレンス

項目を展開すると説明と完全なシグネチャが表示されます。ダッシュは宣言に既定値が記載されていないことを示します。

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.