Editor
保留式原生代码编辑器,支持语法着色、行号、缩进和双轴滚动。
默认不包含任何语言语法。Rust 可注册静态链接的语法;Go 和 TypeScript 可通过 LoadLanguagePack / loadLanguagePack 加载单个跨平台包,其中包含所选 Tree-sitter Wasm 语法与查询。
组件默认不带视觉样式:文本和行号继承颜色,不添加背景、边框、圆角、内边距或语法/差异配色。外观由应用显式提供样式与颜色。
#导入
启用 QuickGUI 可选的 editor crate feature,并导入保留式编辑器类型。
use quickgui::{Editor, IntoElement, SyntaxLanguage, View, ViewContext};#用法
struct EditorExample {
editor: Editor,
}
impl View for EditorExample {
fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
let changed = cx.input_listener("source", |view, value, cx| {
if view.editor.set_text(value).changed {
cx.invalidate();
}
});
self.editor.element("source").on_input(changed)
}
}#语法高亮
SyntaxLanguage 选择已注册的 Tree-sitter 语法和高亮查询。查询捕获结果会归并为八种稳定的 SyntaxTheme 类别,并以只影响前景色的 HighlightStyle 区间应用,因此颜色不会改变文本尺寸。源码或语言变化时重新解析受限文档;调色板变化只重新映射已有捕获,滚动不会执行语法分析。解析最多处理 2 MiB,超过 32 KiB 的单行保持普通颜色,捕获数量也有上限。这是结构化语法高亮,不是语言服务器语义标记。
#API 参考
展开条目查看说明和完整签名。短横线表示声明中未记录默认值。
Editor
查看源码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: EditorStyle) -> Self—
with_behaviorpub fn with_behavior(mut self, behavior: TextInputIndentation) -> Self—
Configures with behavior for this part.
pub fn with_behavior(mut self, behavior: TextInputIndentation) -> Self查看源码textpub fn text(&self) -> &str—
languagepub const fn language(&self) -> SyntaxLanguage—
stylepub const fn style(&self) -> EditorStyle—
behaviorpub const fn behavior(&self) -> TextInputIndentation—
is_truncatedpub const fn is_truncated(&self) -> bool—
set_textpub fn set_text(&mut self, source: &str) -> EditorUpdate—
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: EditorStyle) -> bool—
set_behaviorpub fn set_behavior(&mut self, behavior: TextInputIndentation) -> bool—
Configures set behavior for this part.
pub fn set_behavior(&mut self, behavior: TextInputIndentation) -> bool查看源码set_read_onlypub fn set_read_only(&mut self, read_only: bool) -> bool—
refresh_syntax_languagespub fn refresh_syntax_languages(&mut self) -> bool—
Refresh captures after registering a grammar used by this document or an injection. Returns false without parsing when the registry is unchanged; preserves editing state.
pub fn refresh_syntax_languages(&mut self) -> bool查看源码Editor::element
查看源码pub fn element(&self, id: impl Into<ElementId>) -> ElementBuild the editor leaf. Attach a stable [`crate::InputListener`] with [`Element::on_input`](crate::Element::on_input).
