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