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 text.
77 InsertText(String),
78 /// Insert a fraction.
79 InsertFraction(FracStyle),
80 /// Insert a script slot.
81 InsertScript(ScriptSlot),
82 /// Insert a big operator with empty lower and upper limit slots.
83 InsertBigOp(Symbol),
84 /// Insert a radical with a degree slot and radicand.
85 InsertSqrt,
86 /// Insert paired delimiters.
87 InsertDelimiters {
88 /// Opening delimiter.
89 open: char,
90 /// Closing delimiter.
91 close: char,
92 },
93 /// Insert an accent.
94 InsertAccent(Mark),
95 /// Insert an under over construct.
96 InsertUnderOver(UnderOverSpec),
97 /// Insert styled content.
98 InsertStyled(Variant),
99 /// Insert a matrix.
100 InsertMatrix {
101 /// Matrix environment.
102 env: MatrixEnv,
103 /// Row count.
104 rows: usize,
105 /// Column count.
106 cols: usize,
107 },
108 /// Insert a document fragment.
109 InsertFragment(DocFragment),
110 /// Delete backward.
111 DeleteBackward,
112 /// Delete forward.
113 DeleteForward,
114 /// Insert a matrix row.
115 MatrixInsertRow(Side),
116 /// Delete the current matrix row.
117 MatrixDeleteRow,
118 /// Insert a matrix column.
119 MatrixInsertCol(Side),
120 /// Delete the current matrix column.
121 MatrixDeleteCol,
122 /// Apply a style.
123 ApplyStyle(Variant),
124 /// Commit the highlighted menu row or collapse when no menu is open.
125 Confirm,
126 /// Pick a visible menu row by index.
127 MenuSelect(usize),
128}
129
130/// The result of running a command.
131#[derive(Debug, Clone)]
132pub struct CommandResult {
133 /// The new cursor.
134 pub cursor: Cursor,
135 /// The optional undo entry for the host stack.
136 pub undo: Option<UndoEntry>,
137 /// The optional boundary exit signal.
138 pub exit: Option<ExitDir>,
139}