paperos 0.2.0

A programmable editor built on the Runact actor runtime
Documentation
use super::event_bus::EventBus;
use super::extension::Extension;
use runact::Runtime;
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Entry in the command registry.
pub struct CommandEntry {
    pub handler: Box<dyn CommandHandler>,
    pub description: &'static str,
}

#[allow(dead_code)]
pub struct EditorRuntime {
    runtime: Runtime,
    extensions: Vec<Box<dyn Extension>>,
    commands: HashMap<String, CommandEntry>,
    event_bus: Arc<RwLock<EventBus>>,
}

#[allow(dead_code)]
pub trait CommandHandler: Any + Send + Sync {
    fn handle(
        &mut self,
        args: &[String],
        runtime: &Runtime,
        buffer: Option<runact::ActorId>,
    ) -> Result<Option<String>, Box<dyn std::error::Error>>;
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

#[allow(dead_code)]
impl EditorRuntime {
    pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let runtime = Runtime::new()?;
        Ok(Self {
            runtime,
            extensions: Vec::new(),
            commands: HashMap::new(),
            event_bus: Arc::new(RwLock::new(EventBus::new())),
        })
    }

    pub fn add_extension(
        &mut self,
        mut ext: Box<dyn Extension>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        ext.init(&mut self.runtime)?;
        // Allow the extension to self-register commands.
        ext.register_commands(self)?;
        self.extensions.push(ext);
        Ok(())
    }

    /// Iterate over all registered extensions (for global_bindings, etc.).
    pub fn extensions_iter(&self) -> impl Iterator<Item = &dyn Extension> {
        self.extensions.iter().map(|e| e.as_ref() as &dyn Extension)
    }

    pub fn register_command(
        &mut self,
        name: String,
        handler: Box<dyn CommandHandler>,
        description: &'static str,
    ) {
        self.commands.insert(
            name,
            CommandEntry {
                handler,
                description,
            },
        );
    }

    /// Returns a sorted list of (name, description) for all registered commands.
    pub fn list_commands(&self) -> Vec<(&str, &str)> {
        let mut entries: Vec<(&str, &str)> = self
            .commands
            .iter()
            .map(|(name, entry)| (name.as_str(), entry.description))
            .collect();
        entries.sort_by_key(|(name, _)| *name);
        entries
    }

    pub fn run_command(
        &mut self,
        input: &str,
        buffer: Option<runact::ActorId>,
    ) -> Result<Option<String>, Box<dyn std::error::Error>> {
        let input = input.trim_start_matches(':');
        // Parse the command: first whitespace-separated token is the command name,
        // the rest of the string is the argument (preserved verbatim, including spaces).
        let parts: Vec<String> = if let Some(space_pos) = input.find(char::is_whitespace) {
            let cmd = input[..space_pos].to_string();
            let rest = input[space_pos + 1..].to_string();
            if rest.is_empty() {
                vec![cmd]
            } else {
                vec![cmd, rest]
            }
        } else {
            vec![input.to_string()]
        };
        if parts.is_empty() {
            return Ok(None);
        }
        let cmd = parts[0].as_str();
        let args = &parts;
        if let Some(entry) = self.commands.get_mut(cmd) {
            entry.handler.handle(args, &self.runtime, buffer)
        } else {
            Err(format!("Unknown command: {}", cmd).into())
        }
    }

    pub fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        std::thread::sleep(std::time::Duration::from_secs(1));
        Ok(())
    }

    pub fn shutdown(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        for ext in &mut self.extensions {
            ext.shutdown(&mut self.runtime)?;
        }
        self.runtime.shutdown()?;
        Ok(())
    }

    pub fn runtime(&self) -> &Runtime {
        &self.runtime
    }

    pub fn runtime_mut(&mut self) -> &mut Runtime {
        &mut self.runtime
    }

    /// Shared event bus for cross-extension state change notification.
    pub fn event_bus(&self) -> Arc<RwLock<EventBus>> {
        Arc::clone(&self.event_bus)
    }

    pub fn extension<T: Extension + 'static>(&self) -> Option<&T> {
        self.extensions
            .iter()
            .find_map(|ext| ext.as_any().downcast_ref::<T>())
    }

    pub fn extension_mut<T: Extension + 'static>(&mut self) -> Option<&mut T> {
        self.extensions
            .iter_mut()
            .find_map(|ext| ext.as_any_mut().downcast_mut::<T>())
    }
}

impl Default for EditorRuntime {
    fn default() -> Self {
        Self {
            runtime: Runtime::new().expect("failed to create runtime"),
            extensions: Vec::new(),
            commands: HashMap::new(),
            event_bus: Arc::new(RwLock::new(EventBus::new())),
        }
    }
}