1use std::ops::{Deref, DerefMut};
2
3use editor::test::editor_lsp_test_context::EditorLspTestContext;
4use gpui::{Context, Entity, SemanticVersion, UpdateGlobal};
5use search::{BufferSearchBar, project_search::ProjectSearchBar};
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::<VimGlobals>() {
16 return;
17 }
18 env_logger::try_init().ok();
19 cx.update(|cx| {
20 let settings = SettingsStore::test(cx);
21 cx.set_global(settings);
22 release_channel::init(SemanticVersion::default(), cx);
23 command_palette::init(cx);
24 project_panel::init(cx);
25 git_ui::init(cx);
26 crate::init(cx);
27 search::init(cx);
28 theme::init(theme::LoadThemes::JustBase, cx);
29 settings_ui::init(cx);
30 });
31 }
32
33 pub async fn new(cx: &mut gpui::TestAppContext, enabled: bool) -> VimTestContext {
34 Self::init(cx);
35 let lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
36 Self::new_with_lsp(lsp, enabled)
37 }
38
39 pub async fn new_html(cx: &mut gpui::TestAppContext) -> VimTestContext {
40 Self::init(cx);
41 Self::new_with_lsp(EditorLspTestContext::new_html(cx).await, true)
42 }
43
44 pub async fn new_typescript(cx: &mut gpui::TestAppContext) -> VimTestContext {
45 Self::init(cx);
46 Self::new_with_lsp(
47 EditorLspTestContext::new_typescript(
48 lsp::ServerCapabilities {
49 completion_provider: Some(lsp::CompletionOptions {
50 trigger_characters: Some(vec![".".to_string()]),
51 ..Default::default()
52 }),
53 rename_provider: Some(lsp::OneOf::Right(lsp::RenameOptions {
54 prepare_provider: Some(true),
55 work_done_progress_options: Default::default(),
56 })),
57 definition_provider: Some(lsp::OneOf::Left(true)),
58 ..Default::default()
59 },
60 cx,
61 )
62 .await,
63 true,
64 )
65 }
66
67 pub async fn new_tsx(cx: &mut gpui::TestAppContext) -> VimTestContext {
68 Self::init(cx);
69 Self::new_with_lsp(
70 EditorLspTestContext::new_tsx(
71 lsp::ServerCapabilities {
72 completion_provider: Some(lsp::CompletionOptions {
73 trigger_characters: Some(vec![".".to_string()]),
74 ..Default::default()
75 }),
76 rename_provider: Some(lsp::OneOf::Right(lsp::RenameOptions {
77 prepare_provider: Some(true),
78 work_done_progress_options: Default::default(),
79 })),
80 ..Default::default()
81 },
82 cx,
83 )
84 .await,
85 true,
86 )
87 }
88
89 pub fn init_keybindings(enabled: bool, cx: &mut App) {
90 SettingsStore::update_global(cx, |store, cx| {
91 store.update_user_settings(cx, |s| s.vim_mode = Some(enabled));
92 });
93 let mut default_key_bindings = settings::KeymapFile::load_asset_allow_partial_failure(
94 "keymaps/default-macos.json",
95 cx,
96 )
97 .unwrap();
98 for key_binding in &mut default_key_bindings {
99 key_binding.set_meta(settings::KeybindSource::Default.meta());
100 }
101 cx.bind_keys(default_key_bindings);
102 if enabled {
103 let vim_key_bindings = settings::KeymapFile::load_asset(
104 "keymaps/vim.json",
105 Some(settings::KeybindSource::Vim),
106 cx,
107 )
108 .unwrap();
109 cx.bind_keys(vim_key_bindings);
110 }
111 }
112
113 pub fn new_with_lsp(mut cx: EditorLspTestContext, enabled: bool) -> VimTestContext {
114 cx.update(|_, cx| {
115 Self::init_keybindings(enabled, cx);
116 });
117
118 // Setup search toolbars and keypress hook
119 cx.update_workspace(|workspace, window, cx| {
120 workspace.active_pane().update(cx, |pane, cx| {
121 pane.toolbar().update(cx, |toolbar, cx| {
122 let buffer_search_bar = cx.new(|cx| BufferSearchBar::new(None, window, cx));
123 toolbar.add_item(buffer_search_bar, window, cx);
124
125 let project_search_bar = cx.new(|_| ProjectSearchBar::new());
126 toolbar.add_item(project_search_bar, window, cx);
127 })
128 });
129 workspace.status_bar().update(cx, |status_bar, cx| {
130 let vim_mode_indicator = cx.new(|cx| ModeIndicator::new(window, cx));
131 status_bar.add_right_item(vim_mode_indicator, window, cx);
132 });
133 });
134
135 Self { cx }
136 }
137
138 pub fn update_entity<F, T, R>(&mut self, entity: Entity<T>, update: F) -> R
139 where
140 T: 'static,
141 F: FnOnce(&mut T, &mut Window, &mut Context<T>) -> R + 'static,
142 {
143 let window = self.window;
144 self.update_window(window, move |_, window, cx| {
145 entity.update(cx, |t, cx| update(t, window, cx))
146 })
147 .unwrap()
148 }
149
150 pub fn workspace<F, T>(&mut self, update: F) -> T
151 where
152 F: FnOnce(&mut Workspace, &mut Window, &mut Context<Workspace>) -> T,
153 {
154 self.cx.update_workspace(update)
155 }
156
157 pub fn enable_vim(&mut self) {
158 self.cx.update(|_, cx| {
159 SettingsStore::update_global(cx, |store, cx| {
160 store.update_user_settings(cx, |s| s.vim_mode = Some(true));
161 });
162 })
163 }
164
165 pub fn disable_vim(&mut self) {
166 self.cx.update(|_, cx| {
167 SettingsStore::update_global(cx, |store, cx| {
168 store.update_user_settings(cx, |s| s.vim_mode = Some(false));
169 });
170 })
171 }
172
173 pub fn enable_helix(&mut self) {
174 self.cx.update(|_, cx| {
175 SettingsStore::update_global(cx, |store, cx| {
176 store.update_user_settings(cx, |s| s.helix_mode = Some(true));
177 });
178 })
179 }
180
181 pub fn mode(&mut self) -> Mode {
182 self.update_editor(|editor, _, cx| editor.addon::<VimAddon>().unwrap().entity.read(cx).mode)
183 }
184
185 pub fn forced_motion(&mut self) -> bool {
186 self.update_editor(|_, _, cx| cx.global::<VimGlobals>().forced_motion)
187 }
188
189 pub fn active_operator(&mut self) -> Option<Operator> {
190 self.update_editor(|editor, _, cx| {
191 editor
192 .addon::<VimAddon>()
193 .unwrap()
194 .entity
195 .read(cx)
196 .operator_stack
197 .last()
198 .cloned()
199 })
200 }
201
202 pub fn set_state(&mut self, text: &str, mode: Mode) {
203 self.cx.set_state(text);
204 let vim =
205 self.update_editor(|editor, _window, _cx| editor.addon::<VimAddon>().cloned().unwrap());
206
207 self.update(|window, cx| {
208 vim.entity.update(cx, |vim, cx| {
209 vim.switch_mode(mode, true, window, cx);
210 });
211 });
212 self.cx.cx.cx.run_until_parked();
213 }
214
215 #[track_caller]
216 pub fn assert_state(&mut self, text: &str, mode: Mode) {
217 self.assert_editor_state(text);
218 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
219 }
220
221 pub fn assert_binding(
222 &mut self,
223 keystrokes: &str,
224 initial_state: &str,
225 initial_mode: Mode,
226 state_after: &str,
227 mode_after: Mode,
228 ) {
229 self.set_state(initial_state, initial_mode);
230 self.cx.simulate_keystrokes(keystrokes);
231 self.cx.assert_editor_state(state_after);
232 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
233 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
234 }
235
236 pub fn assert_binding_normal(
237 &mut self,
238 keystrokes: &str,
239 initial_state: &str,
240 state_after: &str,
241 ) {
242 self.set_state(initial_state, Mode::Normal);
243 self.cx.simulate_keystrokes(keystrokes);
244 self.cx.assert_editor_state(state_after);
245 assert_eq!(self.mode(), Mode::Normal, "{}", self.assertion_context());
246 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
247 }
248
249 pub fn shared_clipboard(&mut self) -> VimClipboard {
250 VimClipboard {
251 editor: self
252 .read_from_clipboard()
253 .map(|item| item.text().unwrap())
254 .unwrap_or_default(),
255 }
256 }
257}
258
259pub struct VimClipboard {
260 editor: String,
261}
262
263impl VimClipboard {
264 #[track_caller]
265 pub fn assert_eq(&self, expected: &str) {
266 assert_eq!(self.editor, expected);
267 }
268}
269
270impl Deref for VimTestContext {
271 type Target = EditorLspTestContext;
272
273 fn deref(&self) -> &Self::Target {
274 &self.cx
275 }
276}
277
278impl DerefMut for VimTestContext {
279 fn deref_mut(&mut self) -> &mut Self::Target {
280 &mut self.cx
281 }
282}