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:
- Frontend executes commands via
execute()(recommended) - Or directly modifies internal state via
editor_mut()(advanced usage) - If using
editor_mut(), callmark_modified()after modification to mark the change type - Manager increments version number and triggers all subscribed callbacks
- 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
impl EditorStateManager
Sourcepub fn editor(&self) -> &EditorCore
pub fn editor(&self) -> &EditorCore
Get a reference to the Editor Core
Sourcepub fn editor_mut(&mut self) -> &mut EditorCore
pub fn editor_mut(&mut self) -> &mut EditorCore
Get a mutable reference to the Editor Core
Sourcepub fn execute(
&mut self,
command: Command,
) -> Result<CommandResult, CommandError>
pub fn execute( &mut self, command: Command, ) -> Result<CommandResult, CommandError>
Execute a command and automatically trigger state change notifications.
- This method calls the underlying
CommandExecutorto ensure consistency of components such aspiece_table/line_index/layout_engine. - For commands that cause state changes,
mark_modifiedis automatically called. - For pure query commands (such as
GetViewport), the version number is not incremented.
Sourcepub fn set_viewport_height(&mut self, height: usize)
pub fn set_viewport_height(&mut self, height: usize)
Set viewport height
Sourcepub fn set_scroll_top(&mut self, scroll_top: usize)
pub fn set_scroll_top(&mut self, scroll_top: usize)
Set scroll position
Sourcepub fn get_full_state(&self) -> EditorState
pub fn get_full_state(&self) -> EditorState
Get complete editor state snapshot
Sourcepub fn get_document_state(&self) -> DocumentState
pub fn get_document_state(&self) -> DocumentState
Get document state
Sourcepub fn get_cursor_state(&self) -> CursorState
pub fn get_cursor_state(&self) -> CursorState
Get cursor state
Sourcepub fn get_viewport_state(&self) -> ViewportState
pub fn get_viewport_state(&self) -> ViewportState
Get viewport state
Sourcepub fn get_undo_redo_state(&self) -> UndoRedoState
pub fn get_undo_redo_state(&self) -> UndoRedoState
Get undo/redo state
Sourcepub fn get_folding_state(&self) -> FoldingState
pub fn get_folding_state(&self) -> FoldingState
Get folding state
Sourcepub fn get_style_state(&self) -> StyleState
pub fn get_style_state(&self) -> StyleState
Get style state
Sourcepub fn get_styles_in_range(
&self,
start: usize,
end: usize,
) -> Vec<(usize, usize, StyleId)>
pub fn get_styles_in_range( &self, start: usize, end: usize, ) -> Vec<(usize, usize, StyleId)>
Get all styles within the specified range
Sourcepub fn get_styles_at(&self, offset: usize) -> Vec<StyleId> ⓘ
pub fn get_styles_at(&self, offset: usize) -> Vec<StyleId> ⓘ
Get all styles at the specified position
Sourcepub fn replace_style_layer(
&mut self,
layer: StyleLayerId,
intervals: Vec<Interval>,
)
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.
Sourcepub fn clear_style_layer(&mut self, layer: StyleLayerId)
pub fn clear_style_layer(&mut self, layer: StyleLayerId)
Clear the specified style layer.
Sourcepub fn replace_folding_regions(
&mut self,
regions: Vec<FoldRegion>,
preserve_collapsed: bool,
)
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.
Sourcepub fn clear_folding_regions(&mut self)
pub fn clear_folding_regions(&mut self)
Clear all folding regions.
Sourcepub fn apply_processing_edits<I>(&mut self, edits: I)where
I: IntoIterator<Item = ProcessingEdit>,
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.).
Sourcepub fn apply_processor<P>(&mut self, processor: &mut P) -> Result<(), P::Error>where
P: DocumentProcessor,
pub fn apply_processor<P>(&mut self, processor: &mut P) -> Result<(), P::Error>where
P: DocumentProcessor,
Run a DocumentProcessor against the current document and apply its edits.
Sourcepub fn get_viewport_content(
&self,
start_row: usize,
count: usize,
) -> HeadlessGrid
pub fn get_viewport_content( &self, start_row: usize, count: usize, ) -> HeadlessGrid
Get viewport content
Sourcepub fn get_viewport_content_styled(
&self,
start_visual_row: usize,
count: usize,
) -> HeadlessGrid
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.styleswill contain the merged result ofinterval_treeand allstyle_layers
Sourcepub fn has_changed_since(&self, version: u64) -> bool
pub fn has_changed_since(&self, version: u64) -> bool
Check if state has changed since a version
Sourcepub fn mark_modified(&mut self, change_type: StateChangeType)
pub fn mark_modified(&mut self, change_type: StateChangeType)
Mark document as modified and increment version number
Sourcepub fn mark_saved(&mut self)
pub fn mark_saved(&mut self)
Mark document as unmodified (e.g., after saving)