vim_test_context.rs

  1use std::ops::{Deref, DerefMut};
  2
  3use editor::test::{
  4    editor_lsp_test_context::EditorLspTestContext, editor_test_context::EditorTestContext,
  5};
  6use futures::Future;
  7use gpui::{Context, View, VisualContext};
  8use lsp::request;
  9use search::BufferSearchBar;
 10
 11use crate::{state::Operator, *};
 12
 13pub struct VimTestContext<'a> {
 14    cx: EditorLspTestContext<'a>,
 15}
 16
 17impl<'a> VimTestContext<'a> {
 18    pub fn init(cx: &mut gpui::TestAppContext) {
 19        if cx.has_global::<Vim>() {
 20            dbg!("OOPS");
 21            return;
 22        }
 23        cx.update(|cx| {
 24            search::init(cx);
 25            let settings = SettingsStore::test(cx);
 26            cx.set_global(settings);
 27            command_palette::init(cx);
 28            crate::init(cx);
 29        });
 30    }
 31
 32    pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
 33        Self::init(cx);
 34        let lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
 35        Self::new_with_lsp(lsp, enabled)
 36    }
 37
 38    pub async fn new_typescript(cx: &'a mut gpui::TestAppContext) -> VimTestContext<'a> {
 39        Self::init(cx);
 40        Self::new_with_lsp(
 41            EditorLspTestContext::new_typescript(Default::default(), cx).await,
 42            true,
 43        )
 44    }
 45
 46    pub fn new_with_lsp(mut cx: EditorLspTestContext<'a>, enabled: bool) -> VimTestContext<'a> {
 47        cx.update(|cx| {
 48            cx.update_global(|store: &mut SettingsStore, cx| {
 49                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
 50            });
 51            settings::KeymapFile::load_asset("keymaps/default.json", cx).unwrap();
 52            settings::KeymapFile::load_asset("keymaps/vim.json", cx).unwrap();
 53        });
 54
 55        // Setup search toolbars and keypress hook
 56        cx.update_workspace(|workspace, cx| {
 57            observe_keystrokes(cx);
 58            workspace.active_pane().update(cx, |pane, cx| {
 59                pane.toolbar().update(cx, |toolbar, cx| {
 60                    let buffer_search_bar = cx.new_view(BufferSearchBar::new);
 61                    toolbar.add_item(buffer_search_bar, cx);
 62                    // todo!();
 63                    // let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
 64                    // toolbar.add_item(project_search_bar, cx);
 65                })
 66            });
 67            workspace.status_bar().update(cx, |status_bar, cx| {
 68                let vim_mode_indicator = cx.new_view(ModeIndicator::new);
 69                status_bar.add_right_item(vim_mode_indicator, cx);
 70            });
 71        });
 72
 73        Self { cx }
 74    }
 75
 76    pub fn update_view<F, T, R>(&mut self, view: View<T>, update: F) -> R
 77    where
 78        T: 'static,
 79        F: FnOnce(&mut T, &mut ViewContext<T>) -> R + 'static,
 80    {
 81        let window = self.window.clone();
 82        self.update_window(window, move |_, cx| view.update(cx, update))
 83            .unwrap()
 84    }
 85
 86    pub fn workspace<F, T>(&mut self, update: F) -> T
 87    where
 88        F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
 89    {
 90        self.cx.update_workspace(update)
 91    }
 92
 93    pub fn enable_vim(&mut self) {
 94        self.cx.update(|cx| {
 95            cx.update_global(|store: &mut SettingsStore, cx| {
 96                store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
 97            });
 98        })
 99    }
100
101    pub fn disable_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(false));
105            });
106        })
107    }
108
109    pub fn mode(&mut self) -> Mode {
110        self.cx.read(|cx| cx.global::<Vim>().state().mode)
111    }
112
113    pub fn active_operator(&mut self) -> Option<Operator> {
114        self.cx
115            .read(|cx| cx.global::<Vim>().state().operator_stack.last().copied())
116    }
117
118    pub fn set_state(&mut self, text: &str, mode: Mode) {
119        let window = self.window;
120        self.cx.set_state(text);
121        self.update_window(window, |_, cx| {
122            Vim::update(cx, |vim, cx| {
123                vim.switch_mode(mode, true, cx);
124            })
125        })
126        .unwrap();
127        self.cx.cx.cx.run_until_parked();
128    }
129
130    #[track_caller]
131    pub fn assert_state(&mut self, text: &str, mode: Mode) {
132        self.assert_editor_state(text);
133        assert_eq!(self.mode(), mode, "{}", self.assertion_context());
134    }
135
136    pub fn assert_binding<const COUNT: usize>(
137        &mut self,
138        keystrokes: [&str; COUNT],
139        initial_state: &str,
140        initial_mode: Mode,
141        state_after: &str,
142        mode_after: Mode,
143    ) {
144        self.set_state(initial_state, initial_mode);
145        self.cx.simulate_keystrokes(keystrokes);
146        self.cx.assert_editor_state(state_after);
147        assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
148        assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
149    }
150
151    pub fn handle_request<T, F, Fut>(
152        &self,
153        handler: F,
154    ) -> futures::channel::mpsc::UnboundedReceiver<()>
155    where
156        T: 'static + request::Request,
157        T::Params: 'static + Send,
158        F: 'static + Send + FnMut(lsp::Url, T::Params, gpui::AsyncAppContext) -> Fut,
159        Fut: 'static + Send + Future<Output = Result<T::Result>>,
160    {
161        self.cx.handle_request::<T, F, Fut>(handler)
162    }
163}
164
165impl<'a> Deref for VimTestContext<'a> {
166    type Target = EditorTestContext<'a>;
167
168    fn deref(&self) -> &Self::Target {
169        &self.cx
170    }
171}
172
173impl<'a> DerefMut for VimTestContext<'a> {
174    fn deref_mut(&mut self) -> &mut Self::Target {
175        &mut self.cx
176    }
177}