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 active_operator(&mut self) -> Option<Operator> {
146 self.update_editor(|editor, _, cx| {
147 editor
148 .addon::<VimAddon>()
149 .unwrap()
150 .entity
151 .read(cx)
152 .operator_stack
153 .last()
154 .cloned()
155 })
156 }
157
158 pub fn set_state(&mut self, text: &str, mode: Mode) {
159 self.cx.set_state(text);
160 let vim =
161 self.update_editor(|editor, _window, _cx| editor.addon::<VimAddon>().cloned().unwrap());
162
163 self.update(|window, cx| {
164 vim.entity.update(cx, |vim, cx| {
165 vim.switch_mode(mode, true, window, cx);
166 });
167 });
168 self.cx.cx.cx.run_until_parked();
169 }
170
171 #[track_caller]
172 pub fn assert_state(&mut self, text: &str, mode: Mode) {
173 self.assert_editor_state(text);
174 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
175 }
176
177 pub fn assert_binding(
178 &mut self,
179 keystrokes: &str,
180 initial_state: &str,
181 initial_mode: Mode,
182 state_after: &str,
183 mode_after: Mode,
184 ) {
185 self.set_state(initial_state, initial_mode);
186 self.cx.simulate_keystrokes(keystrokes);
187 self.cx.assert_editor_state(state_after);
188 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
189 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
190 }
191
192 pub fn assert_binding_normal(
193 &mut self,
194 keystrokes: &str,
195 initial_state: &str,
196 state_after: &str,
197 ) {
198 self.set_state(initial_state, Mode::Normal);
199 self.cx.simulate_keystrokes(keystrokes);
200 self.cx.assert_editor_state(state_after);
201 assert_eq!(self.mode(), Mode::Normal, "{}", self.assertion_context());
202 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
203 }
204}
205
206impl Deref for VimTestContext {
207 type Target = EditorLspTestContext;
208
209 fn deref(&self) -> &Self::Target {
210 &self.cx
211 }
212}
213
214impl DerefMut for VimTestContext {
215 fn deref_mut(&mut self) -> &mut Self::Target {
216 &mut self.cx
217 }
218}