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