vim_test_context.rs

  1use std::ops::{Deref, DerefMut};
  2
  3use editor::test::{
  4    editor_lsp_test_context::EditorLspTestContext, editor_test_context::EditorTestContext,
  5};
  6use gpui::ContextHandle;
  7use search::{BufferSearchBar, ProjectSearchBar};
  8
  9use crate::{state::Operator, *};
 10
 11use super::VimBindingTestContext;
 12
 13pub struct VimTestContext<'a> {
 14    cx: EditorLspTestContext<'a>,
 15}
 16
 17impl<'a> VimTestContext<'a> {
 18    pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
 19        let mut cx = EditorLspTestContext::new_rust(Default::default(), cx).await;
 20        cx.update(|cx| {
 21            search::init(cx);
 22            crate::init(cx);
 23
 24            cx.update_global(|store: &mut SettingsStore, cx| {
 25                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
 26            });
 27
 28            settings::KeymapFileContent::load("keymaps/vim.json", cx).unwrap();
 29        });
 30
 31        // Setup search toolbars and keypress hook
 32        cx.update_workspace(|workspace, cx| {
 33            observe_keystrokes(cx);
 34            workspace.active_pane().update(cx, |pane, cx| {
 35                pane.toolbar().update(cx, |toolbar, cx| {
 36                    let buffer_search_bar = cx.add_view(BufferSearchBar::new);
 37                    toolbar.add_item(buffer_search_bar, cx);
 38                    let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
 39                    toolbar.add_item(project_search_bar, cx);
 40                })
 41            });
 42        });
 43
 44        Self { cx }
 45    }
 46
 47    pub fn workspace<F, T>(&mut self, read: F) -> T
 48    where
 49        F: FnOnce(&Workspace, &ViewContext<Workspace>) -> T,
 50    {
 51        self.cx.workspace.read_with(self.cx.cx.cx, read)
 52    }
 53
 54    pub fn enable_vim(&mut self) {
 55        self.cx.update(|cx| {
 56            cx.update_global(|store: &mut SettingsStore, cx| {
 57                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
 58            });
 59        })
 60    }
 61
 62    pub fn disable_vim(&mut self) {
 63        self.cx.update(|cx| {
 64            cx.update_global(|store: &mut SettingsStore, cx| {
 65                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
 66            });
 67        })
 68    }
 69
 70    pub fn mode(&mut self) -> Mode {
 71        self.cx.read(|cx| cx.global::<Vim>().state.mode)
 72    }
 73
 74    pub fn active_operator(&mut self) -> Option<Operator> {
 75        self.cx
 76            .read(|cx| cx.global::<Vim>().state.operator_stack.last().copied())
 77    }
 78
 79    pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
 80        let window_id = self.window_id;
 81        self.update_window(window_id, |cx| {
 82            Vim::update(cx, |vim, cx| {
 83                vim.switch_mode(mode, false, cx);
 84            })
 85        });
 86        self.cx.set_state(text)
 87    }
 88
 89    pub fn assert_state(&mut self, text: &str, mode: Mode) {
 90        self.assert_editor_state(text);
 91        assert_eq!(self.mode(), mode, "{}", self.assertion_context());
 92    }
 93
 94    pub fn assert_binding<const COUNT: usize>(
 95        &mut self,
 96        keystrokes: [&str; COUNT],
 97        initial_state: &str,
 98        initial_mode: Mode,
 99        state_after: &str,
100        mode_after: Mode,
101    ) {
102        self.set_state(initial_state, initial_mode);
103        self.cx.simulate_keystrokes(keystrokes);
104        self.cx.assert_editor_state(state_after);
105        assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
106        assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
107    }
108
109    pub fn binding<const COUNT: usize>(
110        mut self,
111        keystrokes: [&'static str; COUNT],
112    ) -> VimBindingTestContext<'a, COUNT> {
113        let mode = self.mode();
114        VimBindingTestContext::new(keystrokes, mode, mode, self)
115    }
116}
117
118impl<'a> Deref for VimTestContext<'a> {
119    type Target = EditorTestContext<'a>;
120
121    fn deref(&self) -> &Self::Target {
122        &self.cx
123    }
124}
125
126impl<'a> DerefMut for VimTestContext<'a> {
127    fn deref_mut(&mut self) -> &mut Self::Target {
128        &mut self.cx
129    }
130}