normal.rs

 1use editor::{movement, Bias};
 2use gpui::{action, keymap::Binding, MutableAppContext, ViewContext};
 3use language::SelectionGoal;
 4use workspace::Workspace;
 5
 6use crate::{Mode, SwitchMode, VimState};
 7
 8action!(InsertBefore);
 9action!(MoveLeft);
10action!(MoveDown);
11action!(MoveUp);
12action!(MoveRight);
13
14pub fn init(cx: &mut MutableAppContext) {
15    let context = Some("Editor && vim_mode == normal");
16    cx.add_bindings(vec![
17        Binding::new("i", SwitchMode(Mode::Insert), context),
18        Binding::new("h", MoveLeft, context),
19        Binding::new("j", MoveDown, context),
20        Binding::new("k", MoveUp, context),
21        Binding::new("l", MoveRight, context),
22    ]);
23
24    cx.add_action(move_left);
25    cx.add_action(move_down);
26    cx.add_action(move_up);
27    cx.add_action(move_right);
28}
29
30fn move_left(_: &mut Workspace, _: &MoveLeft, cx: &mut ViewContext<Workspace>) {
31    VimState::update_global(cx, |state, cx| {
32        state.update_active_editor(cx, |editor, cx| {
33            editor.move_cursors(cx, |map, mut cursor, _| {
34                *cursor.column_mut() = cursor.column().saturating_sub(1);
35                (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
36            });
37        });
38    })
39}
40
41fn move_down(_: &mut Workspace, _: &MoveDown, cx: &mut ViewContext<Workspace>) {
42    VimState::update_global(cx, |state, cx| {
43        state.update_active_editor(cx, |editor, cx| {
44            editor.move_cursors(cx, movement::down);
45        });
46    });
47}
48
49fn move_up(_: &mut Workspace, _: &MoveUp, cx: &mut ViewContext<Workspace>) {
50    VimState::update_global(cx, |state, cx| {
51        state.update_active_editor(cx, |editor, cx| {
52            editor.move_cursors(cx, movement::up);
53        });
54    });
55}
56
57fn move_right(_: &mut Workspace, _: &MoveRight, cx: &mut ViewContext<Workspace>) {
58    VimState::update_global(cx, |state, cx| {
59        state.update_active_editor(cx, |editor, cx| {
60            editor.move_cursors(cx, |map, mut cursor, _| {
61                *cursor.column_mut() += 1;
62                (map.clip_point(cursor, Bias::Right), SelectionGoal::None)
63            });
64        });
65    });
66}