Skip to main content

EditorStateManager

Struct EditorStateManager 

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

Editor state manager

EditorStateManager wraps the command executor (CommandExecutor) and its internal EditorCore and provides the following features:

  • State Queries: Retrieve various state snapshots (document, cursor, viewport, etc.)
  • Version Tracking: Automatically increment version number after each modification, supporting incremental updates
  • Change Notifications: Notify subscribers of state changes via callback mechanism
  • Viewport Management: Manage scroll position and visible regions
  • Modification Tracking: Track whether the document has been modified (for save prompts)

§Architecture Notes

The state manager adopts a “unidirectional data flow” pattern:

  1. Frontend executes commands via execute() (recommended)
  2. Or directly modifies internal state via editor_mut() (advanced usage)
  3. If using editor_mut(), call mark_modified() after modification to mark the change type
  4. Manager increments version number and triggers all subscribed callbacks
  5. Frontend retrieves the latest state via various get_*_state() methods

§Example

use editor_core::{Command, EditCommand, EditorStateManager};

let mut manager = EditorStateManager::new("Initial text", 80);

// Subscribe to state changes
manager.subscribe(|change| {
    println!("Version {} -> {}: {:?}",
        change.old_version, change.new_version, change.change_type);
});

// Modify document (automatically maintains consistency + automatically triggers state notifications)
manager.execute(Command::Edit(EditCommand::Insert {
    offset: 0,
    text: "New: ".to_string(),
})).unwrap();

// Query state
let doc_state = manager.get_document_state();
assert!(doc_state.is_modified);
assert_eq!(doc_state.version, 1);

Implementations§

Source§

impl EditorStateManager

Source

pub fn new(text: &str, viewport_width: usize) -> Self

Create a new state manager

Source

pub fn empty(viewport_width: usize) -> Self

Create an empty state manager

Source

pub fn editor(&self) -> &EditorCore

Get a reference to the Editor Core

Source

pub fn editor_mut(&mut self) -> &mut EditorCore

Get a mutable reference to the Editor Core

Source

pub fn execute( &mut self, command: Command, ) -> Result<CommandResult, CommandError>

Execute a command and automatically trigger state change notifications.

  • This method calls the underlying CommandExecutor to ensure consistency of components such as piece_table / line_index / layout_engine.
  • For commands that cause state changes, mark_modified is automatically called.
  • For pure query commands (such as GetViewport), the version number is not incremented.
Source

pub fn version(&self) -> u64

Get current version number

Source

pub fn set_viewport_height(&mut self, height: usize)

Set viewport height

Source

pub fn set_scroll_top(&mut self, scroll_top: usize)

Set scroll position

Source

pub fn get_full_state(&self) -> EditorState

Get complete editor state snapshot

Source

pub fn get_document_state(&self) -> DocumentState

Get document state

Source

pub fn get_cursor_state(&self) -> CursorState

Get cursor state

Source

pub fn get_viewport_state(&self) -> ViewportState

Get viewport state

Source

pub fn get_undo_redo_state(&self) -> UndoRedoState

Get undo/redo state

Source

pub fn get_folding_state(&self) -> FoldingState

Get folding state

Source

pub fn get_style_state(&self) -> StyleState

Get style state

Source

pub fn get_styles_in_range( &self, start: usize, end: usize, ) -> Vec<(usize, usize, StyleId)>

Get all styles within the specified range

Source

pub fn get_styles_at(&self, offset: usize) -> Vec<StyleId>

Get all styles at the specified position

Source

pub fn replace_style_layer( &mut self, layer: StyleLayerId, intervals: Vec<Interval>, )

Replace all intervals in the specified style layer.

Suitable for scenarios such as LSP semantic highlighting and simple syntax highlighting that require “full layer refresh”. This method only triggers StyleChanged once, avoiding version number explosion due to individual insertions.

Source

pub fn clear_style_layer(&mut self, layer: StyleLayerId)

Clear the specified style layer.

Source

pub fn replace_folding_regions( &mut self, regions: Vec<FoldRegion>, preserve_collapsed: bool, )

Replace folding regions wholesale.

If preserve_collapsed is true, any region that matches an existing collapsed region (start_line, end_line) will remain collapsed after replacement.

Source

pub fn clear_folding_regions(&mut self)

Clear all folding regions.

Source

pub fn apply_processing_edits<I>(&mut self, edits: I)
where I: IntoIterator<Item = ProcessingEdit>,

Apply derived-state edits produced by a document processor (highlighting, folding, etc.).

Source

pub fn apply_processor<P>(&mut self, processor: &mut P) -> Result<(), P::Error>

Run a DocumentProcessor against the current document and apply its edits.

Source

pub fn get_viewport_content( &self, start_row: usize, count: usize, ) -> HeadlessGrid

Get viewport content

Source

pub fn get_viewport_content_styled( &self, start_visual_row: usize, count: usize, ) -> HeadlessGrid

Get styled viewport content (by visual line).

  • Supports soft wrapping (based on LayoutEngine)
  • Cell.styles will contain the merged result of interval_tree and all style_layers
Source

pub fn subscribe<F>(&mut self, callback: F)
where F: FnMut(&StateChange) + Send + 'static,

Subscribe to state change notifications

Source

pub fn has_changed_since(&self, version: u64) -> bool

Check if state has changed since a version

Source

pub fn mark_modified(&mut self, change_type: StateChangeType)

Mark document as modified and increment version number

Source

pub fn mark_saved(&mut self)

Mark document as unmodified (e.g., after saving)

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.