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