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 SetLanguage(String),
96 SetCursorStyle(CursorStyle),
97 SetTheme(String),
99 Wait(u64),
102 Message(String),
103}
104
105impl EditorAction {
106 pub fn movement(name: &str, extend: bool) -> Option<EditorAction> {
108 Some(match name {
109 "left" => EditorAction::MoveLeft { extend },
110 "right" => EditorAction::MoveRight { extend },
111 "up" => EditorAction::MoveUp { extend },
112 "down" => EditorAction::MoveDown { extend },
113 "word_left" => EditorAction::MoveWordBackward { extend },
114 "word_right" => EditorAction::MoveWordForward { extend },
115 "line_start" => EditorAction::MoveLineStart { extend },
116 "line_end" => EditorAction::MoveLineEnd { extend },
117 "doc_start" => EditorAction::MoveDocStart { extend },
118 "doc_end" => EditorAction::MoveDocEnd { extend },
119 _ => return None,
120 })
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 #[test]
129 fn parses_cursor_styles() {
130 assert_eq!(CursorStyle::parse("bar"), Some(CursorStyle::Bar));
131 assert_eq!(CursorStyle::parse("Block"), Some(CursorStyle::Block));
132 assert_eq!(
133 CursorStyle::parse(" underline "),
134 Some(CursorStyle::Underline)
135 );
136 assert_eq!(CursorStyle::parse("ibeam"), Some(CursorStyle::Bar));
137 assert_eq!(CursorStyle::parse("nope"), None);
138 assert_eq!(CursorStyle::Bar.name(), "bar");
139 assert_eq!(CursorStyle::Block.name(), "block");
140 assert_eq!(CursorStyle::Underline.name(), "underline");
141 }
142}