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 cx.update(|cx| {
19 let settings = SettingsStore::test(cx);
20 cx.set_global(settings);
21 release_channel::init(SemanticVersion::default(), cx);
22 command_palette::init(cx);
23 project_panel::init(cx);
24 git_ui::init(cx);
25 crate::init(cx);
26 search::init(cx);
27 workspace::init_settings(cx);
28 language::init(cx);
29 editor::init_settings(cx);
30 project::Project::init_settings(cx);
31 theme::init(theme::LoadThemes::JustBase, cx);
32 });
33 }
34
35 pub async fn new(cx: &mut gpui::TestAppContext, enabled: bool) -> VimTestContext {
36 Self::init(cx);
37 let lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
38 Self::new_with_lsp(lsp, enabled)
39 }
40
41 pub async fn new_html(cx: &mut gpui::TestAppContext) -> VimTestContext {
42 Self::init(cx);
43 Self::new_with_lsp(EditorLspTestContext::new_html(cx).await, true)
44 }
45
46 pub async fn new_typescript(cx: &mut gpui::TestAppContext) -> VimTestContext {
47 Self::init(cx);
48 Self::new_with_lsp(
49 EditorLspTestContext::new_typescript(
50 lsp::ServerCapabilities {
51 rename_provider: Some(lsp::OneOf::Right(lsp::RenameOptions {
52 prepare_provider: Some(true),
53 work_done_progress_options: Default::default(),
54 })),
55 ..Default::default()
56 },
57 cx,
58 )
59 .await,
60 true,
61 )
62 }
63
64 pub fn init_keybindings(enabled: bool, cx: &mut App) {
65 SettingsStore::update_global(cx, |store, cx| {
66 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
67 });
68 let default_key_bindings = settings::KeymapFile::load_asset_allow_partial_failure(
69 "keymaps/default-macos.json",
70 cx,
71 )
72 .unwrap();
73 cx.bind_keys(default_key_bindings);
74 if enabled {
75 let vim_key_bindings =
76 settings::KeymapFile::load_asset("keymaps/vim.json", cx).unwrap();
77 cx.bind_keys(vim_key_bindings);
78 }
79 }
80
81 pub fn new_with_lsp(mut cx: EditorLspTestContext, enabled: bool) -> VimTestContext {
82 cx.update(|_, cx| {
83 Self::init_keybindings(enabled, cx);
84 });
85
86 // Setup search toolbars and keypress hook
87 cx.update_workspace(|workspace, window, cx| {
88 workspace.active_pane().update(cx, |pane, cx| {
89 pane.toolbar().update(cx, |toolbar, cx| {
90 let buffer_search_bar = cx.new(|cx| BufferSearchBar::new(None, window, cx));
91 toolbar.add_item(buffer_search_bar, window, cx);
92
93 let project_search_bar = cx.new(|_| ProjectSearchBar::new());
94 toolbar.add_item(project_search_bar, window, cx);
95 })
96 });
97 workspace.status_bar().update(cx, |status_bar, cx| {
98 let vim_mode_indicator = cx.new(|cx| ModeIndicator::new(window, cx));
99 status_bar.add_right_item(vim_mode_indicator, window, cx);
100 });
101 });
102
103 Self { cx }
104 }
105
106 pub fn update_entity<F, T, R>(&mut self, entity: Entity<T>, update: F) -> R
107 where
108 T: 'static,
109 F: FnOnce(&mut T, &mut Window, &mut Context<T>) -> R + 'static,
110 {
111 let window = self.window;
112 self.update_window(window, move |_, window, cx| {
113 entity.update(cx, |t, cx| update(t, window, cx))
114 })
115 .unwrap()
116 }
117
118 pub fn workspace<F, T>(&mut self, update: F) -> T
119 where
120 F: FnOnce(&mut Workspace, &mut Window, &mut Context<Workspace>) -> T,
121 {
122 self.cx.update_workspace(update)
123 }
124
125 pub fn enable_vim(&mut self) {
126 self.cx.update(|_, cx| {
127 SettingsStore::update_global(cx, |store, cx| {
128 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
129 });
130 })
131 }
132
133 pub fn disable_vim(&mut self) {
134 self.cx.update(|_, cx| {
135 SettingsStore::update_global(cx, |store, cx| {
136 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
137 });
138 })
139 }
140
141 pub fn mode(&mut self) -> Mode {
142 self.update_editor(|editor, _, cx| editor.addon::<VimAddon>().unwrap().entity.read(cx).mode)
143 }
144
145 pub fn forced_motion(&mut self) -> bool {
146 self.update_editor(|_, _, cx| cx.global::<VimGlobals>().forced_motion)
147 }
148
149 pub fn active_operator(&mut self) -> Option<Operator> {
150 self.update_editor(|editor, _, cx| {
151 editor
152 .addon::<VimAddon>()
153 .unwrap()
154 .entity
155 .read(cx)
156 .operator_stack
157 .last()
158 .cloned()
159 })
160 }
161
162 pub fn set_state(&mut self, text: &str, mode: Mode) {
163 self.cx.set_state(text);
164 let vim =
165 self.update_editor(|editor, _window, _cx| editor.addon::<VimAddon>().cloned().unwrap());
166
167 self.update(|window, cx| {
168 vim.entity.update(cx, |vim, cx| {
169 vim.switch_mode(mode, true, window, cx);
170 });
171 });
172 self.cx.cx.cx.run_until_parked();
173 }
174
175 #[track_caller]
176 pub fn assert_state(&mut self, text: &str, mode: Mode) {
177 self.assert_editor_state(text);
178 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
179 }
180
181 pub fn assert_binding(
182 &mut self,
183 keystrokes: &str,
184 initial_state: &str,
185 initial_mode: Mode,
186 state_after: &str,
187 mode_after: Mode,
188 ) {
189 self.set_state(initial_state, initial_mode);
190 self.cx.simulate_keystrokes(keystrokes);
191 self.cx.assert_editor_state(state_after);
192 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
193 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
194 }
195
196 pub fn assert_binding_normal(
197 &mut self,
198 keystrokes: &str,
199 initial_state: &str,
200 state_after: &str,
201 ) {
202 self.set_state(initial_state, Mode::Normal);
203 self.cx.simulate_keystrokes(keystrokes);
204 self.cx.assert_editor_state(state_after);
205 assert_eq!(self.mode(), Mode::Normal, "{}", self.assertion_context());
206 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
207 }
208}
209
210impl Deref for VimTestContext {
211 type Target = EditorLspTestContext;
212
213 fn deref(&self) -> &Self::Target {
214 &self.cx
215 }
216}
217
218impl DerefMut for VimTestContext {
219 fn deref_mut(&mut self) -> &mut Self::Target {
220 &mut self.cx
221 }
222}