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