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