vim_test_context.rs

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