2022-04-15 23:00:44 +00:00
|
|
|
use crate::{state::Mode, Vim};
|
2022-05-12 21:18:46 +00:00
|
|
|
use editor::{Autoscroll, Bias};
|
2022-04-10 21:29:45 +00:00
|
|
|
use gpui::{actions, MutableAppContext, ViewContext};
|
2022-03-25 02:24:36 +00:00
|
|
|
use language::SelectionGoal;
|
|
|
|
use workspace::Workspace;
|
|
|
|
|
2022-04-07 23:20:49 +00:00
|
|
|
actions!(vim, [NormalBefore]);
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.add_action(normal_before);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn normal_before(_: &mut Workspace, _: &NormalBefore, cx: &mut ViewContext<Workspace>) {
|
2022-04-15 23:00:44 +00:00
|
|
|
Vim::update(cx, |state, cx| {
|
2022-03-26 20:30:55 +00:00
|
|
|
state.update_active_editor(cx, |editor, cx| {
|
2022-05-12 21:18:46 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
2022-05-06 04:09:26 +00:00
|
|
|
s.move_cursors_with(|map, mut cursor, _| {
|
|
|
|
*cursor.column_mut() = cursor.column().saturating_sub(1);
|
|
|
|
(map.clip_point(cursor, Bias::Left), SelectionGoal::None)
|
|
|
|
});
|
2022-03-26 20:30:55 +00:00
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
});
|
2022-04-15 23:00:44 +00:00
|
|
|
state.switch_mode(Mode::Normal, cx);
|
2022-03-26 20:30:55 +00:00
|
|
|
})
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
2022-03-28 00:58:28 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-04-15 23:00:44 +00:00
|
|
|
use crate::{state::Mode, vim_test_context::VimTestContext};
|
2022-03-28 00:58:28 +00:00
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
|
2022-04-21 23:14:58 +00:00
|
|
|
let mut cx = VimTestContext::new(cx, true).await;
|
2022-03-28 00:58:28 +00:00
|
|
|
cx.simulate_keystroke("i");
|
|
|
|
assert_eq!(cx.mode(), Mode::Insert);
|
2022-04-15 23:00:44 +00:00
|
|
|
cx.simulate_keystrokes(["T", "e", "s", "t"]);
|
2022-03-28 00:58:28 +00:00
|
|
|
cx.assert_editor_state("Test|");
|
|
|
|
cx.simulate_keystroke("escape");
|
2022-04-15 23:00:44 +00:00
|
|
|
assert_eq!(cx.mode(), Mode::Normal);
|
2022-03-28 00:58:28 +00:00
|
|
|
cx.assert_editor_state("Tes|t");
|
|
|
|
}
|
|
|
|
}
|