GPUI Box GitHub

Components / controls

Editor view

Plain source editing with caller-owned language policy.

A view survives a frame. Hold it in an Entity with cx.new(..) and reach it with .update(..).

use gpui_kit::controls::editor::Editor;

Reviewed in editor editor-multicursor editor-services editor-folding editor-options.

Construct
new(ident: impl Into<Ident>, label: impl Into<SharedString>, text: impl Into<SharedString>, window: &mut Window, cx: &mut Context<Self>) -> Self
Options

Chain onto the value.

language_services(enabled: bool) -> Self
rows(rows: usize) -> Self
line_numbers(visible: bool) -> Self
read_only(read_only: bool) -> Self
indent_with(indenter: impl Fn(EditorIndentRequest) -> Option<EditorIndentation> + 'static) -> Self
highlights(highlights: EditorHighlights) -> Self
syntax(syntax: EditorSyntax) -> Self
Commands

Need a Context, so they need a view.

set_folds(revision: u64, mut folds: Vec<EditorFold>, cx: &mut Context<Self>) -> bool
set_fold_collapsed(id: &str, collapsed: bool, cx: &mut Context<Self>) -> bool
request_service(kind: EditorServiceKind, position: usize, cx: &mut Context<Self>) -> Option<EditorServiceRequest>
set_service_result(request: u64, mut result: AsyncValue<EditorServiceResult, SharedString>, cx: &mut Context<Self>) -> bool
dismiss_service(cx: &mut Context<Self>)
accept_service_item(id: &str, cx: &mut Context<Self>) -> bool
set_diagnostics(revision: u64, diagnostics: Vec<EditorDiagnostic>, cx: &mut Context<Self>) -> bool
set_semantic_tokens(revision: u64, mut tokens: Vec<EditorSemanticToken>, cx: &mut Context<Self>) -> bool
set_label(label: impl Into<SharedString>, cx: &mut Context<Self>)
set_rows(rows: usize, cx: &mut Context<Self>)
set_line_numbers(visible: bool, cx: &mut Context<Self>)
set_read_only(read_only: bool, cx: &mut Context<Self>)
set_disabled(disabled: bool, cx: &mut Context<Self>)
set_indent_with(indenter: Option<Rc<dyn Fn(EditorIndentRequest) -> Option<EditorIndentation>>>, cx: &mut Context<Self>)
set_syntax(syntax: Option<EditorSyntax>, cx: &mut Context<Self>)
set_language_services(enabled: bool, cx: &mut Context<Self>)
set_selections(selections: impl IntoIterator<Item = (Range<usize>, bool)>, cx: &mut Context<Self>) -> bool
select_rectangle(anchor: Point<Pixels>, focus: Point<Pixels>, cx: &mut Context<Self>) -> bool
apply_edits(revision: u64, edits: impl IntoIterator<Item = (Range<usize>, SharedString)>, cx: &mut Context<Self>) -> bool
set_value(value: impl Into<SharedString>, cx: &mut Context<Self>)
set_highlights(highlights: EditorHighlights, cx: &mut Context<Self>) -> bool
Queries

Only answer; they change nothing.

is_fold_collapsed(id: &str) -> bool
is_disabled() -> bool
is_read_only() -> bool
syntax_state() -> Option<&EditorSyntax>
text_area() -> &Entity<TextArea>
snapshot(cx: &App) -> TextAreaSnapshot
selected_range(cx: &App) -> Range<usize>
selections(cx: &App) -> Vec<(Range<usize>, bool)>
geometry(cx: &App) -> Option<EditorGeometry>
Reports

The variants of the event it emits. It never applies the change itself.

  • ServiceRequested
  • ServiceAccepted
  • DefinitionRequested
  • CodeActionRequested
  • Edited
  • Changed
  • SelectionChanged
  • FoldChanged
  • Pasted
  • Submitted
  • Cancelled
  • Focused
  • Blurred
  • Parsed
  • SyntaxUnavailable
Example · editor
fn editor(window: &mut Window, cx: &mut App) -> AnyElement {
    ensure_editor(window, cx);
    let editor = cx.global::<SceneEditor>().editor.clone();
    let theme = cx.theme().clone();
    let scene = stack(&theme)
        .w(px(760.0))
        .child(caption(
            &theme,
            "one text/IME/history geometry; caller-owned revision highlights and indentation",
        ))
        .child(editor);
    #[cfg(feature = "syntax")]
    let scene = scene
        .child(caption(
            &theme,
            "JSON fixture · in-process incremental syntax, no language server",
        ))
        .child(cx.global::<SceneEditor>().syntax.clone());
    scene.into_any_element()
}

Source: crates/gpui-kit/src/controls/editor.rs