vim_test_context.rs

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