跳到正文

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 参考

展开条目查看说明和完整签名。短横线表示声明中未记录默认值。

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: EditorStyle) -> Self—

Configures with style for this part.

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

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

Configures style for this part.

pub const fn style(&self) -> EditorStyle
查看源码
behaviorpub const fn behavior(&self) -> TextInputIndentation—

Configures behavior for this part.

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

Configures is truncated for this part.

pub const fn is_truncated(&self) -> bool
查看源码
set_textpub fn set_text(&mut self, source: &str) -> EditorUpdate—

Configures set text for this part.

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

Configures set style for this part.

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

Configures set read only for this part.

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

Build the editor leaf. Attach a stable [`crate::InputListener`] with [`Element::on_input`](crate::Element::on_input).