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