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 リファレンス
項目を展開すると説明と完全なシグネチャが表示されます。ダッシュは宣言に既定値が記載されていないことを示します。
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—
Configures with style for this part.
pub 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.
