vim_test_context.rs

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