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