1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{
client::ClientHandle,
editor::{Editor, EditorContext, EditorControlFlow, KeysIterator},
};
mod command;
mod insert;
mod normal;
pub(crate) mod picker;
pub(crate) mod read_line;
pub(crate) trait ModeState {
fn on_enter(editor: &mut Editor);
fn on_exit(editor: &mut Editor);
fn on_client_keys(
ctx: &mut EditorContext,
client_handle: ClientHandle,
keys: &mut KeysIterator,
) -> Option<EditorControlFlow>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModeKind {
Normal,
Insert,
Command,
ReadLine,
Picker,
}
impl Default for ModeKind {
fn default() -> Self {
Self::Normal
}
}
#[derive(Default)]
pub struct Mode {
kind: ModeKind,
pub normal_state: normal::State,
pub insert_state: insert::State,
pub command_state: command::State,
pub read_line_state: read_line::State,
pub picker_state: picker::State,
}
impl Mode {
pub fn kind(&self) -> ModeKind {
self.kind
}
pub(crate) fn change_to(editor: &mut Editor, next: ModeKind) {
if editor.mode.kind == next {
return;
}
match editor.mode.kind {
ModeKind::Normal => normal::State::on_exit(editor),
ModeKind::Insert => insert::State::on_exit(editor),
ModeKind::Command => command::State::on_exit(editor),
ModeKind::ReadLine => read_line::State::on_exit(editor),
ModeKind::Picker => picker::State::on_exit(editor),
}
editor.mode.kind = next;
match editor.mode.kind {
ModeKind::Normal => normal::State::on_enter(editor),
ModeKind::Insert => insert::State::on_enter(editor),
ModeKind::Command => command::State::on_enter(editor),
ModeKind::ReadLine => read_line::State::on_enter(editor),
ModeKind::Picker => picker::State::on_enter(editor),
}
}
pub(crate) fn on_client_keys(
ctx: &mut EditorContext,
client_handle: ClientHandle,
keys: &mut KeysIterator,
) -> Option<EditorControlFlow> {
match ctx.editor.mode.kind {
ModeKind::Normal => normal::State::on_client_keys(ctx, client_handle, keys),
ModeKind::Insert => insert::State::on_client_keys(ctx, client_handle, keys),
ModeKind::Command => command::State::on_client_keys(ctx, client_handle, keys),
ModeKind::ReadLine => read_line::State::on_client_keys(ctx, client_handle, keys),
ModeKind::Picker => picker::State::on_client_keys(ctx, client_handle, keys),
}
}
}