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