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