EditorCommand

Trait EditorCommand 

Source
pub trait EditorCommand:
    Debug
    + Send
    + Sync {
    // Required methods
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult>;
    fn description(&self) -> &str;

    // Provided methods
    fn modifies_content(&self) -> bool { ... }
    fn memory_usage(&self) -> usize { ... }
}
Expand description

Trait for editor commands that can be executed and undone

All commands implement this trait to provide a consistent interface for execution, undo/redo, and introspection.

§Examples

Creating a custom command:

use ass_editor::{EditorCommand, EditorDocument, CommandResult, Result, Position, Range};

#[derive(Debug)]
struct UppercaseCommand {
    description: String,
}

impl UppercaseCommand {
    fn new() -> Self {
        Self {
            description: "Convert to uppercase".to_string(),
        }
    }
}

impl EditorCommand for UppercaseCommand {
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
        let text = document.text().to_uppercase();
        let range = Range::new(Position::new(0), Position::new(document.len()));
        document.replace(range, &text)?;
        Ok(CommandResult::success().with_message("Text converted to uppercase".to_string()))
    }

    fn description(&self) -> &str {
        &self.description
    }
}

Required Methods§

Source

fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult>

Execute the command on the given document

Returns a result indicating success/failure and metadata about the operation for undo/redo tracking.

Source

fn description(&self) -> &str

Get a human-readable description of the command

Provided Methods§

Source

fn modifies_content(&self) -> bool

Check if this command modifies document content

Used to determine if the document should be marked as modified and whether to save undo state.

Source

fn memory_usage(&self) -> usize

Get the estimated memory usage of this command

Used for memory management in undo stacks with limited capacity. Default implementation provides a conservative estimate.

Implementors§