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