vim_test_context.rs

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