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
impl CodeEditor
Sourcepub fn new(source: impl Into<String>) -> Self
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.
Sourcepub fn language(self, grammar: SyntaxDef) -> Self
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.
Sourcepub fn theme(self, theme: TokenTheme) -> Self
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.
Sourcepub fn font(self, font: Font) -> Self
pub fn font(self, font: Font) -> Self
Set the (monospace) font. Default Font::MONOSPACE.
Sourcepub fn id(self, id: impl Into<Id>) -> Self
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.
Sourcepub fn find(self, enabled: bool) -> Self
pub fn find(self, enabled: bool) -> Self
Enable or disable the built-in find bar and its Ctrl+F / Ctrl+H bindings. Default on.
Sourcepub fn line_comment(self, marker: Option<&str>) -> Self
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.
Sourcepub fn bracket_lexing(
self,
string_delims: Vec<u8>,
char_delim: Option<u8>,
) -> Self
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.
Sourcepub fn completions(self, provider: impl Completions + 'static) -> Self
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.
Sourcepub fn hover(self, provider: impl Hover + 'static) -> Self
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.
Sourcepub fn signature(self, provider: impl SignatureHelp + 'static) -> Self
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.
Sourcepub fn document(&self) -> &Document
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.
Sourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Whether an edit has landed since the last take_dirty.
Sourcepub fn take_dirty(&mut self) -> bool
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.
Sourcepub fn selection(&self) -> Range<u32>
pub fn selection(&self) -> Range<u32>
The primary selection’s byte range (empty at a bare caret).
Sourcepub fn edit(&mut self, ops: Vec<EditOp>)
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.
Sourcepub fn load(&mut self, source: impl Into<String>, grammar: Option<SyntaxDef>)
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.)
Sourcepub fn fold_all(&mut self)
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.
Sourcepub fn set_theme(&mut self, theme: TokenTheme)
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.
Sourcepub fn set_diagnostics(
&mut self,
revision: Revision,
diags: Vec<Diagnostic>,
) -> DiagnosticsOutcome
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().
Sourcepub fn take_completion_request(&mut self) -> Option<CompletionRequest>
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.
Sourcepub fn set_completions(
&mut self,
revision: Revision,
items: Vec<CompletionItem>,
)
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.
Sourcepub fn take_signature_request(&mut self) -> Option<SignatureRequest>
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.
Sourcepub fn set_signature(&mut self, revision: Revision, info: Option<SignatureInfo>)
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.
Sourcepub fn take_hover_request(&mut self) -> Option<HoverRequest>
pub fn take_hover_request(&mut self) -> Option<HoverRequest>
Take the pending async hover request, if any — the hover twin of
take_completion_request.
Sourcepub fn set_hover(&mut self, revision: Revision, info: Option<HoverInfo>)
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.
Sourcepub fn observe_changes(&mut self, on: bool)
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.
Sourcepub fn drain_changes(&mut self) -> Vec<EditOp>
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.
Sourcepub fn update(&mut self, event: Event) -> Task<Event>
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).
Sourcepub fn view(&self) -> Element<'_, Event>
pub fn view(&self) -> Element<'_, Event>
The editor element. Map it back to your message type:
self.editor.view().map(Message::Editor).
Sourcepub fn subscription(&self) -> Subscription<Event>
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§
impl !Freeze for CodeEditor
impl !RefUnwindSafe for CodeEditor
impl !Send for CodeEditor
impl !Sync for CodeEditor
impl !UnwindSafe for CodeEditor
impl Unpin for CodeEditor
impl UnsafeUnpin for CodeEditor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<State, Message> IntoBoot<State, Message> for State
impl<State, Message> IntoBoot<State, Message> for State
Source§fn into_boot(self) -> (State, Task<Message>)
fn into_boot(self) -> (State, Task<Message>)
Application.