Skip to main content

fast_text_plugin/
action.rs

1//! An editor action requested by a plugin.
2//!
3//! Plugins never touch the editor directly. They *describe* changes by pushing
4//! `EditorAction`s into a queue, and the host applies them in order. This keeps
5//! scripting fully decoupled from the editor's internals (and from Rust's
6//! borrow checker).
7
8/// The shape of the terminal cursor.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CursorStyle {
11    /// A filled block (the classic terminal cursor).
12    Block,
13    /// A thin vertical bar (the usual editor default).
14    Bar,
15    /// An underscore under the character.
16    Underline,
17}
18
19impl CursorStyle {
20    /// Parses a friendly spelling like `bar`, `block`, or `underline`.
21    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    /// The canonical spelling of this style.
31    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/// A movement or edit a plugin wants performed.
41#[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    /// Switch to the named color theme (built-in or defined by a plugin).
98    SetTheme(String),
99    /// Pause automated playback for this many milliseconds before running the
100    /// next action. Hosts without timed playback may treat it immediately.
101    Wait(u64),
102    Message(String),
103}
104
105impl EditorAction {
106    /// Parses a movement name into an action.
107    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}