Skip to main content

EditorState

Struct EditorState 

Source
pub struct EditorState { /* private fields */ }

Implementations§

Source§

impl EditorState

Source

pub fn caret_table_align(&self) -> Option<CellAlign>

The alignment of the table column the caret sits in — but only while the caret is in the table’s HEADER row (the toolbar lives there; alignment is a per-column property, set once from the header). None otherwise. Read from the current content, since the painted table_rows lag a frame right after a separator rewrite (which would highlight the just-changed-from button).

Source

pub fn set_caret_table_align( &mut self, align: CellAlign, cx: &mut Context<'_, Self>, )

Set the alignment of the caret’s table column by rewriting that table’s |---| separator row; the caret stays put. No-op outside a table cell.

Source

pub fn insert_table_row(&mut self, below: bool, cx: &mut Context<'_, Self>)

Insert an empty row above/below the caret’s row (Word-style); the caret moves into the new row’s first cell. No-op outside a table.

Source

pub fn delete_table_row(&mut self, cx: &mut Context<'_, Self>)

Delete the caret’s table row (body rows only — the header + separator stay). The caret keeps its cell + in-cell offset, landing on the row that takes the deleted row’s place. No-op outside a table.

Source

pub fn delete_table(&mut self, cx: &mut Context<'_, Self>)

Delete the whole table the caret is in — its grid lines plus an optional <!-- table:STYLE --> marker line directly above — joining the surrounding text. The caret lands where the table was.

Source

pub fn duplicate_table_row(&mut self, cx: &mut Context<'_, Self>)

Duplicate the caret’s row below itself (the header duplicates as the first body row). The caret lands in the copy, same cell + offset.

Source

pub fn copy_table(&mut self, cx: &mut Context<'_, Self>)

Copy the caret’s table — its grid source plus any <!-- table:STYLE --> marker — to the clipboard (markdown, pasteable anywhere).

Source

pub fn set_table_style( &mut self, name: Option<&'static str>, cx: &mut Context<'_, Self>, )

Set the caret table’s visual style (None = Grid, the default), preserving any drag-resized cols= widths in the marker.

Source

pub fn insert_table_column(&mut self, right: bool, cx: &mut Context<'_, Self>)

Insert an empty column left/right of the caret’s column (a cell added to every row; the separator gets a default-left marker). The caret stays in its cell. No-op outside a table.

Source

pub fn delete_table_column(&mut self, cx: &mut Context<'_, Self>)

Delete the caret’s column from every row; the caret stays near where the column was. No-op outside a table, or on the last remaining column.

Source§

impl EditorState

Source

pub fn new(_window: &mut Window, cx: &mut Context<'_, Self>) -> Self

Source

pub fn with_text(self, text: impl Into<String>) -> Self

Builder: start with the given text (caret at the start).

Source

pub fn with_placeholder(self, text: impl Into<SharedString>) -> Self

Builder: placeholder shown when empty.

Source

pub fn text(&self) -> &str

The current document text.

Source

pub fn replace_range( &mut self, range: Range<usize>, text: &str, cx: &mut Context<'_, Self>, )

Replace byte range with text as ONE recorded (undoable) edit, leaving the caret after the inserted text. Unlike Self::set_text this preserves — and extends — the undo history, so a host writing back a structural edit (e.g. a committed $$…$$ formula) lands as a normal undo step rather than clobbering the history.

Source

pub fn set_text(&mut self, text: impl Into<String>, cx: &mut Context<'_, Self>)

Replace the whole document; resets the caret to the start.

Source

pub fn set_diagnostics( &mut self, diagnostics: Vec<Diagnostic>, cx: &mut Context<'_, Self>, )

Replace the set of diagnostics (underlined spans). The host computes these (e.g. spell-check) and refreshes them as the text changes.

Source

pub fn set_code_languages(&mut self, langs: Vec<SharedString>)

Turn on WYSIWYG (live-preview) markdown styling with the given color/font palette (call once at setup). Inline bold/italic/code/link/ tag formatting then renders as you type — markers stay in the text, dimmed. Without it the editor is the raw view: plain text, spell-check underlines only. Languages offered in a code block’s language picker (the host’s highlighter set, e.g. its compiled tree-sitter grammars). Empty — the default — leaves the tag click-inert.

Source

pub fn set_scroll_compensator( &mut self, f: impl Fn(Pixels, &mut Window, &mut App) + 'static, )

Install the scroll-anchoring hook (see ScrollCompensatorFn): when an async block render (math/mermaid/image) changes heights above the window viewport, the host receives the delta and shifts its scroll offset so the visible content stays put (Cditor’s anchor-restore).

Source

pub fn set_markdown_style( &mut self, style: SyntaxStyle, cx: &mut Context<'_, Self>, )

Source

pub fn set_labels(&mut self, labels: Labels, cx: &mut Context<'_, Self>)

Set the host-localized labels for the context menus / chrome. Replace on every language switch so the current editors pick it up.

Source

pub fn clear_markdown_style(&mut self, cx: &mut Context<'_, Self>)

Turn off live-preview styling — the editor falls back to plain text (spell-check underlines only). Used when the host’s WYSIWYG setting is switched off; a no-op if styling was already off.

Source

pub fn on_suggest(&mut self, provider: impl Fn(&str) -> Vec<String> + 'static)

Install the provider consulted when the user right-clicks a flagged word. It’s handed the offending word and returns replacements (best first). Kept lazy by design — the OS suggestion call can be slow, so it runs only on right-click, never in the per-edit detection pass.

Source

pub fn set_block_image_provider( &mut self, provider: impl Fn(&str) -> Option<Arc<RenderImage>> + 'static, )

Install the provider that resolves a standalone image line’s src to a decoded image; with it, such lines render inline (W4) when the caret is elsewhere. Without it (or while an image is still loading), the line shows its raw ![](src) source.

Source

pub fn set_block_chip_provider( &mut self, provider: impl Fn(&str) -> Option<SharedString> + 'static, )

Install the provider that classifies an ![](src) reference as a file chip (e.g. a PDF) and supplies its label. With it, such lines render as a clickable chip when the caret is elsewhere; a left-click emits EditorEvent::OpenLink and a right-click places the caret to edit.

Source

pub fn set_embed_provider( &mut self, provider: impl Fn(&str) -> Option<(AnyView, Pixels)> + 'static, )

Install the provider that resolves a standalone ![[target]] line to a host view rendering the transclusion + the height to reserve for it. With it, such lines show the embedded content in place (raw on caret); without (or when it returns None) they fall back to a clickable chip.

Source

pub fn set_block_mermaid_provider( &mut self, provider: impl Fn(&str) -> Option<(Arc<RenderImage>, f32, f32)> + 'static, )

Install the provider that resolves a ```mermaid block’s source to a rendered diagram: the bitmap plus its logical (display) px size — see [BlockMathFn] for why the host supplies the size. With it, such a block renders as the diagram when the caret is elsewhere; with the caret inside (or while it renders) it shows the raw fenced source. Pre-render with mermaid_sources.

Source

pub fn set_clipboard_writer(&mut self, writer: ClipboardWriter)

Install the provider that resolves a $$…$$ block’s LaTeX to a typeset equation: the bitmap plus its logical (display) px size — see [BlockMathFn] for why the host supplies the size. With it, such a block renders as the equation when the caret is elsewhere; with the caret inside (or while it renders) it shows the raw $$…$$ source. Pre-render with math_sources. Route Copy/Cut through writer instead of gpui’s plain-string copy — the host owns the actual clipboard write (and its extra flavors).

Highlight matches (source byte ranges) behind the text — soft yellow, with active in the stronger current-match orange (the reader’s browser-style find colors). Empty clears. Host-driven: a find bar computes matches (see find_in_source) and steps active.

Source

pub fn offset_screen_top(&self, offset: usize) -> Option<Pixels>

The window-space top of the row containing byte offset (from the last layout) — for a host scrolling a find match into view. None before first paint or for an out-of-range offset.

Source

pub fn set_block_math_provider( &mut self, provider: impl Fn(&str) -> Option<(Arc<RenderImage>, f32, f32)> + 'static, )

Source

pub fn set_block_math_em(&mut self, em: f32)

Declare the em the block_math provider rasterizes at (e.g. the host’s display-math font size). Turns on inline $…$ rendering: each inline formula reuses the block raster for the same LaTeX, scaled by text_em / em so it sits at text size. Pre-render inline sources too (see inline_math_sources).

Source

pub fn set_code_highlighter( &mut self, f: impl Fn(&str, &str) -> Vec<(Range<usize>, HighlightStyle)> + 'static, )

Set the fenced-code syntax highlighter (see [CodeHighlightFn]).

Source

pub fn take_replaced_selection(&mut self) -> Option<String>

The text the most recent keystroke edit replaced (its selection), if any — consumed (one read per edit). Lets a host’s auto-pair logic tell “opener typed over a selection” from deletions with identical diffs.

Source

pub fn set_auto_replace( &mut self, f: impl Fn(&str) -> Option<(Range<usize>, String)> + 'static, )

Set the word-completion auto-replace hook (see [AutoReplaceFn]).

Source

pub fn set_editing_block( &mut self, range: Range<usize>, view: AnyView, height: Pixels, cx: &mut Context<'_, Self>, )

Begin an in-line structural edit of the $$…$$ block at range: reserve a gap at its spot and paint view (the host’s editor) there. The host focuses view.

Source

pub fn editing_block_range(&self) -> Option<Range<usize>>

The byte range of the block currently being structurally edited (the range handed to Self::set_editing_block — the source text is untouched while the edit is open, so it stays valid). None when no block edit is open.

Source

pub fn end_editing_block( &mut self, cx: &mut Context<'_, Self>, ) -> Option<Range<usize>>

End an in-line math edit (the host has committed / cancelled). Returns the block’s byte range, so the host can overwrite it.

Source

pub fn set_editing_inline( &mut self, range: Range<usize>, view: AnyView, offset: Point<Pixels>, cx: &mut Context<'_, Self>, )

Begin a structural edit of the inline $…$ span at range (absolute bytes): overlay view (the host’s editor) at the formula’s painted spot. The host focuses view. offset places the view relative to the raster’s top-left, letting the host align the view’s glyphs with the displayed formula’s (their paddings differ).

Source

pub fn end_editing_inline( &mut self, cx: &mut Context<'_, Self>, ) -> Option<Range<usize>>

End an inline math edit. Returns the span’s byte range, so the host can overwrite it.

Source

pub fn is_inline_math_range(&self, range: &Range<usize>) -> bool

Whether range still bounds an inline $…$ span (a $ at each end, content between, no newline, not a $$ fence) — guards the inline commit against a stale/shifted range that would otherwise splice text at the wrong spot.

Source

pub fn math_align(&self, block_start: usize) -> MathAlign

The horizontal alignment of the $$…$$ block whose byte range starts at block_start (its <!-- math:ALIGN --> marker, or Center by default) — so the host can seed the in-line editor at the right justification when opening it.

Source

pub fn math_marker_edit( &self, block: Range<usize>, align: MathAlign, ) -> (Range<usize>, String)

Compute the recorded edit that writes align’s marker for the $$ block at byte block: the (possibly marker-extended) range to replace, and the marker prefix to prepend to the rewritten block. Center (default) → no marker (drops any existing one); left/right → add or replace it. The host appends the block text to the prefix. Folding the marker into the block’s commit edit avoids a separate, range-shifting edit.

Source

pub fn find_math_block( &self, source: &str, approx: usize, ) -> Option<Range<usize>>

Re-find a $$…$$ block by its exact LaTeX source, returned as a BYTE range (nearest to the now-stale byte approx if several match) — so opening/committing one after a prior formula’s commit shifted offsets targets the right block. math_blocks yields LINE ranges, so convert like math_block_at does (else the caret jumps to the top).

Source

pub fn find_inline_math( &self, latex: &str, approx: usize, ) -> Option<Range<usize>>

Re-find an inline $…$ span by its exact inner LaTeX, as an absolute byte range (nearest to the now-stale byte approx if several match) — the inline counterpart of Self::find_math_block, so opening/committing after a prior edit shifted offsets targets the right span.

Source

pub fn is_math_block_range(&self, range: &Range<usize>) -> bool

Whether byte range (half-open) still starts a $$…$$ block — a commit guard so a stale/shifted range can’t splice the block into the wrong place and corrupt the doc.

Source

pub fn set_tab_indent(&mut self, spaces: usize)

Spaces inserted per Tab / list-nesting level (Indent/Outdent). The host keeps this in sync with its list-indent setting so nesting is configurable.

Source

pub fn cursor(&self) -> usize

The caret’s byte offset into Self::text (the moving end of any selection). For hosts that drive a menu/completion off the caret position.

Source

pub fn last_edit_was_keystroke(&self) -> bool

Whether the last content change was a single typed character or single-char backspace (vs a programmatic / multi-char edit). Hosts gate auto-pairing on this so structural edits (table row/column ops, paste, …) don’t trip it.

Source

pub fn set_cursor(&mut self, offset: usize, cx: &mut Context<'_, Self>)

Place the caret at offset (a byte offset into the document), collapsing any selection. Clamped to the document and snapped down to a char boundary, so a host can pass a raw click offset safely — e.g. to enter edit mode where rendered text was clicked.

Source

pub fn row_layout(&self) -> Vec<(Pixels, Pixels)>

Per logical line, from the last paint: its top offset within the editor and its first wrap-row’s height — enough for a host-drawn gutter (line numbers) to align with rows without re-deriving layout. Empty before the first paint. Rows collapsed by a heading fold show no vertical advance (the next row’s top equals theirs) — a gutter should skip those.

Source

pub fn bounds_for_offset(&self, offset: usize) -> Option<Bounds<Pixels>>

Window-space bounds of the caret at offset, from the last paint’s layout — for anchoring a popup (e.g. a slash menu) at a document offset. None before the first paint or if offset’s row isn’t laid out.

Source

pub fn value(&self) -> SharedString

The document text as an owned SharedString; use Self::text for a borrowed &str.

Source

pub fn focus(&self, window: &mut Window, cx: &mut Context<'_, Self>)

Focus the editor so it receives keyboard input. (set_cursor only moves the caret; call this to enter edit mode, e.g. on a click into rendered text.)

Source

pub fn copy_plain(&mut self, _window: &mut Window, cx: &mut Context<'_, Self>)

Copy the selection as the raw markdown ONLY — no host clipboard flavors — for pasting literal source into rich surfaces (the context menu’s “Copy as Markdown”). Same selection/renumber rules as copy.

Source

pub fn edit_math_at_caret(&mut self, cx: &mut Context<'_, Self>)

If the caret sits inside a $$…$$ block, ask the host to open the structural editor for it (caret at the formula’s start). Lets the host turn a freshly-inserted, empty math block (the /math snippet) straight into a live editor instead of raw source.

Source

pub fn property_block_at_caret(&self) -> Option<(Range<usize>, SharedString)>

The property block covering the caret’s line, if any (WYSIWYG-only, like Self::edit_math_at_caret) — so the host can open the property editor on a freshly-inserted /property line instead of leaving raw source.

Source

pub fn set_grip_inset(&mut self, inset: Pixels)

Extra left offset for the gutter drag grip (e.g. the host’s line-number gutter width), so the grip clears other gutter chrome.

Source

pub fn exit_math( &mut self, block: Range<usize>, after: bool, window: &mut Window, cx: &mut Context<'_, Self>, )

Seat the caret on the plain-text line just before (after = false) or after (after = true) the math block, and focus the editor — the keyboard counterpart to clicking away, for when the caret flows out of a $$…$$ formula’s structural editor (so it never lands on the hidden $$ fence lines, which would reveal raw source).

Source

pub fn caret_screen_bounds(&self) -> Option<Bounds<Pixels>>

The caret’s bounds in window space (its painted Y range), or None before the first paint. Lets a host scroll the caret into view; computed from the layout stored at the last paint, so it’s valid for caret moves that don’t change the text (arrow keys, click).

Trait Implementations§

Source§

impl EntityInputHandler for EditorState

Source§

fn text_for_range( &mut self, range_utf16: Range<usize>, actual_range: &mut Option<Range<usize>>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<String>

Source§

fn selected_text_range( &mut self, _: bool, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<UTF16Selection>

Source§

fn marked_text_range( &self, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<Range<usize>>

Source§

fn unmark_text(&mut self, _: &mut Window, _: &mut Context<'_, Self>)

Source§

fn replace_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, _: &mut Window, cx: &mut Context<'_, Self>, )

Source§

fn replace_and_mark_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, new_selected_range_utf16: Option<Range<usize>>, _: &mut Window, cx: &mut Context<'_, Self>, )

Source§

fn bounds_for_range( &mut self, range_utf16: Range<usize>, bounds: Bounds<Pixels>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<Bounds<Pixels>>

Source§

fn character_index_for_point( &mut self, point: Point<Pixels>, _: &mut Window, _: &mut Context<'_, Self>, ) -> Option<usize>

Source§

fn paste( &mut self, item: ClipboardItem, window: &mut Window, cx: &mut Context<'_, Self>, )

See InputHandler::paste for details
Source§

fn set_selected_text_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut Context<'_, Self>, )

Source§

fn text_length_utf16( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<usize>

Source§

fn accepts_text_input( &self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> bool

Source§

fn text_input_configuration( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> TextInputConfiguration

Source§

fn text_input_editable_range( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<Range<usize>>

Source§

impl EventEmitter<EditorEvent> for EditorState

Source§

impl Focusable for EditorState

Source§

fn focus_handle(&self, _: &App) -> FocusHandle

Returns the focus handle associated with this view.
Source§

impl Render for EditorState

Source§

fn render( &mut self, window: &mut Window, cx: &mut Context<'_, Self>, ) -> impl IntoElement

Render this view into an element tree.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more