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