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            cx.update_global(|store: &mut SettingsStore, 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            observe_keystrokes(cx);
 71            workspace.active_pane().update(cx, |pane, cx| {
 72                pane.toolbar().update(cx, |toolbar, cx| {
 73                    let buffer_search_bar = cx.new_view(BufferSearchBar::new);
 74                    toolbar.add_item(buffer_search_bar, cx);
 75
 76                    let project_search_bar = cx.new_view(|_| ProjectSearchBar::new());
 77                    toolbar.add_item(project_search_bar, cx);
 78                })
 79            });
 80            workspace.status_bar().update(cx, |status_bar, cx| {
 81                let vim_mode_indicator = cx.new_view(ModeIndicator::new);
 82                status_bar.add_right_item(vim_mode_indicator, cx);
 83            });
 84        });
 85
 86        Self { cx }
 87    }
 88
 89    pub fn update_view<F, T, R>(&mut self, view: View<T>, update: F) -> R
 90    where
 91        T: 'static,
 92        F: FnOnce(&mut T, &mut ViewContext<T>) -> R + 'static,
 93    {
 94        let window = self.window.clone();
 95        self.update_window(window, move |_, cx| view.update(cx, update))
 96            .unwrap()
 97    }
 98
 99    pub fn workspace<F, T>(&mut self, update: F) -> T
100    where
101        F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
102    {
103        self.cx.update_workspace(update)
104    }
105
106    pub fn enable_vim(&mut self) {
107        self.cx.update(|cx| {
108            cx.update_global(|store: &mut SettingsStore, cx| {
109                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
110            });
111        })
112    }
113
114    pub fn disable_vim(&mut self) {
115        self.cx.update(|cx| {
116            cx.update_global(|store: &mut SettingsStore, cx| {
117                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
118            });
119        })
120    }
121
122    pub fn mode(&mut self) -> Mode {
123        self.cx.read(|cx| cx.global::<Vim>().state().mode)
124    }
125
126    pub fn active_operator(&mut self) -> Option<Operator> {
127        self.cx
128            .read(|cx| cx.global::<Vim>().state().operator_stack.last().copied())
129    }
130
131    pub fn set_state(&mut self, text: &str, mode: Mode) {
132        let window = self.window;
133        self.cx.set_state(text);
134        self.update_window(window, |_, cx| {
135            Vim::update(cx, |vim, cx| {
136                vim.switch_mode(mode, true, cx);
137            })
138        })
139        .unwrap();
140        self.cx.cx.cx.run_until_parked();
141    }
142
143    #[track_caller]
144    pub fn assert_state(&mut self, text: &str, mode: Mode) {
145        self.assert_editor_state(text);
146        assert_eq!(self.mode(), mode, "{}", self.assertion_context());
147    }
148
149    pub fn assert_binding<const COUNT: usize>(
150        &mut self,
151        keystrokes: [&str; COUNT],
152        initial_state: &str,
153        initial_mode: Mode,
154        state_after: &str,
155        mode_after: Mode,
156    ) {
157        self.set_state(initial_state, initial_mode);
158        self.cx.simulate_keystrokes(keystrokes);
159        self.cx.assert_editor_state(state_after);
160        assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
161        assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
162    }
163}
164
165impl Deref for VimTestContext {
166    type Target = EditorLspTestContext;
167
168    fn deref(&self) -> &Self::Target {
169        &self.cx
170    }
171}
172
173impl DerefMut for VimTestContext {
174    fn deref_mut(&mut self) -> &mut Self::Target {
175        &mut self.cx
176    }
177}