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 futures::Future;
  7use gpui::{Context, View, VisualContext};
  8use lsp::request;
  9use search::BufferSearchBar;
 10
 11use crate::{state::Operator, *};
 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 lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
 20        Self::new_with_lsp(lsp, enabled)
 21    }
 22
 23    pub async fn new_typescript(cx: &'a mut gpui::TestAppContext) -> VimTestContext<'a> {
 24        Self::new_with_lsp(
 25            EditorLspTestContext::new_typescript(Default::default(), cx).await,
 26            true,
 27        )
 28    }
 29
 30    pub fn new_with_lsp(mut cx: EditorLspTestContext<'a>, enabled: bool) -> VimTestContext<'a> {
 31        cx.update(|cx| {
 32            search::init(cx);
 33            crate::init(cx);
 34            command_palette::init(cx);
 35        });
 36
 37        cx.update(|cx| {
 38            cx.update_global(|store: &mut SettingsStore, cx| {
 39                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
 40            });
 41            settings::KeymapFile::load_asset("keymaps/default.json", cx).unwrap();
 42            settings::KeymapFile::load_asset("keymaps/vim.json", cx).unwrap();
 43        });
 44
 45        // Setup search toolbars and keypress hook
 46        cx.update_workspace(|workspace, cx| {
 47            observe_keystrokes(cx);
 48            workspace.active_pane().update(cx, |pane, cx| {
 49                pane.toolbar().update(cx, |toolbar, cx| {
 50                    let buffer_search_bar = cx.build_view(BufferSearchBar::new);
 51                    toolbar.add_item(buffer_search_bar, cx);
 52                    // todo!();
 53                    // let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
 54                    // toolbar.add_item(project_search_bar, cx);
 55                })
 56            });
 57            workspace.status_bar().update(cx, |status_bar, cx| {
 58                let vim_mode_indicator = cx.build_view(ModeIndicator::new);
 59                status_bar.add_right_item(vim_mode_indicator, cx);
 60            });
 61        });
 62
 63        Self { cx }
 64    }
 65
 66    pub fn update_view<F, T, R>(&mut self, view: View<T>, update: F) -> R
 67    where
 68        F: FnOnce(&mut T, &mut ViewContext<T>) -> R,
 69    {
 70        self.update_window(self.window, |_, cx| view.update(cx, update))
 71            .unwrap()
 72    }
 73
 74    pub fn workspace<F, T>(&mut self, update: F) -> T
 75    where
 76        F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
 77    {
 78        self.update_window(self.window, |_, cx| self.cx.workspace.update(cx, update))
 79            .unwrap()
 80    }
 81
 82    pub fn enable_vim(&mut self) {
 83        self.cx.update(|cx| {
 84            cx.update_global(|store: &mut SettingsStore, cx| {
 85                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
 86            });
 87        })
 88    }
 89
 90    pub fn disable_vim(&mut self) {
 91        self.cx.update(|cx| {
 92            cx.update_global(|store: &mut SettingsStore, cx| {
 93                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
 94            });
 95        })
 96    }
 97
 98    pub fn mode(&mut self) -> Mode {
 99        self.cx.read(|cx| cx.global::<Vim>().state().mode)
100    }
101
102    pub fn active_operator(&mut self) -> Option<Operator> {
103        self.cx
104            .read(|cx| cx.global::<Vim>().state().operator_stack.last().copied())
105    }
106
107    pub fn set_state(&mut self, text: &str, mode: Mode) {
108        let window = self.window;
109        self.cx.set_state(text);
110        self.update_window(window, |_, cx| {
111            Vim::update(cx, |vim, cx| {
112                vim.switch_mode(mode, true, cx);
113            })
114        });
115        self.cx.cx.cx.run_until_parked();
116    }
117
118    #[track_caller]
119    pub fn assert_state(&mut self, text: &str, mode: Mode) {
120        self.assert_editor_state(text);
121        assert_eq!(self.mode(), mode, "{}", self.assertion_context());
122    }
123
124    pub fn assert_binding<const COUNT: usize>(
125        &mut self,
126        keystrokes: [&str; COUNT],
127        initial_state: &str,
128        initial_mode: Mode,
129        state_after: &str,
130        mode_after: Mode,
131    ) {
132        self.set_state(initial_state, initial_mode);
133        self.cx.simulate_keystrokes(keystrokes);
134        self.cx.assert_editor_state(state_after);
135        assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
136        assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
137    }
138
139    pub fn handle_request<T, F, Fut>(
140        &self,
141        handler: F,
142    ) -> futures::channel::mpsc::UnboundedReceiver<()>
143    where
144        T: 'static + request::Request,
145        T::Params: 'static + Send,
146        F: 'static + Send + FnMut(lsp::Url, T::Params, gpui::AsyncAppContext) -> Fut,
147        Fut: 'static + Send + Future<Output = Result<T::Result>>,
148    {
149        self.cx.handle_request::<T, F, Fut>(handler)
150    }
151}
152
153impl<'a> Deref for VimTestContext<'a> {
154    type Target = EditorTestContext<'a>;
155
156    fn deref(&self) -> &Self::Target {
157        &self.cx
158    }
159}
160
161impl<'a> DerefMut for VimTestContext<'a> {
162    fn deref_mut(&mut self) -> &mut Self::Target {
163        &mut self.cx
164    }
165}