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