Skip to main content

mathtex_editor_core/
command.rs

1//! Semantic commands mapped from host input before `Editor::exec`.
2
3use crate::doc::DocFragment;
4use crate::editor::UndoEntry;
5use crate::model::{Cursor, FracStyle, Mark, MatrixEnv, ScriptSlot, Symbol, UnderOverSpec, Variant};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8/// A movement direction.
9pub enum Dir {
10    /// Move left.
11    Left,
12    /// Move right.
13    Right,
14    /// Move up.
15    Up,
16    /// Move down.
17    Down,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21/// A side relative to an existing row or column.
22pub enum Side {
23    /// Insert before the target.
24    Before,
25    /// Insert after the target.
26    After,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30/// A direction where the caret leaves the math region.
31pub enum ExitDir {
32    /// Exit left.
33    Left,
34    /// Exit right.
35    Right,
36    /// Exit up.
37    Up,
38    /// Exit down.
39    Down,
40}
41
42/// A point in the host's render coordinate space.
43#[derive(Debug, Clone, Copy, PartialEq)]
44pub struct Point {
45    /// Horizontal position.
46    pub x: f64,
47    /// Vertical position.
48    pub y: f64,
49}
50
51#[derive(Debug, Clone, PartialEq)]
52/// An editor command.
53pub enum Command {
54    /// Move the cursor.
55    Move(Dir),
56    /// Move to the line start.
57    MoveLineStart,
58    /// Move to the line end.
59    MoveLineEnd,
60    /// Move to the next tab stop.
61    Tab,
62    /// Move to the previous tab stop.
63    ShiftTab,
64    /// Move to a rendered point.
65    MoveTo(Point),
66    /// Extend selection by direction.
67    Extend(Dir),
68    /// Extend selection to a rendered point.
69    ExtendTo(Point),
70    /// Select all content.
71    SelectAll,
72    /// Collapse the selection.
73    Collapse,
74    /// Insert a symbol atom.
75    InsertAtom(Symbol),
76    /// Insert an opaque host object atom carrying a host minted token.
77    InsertHostBox(u32),
78    /// Insert text.
79    InsertText(String),
80    /// Insert a fraction.
81    InsertFraction(FracStyle),
82    /// Insert a script slot.
83    InsertScript(ScriptSlot),
84    /// Insert a big operator with empty lower and upper limit slots.
85    InsertBigOp(Symbol),
86    /// Insert a radical with a degree slot and radicand.
87    InsertSqrt,
88    /// Insert paired delimiters.
89    InsertDelimiters {
90        /// Opening delimiter.
91        open: char,
92        /// Closing delimiter.
93        close: char,
94    },
95    /// Insert an accent.
96    InsertAccent(Mark),
97    /// Insert an under over construct.
98    InsertUnderOver(UnderOverSpec),
99    /// Insert styled content.
100    InsertStyled(Variant),
101    /// Insert a matrix.
102    InsertMatrix {
103        /// Matrix environment.
104        env: MatrixEnv,
105        /// Row count.
106        rows: usize,
107        /// Column count.
108        cols: usize,
109    },
110    /// Insert a document fragment.
111    InsertFragment(DocFragment),
112    /// Delete backward.
113    DeleteBackward,
114    /// Delete forward.
115    DeleteForward,
116    /// Insert a matrix row.
117    MatrixInsertRow(Side),
118    /// Delete the current matrix row.
119    MatrixDeleteRow,
120    /// Insert a matrix column.
121    MatrixInsertCol(Side),
122    /// Delete the current matrix column.
123    MatrixDeleteCol,
124    /// Apply a style.
125    ApplyStyle(Variant),
126    /// Commit the highlighted menu row or collapse when no menu is open.
127    Confirm,
128    /// Pick a visible menu row by index.
129    MenuSelect(usize),
130}
131
132/// The result of running a command.
133#[derive(Debug, Clone)]
134pub struct CommandResult {
135    /// The new cursor.
136    pub cursor: Cursor,
137    /// The optional undo entry for the host stack.
138    pub undo: Option<UndoEntry>,
139    /// The optional boundary exit signal.
140    pub exit: Option<ExitDir>,
141}