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