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