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