fast_text_plugin/
action.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CursorStyle {
11 Block,
13 Bar,
15 Underline,
17}
18
19impl CursorStyle {
20 pub fn parse(name: &str) -> Option<CursorStyle> {
22 Some(match name.trim().to_ascii_lowercase().as_str() {
23 "block" | "box" => CursorStyle::Block,
24 "bar" | "line" | "beam" | "ibeam" => CursorStyle::Bar,
25 "underline" | "under" | "underscore" => CursorStyle::Underline,
26 _ => return None,
27 })
28 }
29
30 pub fn name(self) -> &'static str {
32 match self {
33 CursorStyle::Block => "block",
34 CursorStyle::Bar => "bar",
35 CursorStyle::Underline => "underline",
36 }
37 }
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum EditorAction {
43 Insert(String),
44 InsertAt {
45 at: usize,
46 text: String,
47 },
48 DeleteRange {
49 start: usize,
50 end: usize,
51 },
52 DeleteSelection,
53 Backspace,
54 DeleteForward,
55 InsertNewline,
56 SetCursor(usize),
57 Select {
58 anchor: usize,
59 head: usize,
60 },
61 SelectAll,
62 MoveLeft {
63 extend: bool,
64 },
65 MoveRight {
66 extend: bool,
67 },
68 MoveUp {
69 extend: bool,
70 },
71 MoveDown {
72 extend: bool,
73 },
74 MoveWordForward {
75 extend: bool,
76 },
77 MoveWordBackward {
78 extend: bool,
79 },
80 MoveLineStart {
81 extend: bool,
82 },
83 MoveLineEnd {
84 extend: bool,
85 },
86 MoveDocStart {
87 extend: bool,
88 },
89 MoveDocEnd {
90 extend: bool,
91 },
92 Undo,
93 Redo,
94 Save,
95 Format,
97 SetLanguage(String),
98 SetCursorStyle(CursorStyle),
99 SetTheme(String),
101 SetStatus(Option<String>),
104 Wait(u64),
107 Message(String),
108}
109
110impl EditorAction {
111 pub fn movement(name: &str, extend: bool) -> Option<EditorAction> {
113 Some(match name {
114 "left" => EditorAction::MoveLeft { extend },
115 "right" => EditorAction::MoveRight { extend },
116 "up" => EditorAction::MoveUp { extend },
117 "down" => EditorAction::MoveDown { extend },
118 "word_left" => EditorAction::MoveWordBackward { extend },
119 "word_right" => EditorAction::MoveWordForward { extend },
120 "line_start" => EditorAction::MoveLineStart { extend },
121 "line_end" => EditorAction::MoveLineEnd { extend },
122 "doc_start" => EditorAction::MoveDocStart { extend },
123 "doc_end" => EditorAction::MoveDocEnd { extend },
124 _ => return None,
125 })
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn parses_cursor_styles() {
135 assert_eq!(CursorStyle::parse("bar"), Some(CursorStyle::Bar));
136 assert_eq!(CursorStyle::parse("Block"), Some(CursorStyle::Block));
137 assert_eq!(
138 CursorStyle::parse(" underline "),
139 Some(CursorStyle::Underline)
140 );
141 assert_eq!(CursorStyle::parse("ibeam"), Some(CursorStyle::Bar));
142 assert_eq!(CursorStyle::parse("nope"), None);
143 assert_eq!(CursorStyle::Bar.name(), "bar");
144 assert_eq!(CursorStyle::Block.name(), "block");
145 assert_eq!(CursorStyle::Underline.name(), "underline");
146 }
147}