vim_test_context.rs

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