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