Skip to main content

CodeEditor

Struct CodeEditor 

Source
pub struct CodeEditor { /* private fields */ }
Expand description

A batteries-included code editor: owns a Document, renders the Editor widget, and runs the editing/highlighting plumbing internally. See the module docs for the mechanism-vs-policy split and the ownership fork.

Implementations§

Source§

impl CodeEditor

Source

pub fn new(source: impl Into<String>) -> Self

A new editor over source, with the default theme (scrive_dark_theme) but no grammar — plain text until language attaches one.

§Panics

Panics if source does not fit scrive’s u32 offset space (~4 GiB) — far past any editable document.

Source

pub fn language(self, grammar: SyntaxDef) -> Self

Attach a grammar, enabling syntax highlighting. Uses the theme set by theme if one was staged, else the bundled default. Order-independent with theme. Seeds the whole (small) document’s colors immediately so the first paint is highlighted without waiting for a viewport report — the cold-load fix.

Source

pub fn theme(self, theme: TokenTheme) -> Self

Override the syntax theme (default: scrive_dark_theme). Order-independent with language: before a grammar is attached it is staged; after, it re-themes the live cache.

Source

pub fn font(self, font: Font) -> Self

Set the (monospace) font. Default Font::MONOSPACE.

Source

pub fn text_size(self, px: f32) -> Self

Set the font size in logical pixels. Default 14.0.

Source

pub fn id(self, id: impl Into<Id>) -> Self

Give the editor a widget id for focus addressing (default "scrive-editor"). Needed only if a multi-pane host targets focus at it.

Source

pub fn find(self, enabled: bool) -> Self

Enable or disable the built-in find bar and its Ctrl+F / Ctrl+H bindings. Default on.

Source

pub fn line_comment(self, marker: Option<&str>) -> Self

Set the line-comment marker used by toggle-comment (e.g. Some("//")); None (the default) disables toggle-comment. It also drives comment-aware bracket matching, so brackets inside // … stop being matched / coloured / folded. Language config, like the grammar — the core ships none.

Source

pub fn bracket_lexing( self, string_delims: Vec<u8>, char_delim: Option<u8>, ) -> Self

Configure comment/string-aware bracket matching’s string and char-literal delimiters — brackets inside a string (e.g. "{}") or char literal are not matched, coloured, folded, or indent-guided. (Line comments come from line_comment.) char_delim is off by default: in some languages ' is ambiguous (Rust lifetimes), so it is opt-in.

Source

pub fn completions(self, provider: impl Completions + 'static) -> Self

Supply a completion provider (default: none). The editor drives it on edits, filters and renders the popup, and applies an accepted item — including expanding a snippet insert into an interactive tab-stop session.

Source

pub fn hover(self, provider: impl Hover + 'static) -> Self

Supply a hover provider (default: none). The editor queries it on the idle-hover action and renders the returned card above the word.

Source

pub fn signature(self, provider: impl SignatureHelp + 'static) -> Self

Supply a signature-help provider (default: none). ( opens the box; while open, edits/moves re-query and a None reply closes it.

Source

pub fn document(&self) -> &Document

The owned document, for saving / diffing / inspection (document().serialize(..), .snapshot(), .revision()). Read-only: mutations go through the blessed methods so the post-edit tail always runs.

Source

pub fn is_dirty(&self) -> bool

Whether an edit has landed since the last take_dirty.

Source

pub fn take_dirty(&mut self) -> bool

Read and clear the dirty flag — for a host that marks a title bar or schedules a save on change.

Source

pub fn cursor(&self) -> Point

The primary caret position as (row, col).

Source

pub fn selection(&self) -> Range<u32>

The primary selection’s byte range (empty at a bare caret).

Source

pub fn edit(&mut self, ops: Vec<EditOp>)

Apply a programmatic batch of edits as one transaction, then run the post-edit tail. The tail is why this exists instead of a raw &mut Document: it keeps highlighting, find, and the intel controllers current.

Source

pub fn load(&mut self, source: impl Into<String>, grammar: Option<SyntaxDef>)

Swap the whole buffer (load a new file), keeping the current grammar and theme unless grammar supplies a new language. The blessed whole-buffer swap: it runs as one transaction and re-tokenizes the visible document, so highlighting is correct immediately — never mutate the buffer behind the tail’s back. (The swap is a normal transaction, so it is undoable.)

Source

pub fn fold_all(&mut self)

Collapse every foldable region (the “Fold All” command). Folds are view state, so this records nothing on the undo stack and runs no post-edit tail.

Source

pub fn set_theme(&mut self, theme: TokenTheme)

Swap the syntax theme at runtime. The retained theme updates too, so a later load keeps it.

Source

pub fn set_diagnostics( &mut self, revision: Revision, diags: Vec<Diagnostic>, ) -> DiagnosticsOutcome

Publish a diagnostic set from the host’s (debounced, off-thread) compile or language-server pass. Stamped by revision: a set computed against a snapshot the buffer has moved past is dropped (the previous squiggles keep riding edits). This is the ingest half of recompile-on-edit — pair it with an edit signal (take_dirty or a revision compare) and document().snapshot().

Source

pub fn take_completion_request(&mut self) -> Option<CompletionRequest>

Take the pending async completion request, if any. A host with an off-thread / language-server provider (rather than a synchronous completions one) polls this after update, runs its query at the request’s position and revision, and returns the result through set_completions.

Source

pub fn set_completions( &mut self, revision: Revision, items: Vec<CompletionItem>, )

Ingest completion items from an async provider, stamped by revision. Strict staleness: if the buffer has moved since the request the items are dropped (the host re-requests on the next edit). On a current revision they open/refilter the popup against the live word — including snippet-format items (InsertText::Snippet), which expand into an interactive tab-stop session on accept exactly like a synchronous provider’s.

Source

pub fn take_signature_request(&mut self) -> Option<SignatureRequest>

Take the pending async signature-help request, if any — the signature twin of take_completion_request.

Source

pub fn set_signature(&mut self, revision: Revision, info: Option<SignatureInfo>)

Ingest a signature-help result from an async provider, stamped by revision (strict staleness). None closes the box. Current-revision results replace the shown signature.

Source

pub fn take_hover_request(&mut self) -> Option<HoverRequest>

Take the pending async hover request, if any — the hover twin of take_completion_request.

Source

pub fn set_hover(&mut self, revision: Revision, info: Option<HoverInfo>)

Ingest a hover card from an async provider, stamped by revision (strict staleness). Replaces the shown card (the host builds it, and may fold in diagnostics read from document); None clears it.

Source

pub fn observe_changes(&mut self, on: bool)

Enable or disable the incremental-change log — for a host mirroring edits to a language server (textDocument/didChange). Off by default (zero overhead); forwards to Document::observe_changes. Full-document sync hosts leave this off and re-read document().snapshot() instead.

Source

pub fn drain_changes(&mut self) -> Vec<EditOp>

Drain the incremental-change log: every applied edit since the last drain, as EditOp deltas ready to translate into LSP content changes. Empty unless observe_changes is on. Forwards to Document::drain_changes.

Source

pub fn update(&mut self, event: Event) -> Task<Event>

Fold one Event into the editor. Map it back to your message type: Message::Editor(e) => self.editor.update(e).map(Message::Editor).

Source

pub fn view(&self) -> Element<'_, Event>

The editor element. Map it back to your message type: self.editor.view().map(Message::Editor).

Source

pub fn subscription(&self) -> Subscription<Event>

The editor’s subscription — the frames-gated highlight sweep. It runs only while a dirty highlight frontier remains, and because language / an edit leaves the frontier dirty it fires on the very next frame with no input required (this is what makes highlighting appear at load instead of after the first scroll). At convergence it returns Subscription::none, so an idle document does zero per-frame work. Map it: self.editor.subscription().map(Message::Editor).

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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<State, Message> IntoBoot<State, Message> for State

Source§

fn into_boot(self) -> (State, Task<Message>)

Turns some type into the initial state of some Application.
Source§

impl<T> MaybeClone for T

Source§

impl<T> MaybeDebug for T

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

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