normal.rs

 1use editor::{movement, Bias};
 2use gpui::{action, keymap::Binding, MutableAppContext, ViewContext};
 3use language::SelectionGoal;
 4use workspace::Workspace;
 5
 6use crate::{editor_utils::VimEditorExt, 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_active_editor(cx, |editor, cx| {
32        editor.adjusted_move_cursors(cx, |map, mut cursor, _| {
33            *cursor.column_mut() = cursor.column().saturating_sub(1);
34            (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
35        });
36    });
37}
38
39fn move_down(_: &mut Workspace, _: &MoveDown, cx: &mut ViewContext<Workspace>) {
40    VimState::update_active_editor(cx, |editor, cx| {
41        editor.adjusted_move_cursors(cx, movement::down);
42    });
43}
44
45fn move_up(_: &mut Workspace, _: &MoveUp, cx: &mut ViewContext<Workspace>) {
46    VimState::update_active_editor(cx, |editor, cx| {
47        editor.adjusted_move_cursors(cx, movement::up);
48    });
49}
50
51fn move_right(_: &mut Workspace, _: &MoveRight, cx: &mut ViewContext<Workspace>) {
52    VimState::update_active_editor(cx, |editor, cx| {
53        editor.adjusted_move_cursors(cx, |map, mut cursor, _| {
54            *cursor.column_mut() += 1;
55            (map.clip_point(cursor, Bias::Right), SelectionGoal::None)
56        });
57    });
58}