mathtex-editor-core 0.1.0

Headless core of the mathtex structural math editor: model, operations, navigation, selection, IR matching
Documentation
//! Semantic commands mapped from host input before `Editor::exec`.

use crate::doc::DocFragment;
use crate::editor::UndoEntry;
use crate::model::{Cursor, FracStyle, Mark, MatrixEnv, ScriptSlot, Symbol, UnderOverSpec, Variant};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// A movement direction.
pub enum Dir {
    /// Move left.
    Left,
    /// Move right.
    Right,
    /// Move up.
    Up,
    /// Move down.
    Down,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// A side relative to an existing row or column.
pub enum Side {
    /// Insert before the target.
    Before,
    /// Insert after the target.
    After,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// A direction where the caret leaves the math region.
pub enum ExitDir {
    /// Exit left.
    Left,
    /// Exit right.
    Right,
    /// Exit up.
    Up,
    /// Exit down.
    Down,
}

/// A point in the host's render coordinate space.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
    /// Horizontal position.
    pub x: f64,
    /// Vertical position.
    pub y: f64,
}

#[derive(Debug, Clone, PartialEq)]
/// An editor command.
pub enum Command {
    /// Move the cursor.
    Move(Dir),
    /// Move to the line start.
    MoveLineStart,
    /// Move to the line end.
    MoveLineEnd,
    /// Move to the next tab stop.
    Tab,
    /// Move to the previous tab stop.
    ShiftTab,
    /// Move to a rendered point.
    MoveTo(Point),
    /// Extend selection by direction.
    Extend(Dir),
    /// Extend selection to a rendered point.
    ExtendTo(Point),
    /// Select all content.
    SelectAll,
    /// Collapse the selection.
    Collapse,
    /// Insert a symbol atom.
    InsertAtom(Symbol),
    /// Insert text.
    InsertText(String),
    /// Insert a fraction.
    InsertFraction(FracStyle),
    /// Insert a script slot.
    InsertScript(ScriptSlot),
    /// Insert a big operator with empty lower and upper limit slots.
    InsertBigOp(Symbol),
    /// Insert a radical with a degree slot and radicand.
    InsertSqrt,
    /// Insert paired delimiters.
    InsertDelimiters {
        /// Opening delimiter.
        open: char,
        /// Closing delimiter.
        close: char,
    },
    /// Insert an accent.
    InsertAccent(Mark),
    /// Insert an under over construct.
    InsertUnderOver(UnderOverSpec),
    /// Insert styled content.
    InsertStyled(Variant),
    /// Insert a matrix.
    InsertMatrix {
        /// Matrix environment.
        env: MatrixEnv,
        /// Row count.
        rows: usize,
        /// Column count.
        cols: usize,
    },
    /// Insert a document fragment.
    InsertFragment(DocFragment),
    /// Delete backward.
    DeleteBackward,
    /// Delete forward.
    DeleteForward,
    /// Insert a matrix row.
    MatrixInsertRow(Side),
    /// Delete the current matrix row.
    MatrixDeleteRow,
    /// Insert a matrix column.
    MatrixInsertCol(Side),
    /// Delete the current matrix column.
    MatrixDeleteCol,
    /// Apply a style.
    ApplyStyle(Variant),
    /// Commit the highlighted menu row or collapse when no menu is open.
    Confirm,
    /// Pick a visible menu row by index.
    MenuSelect(usize),
}

/// The result of running a command.
#[derive(Debug, Clone)]
pub struct CommandResult {
    /// The new cursor.
    pub cursor: Cursor,
    /// The optional undo entry for the host stack.
    pub undo: Option<UndoEntry>,
    /// The optional boundary exit signal.
    pub exit: Option<ExitDir>,
}