1use std::ops::{Deref, DerefMut};
2
3use editor::test::{
4 editor_lsp_test_context::EditorLspTestContext, editor_test_context::EditorTestContext,
5};
6use gpui::ContextHandle;
7use search::{BufferSearchBar, ProjectSearchBar};
8
9use crate::{state::Operator, *};
10
11use super::VimBindingTestContext;
12
13pub struct VimTestContext<'a> {
14 cx: EditorLspTestContext<'a>,
15}
16
17impl<'a> VimTestContext<'a> {
18 pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
19 let mut cx = EditorLspTestContext::new_rust(Default::default(), cx).await;
20
21 cx.update(|cx| {
22 search::init(cx);
23 crate::init(cx);
24 command_palette::init(cx);
25 });
26
27 cx.update(|cx| {
28 cx.update_global(|store: &mut SettingsStore, cx| {
29 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
30 });
31 settings::KeymapFile::load_asset("keymaps/default.json", cx).unwrap();
32 settings::KeymapFile::load_asset("keymaps/vim.json", cx).unwrap();
33 });
34
35 // Setup search toolbars and keypress hook
36 cx.update_workspace(|workspace, cx| {
37 observe_keystrokes(cx);
38 workspace.active_pane().update(cx, |pane, cx| {
39 pane.toolbar().update(cx, |toolbar, cx| {
40 let buffer_search_bar = cx.add_view(BufferSearchBar::new);
41 toolbar.add_item(buffer_search_bar, cx);
42 let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
43 toolbar.add_item(project_search_bar, cx);
44 })
45 });
46 workspace.status_bar().update(cx, |status_bar, cx| {
47 let vim_mode_indicator = cx.add_view(ModeIndicator::new);
48 status_bar.add_right_item(vim_mode_indicator, cx);
49 });
50 });
51
52 Self { cx }
53 }
54
55 pub fn workspace<F, T>(&mut self, read: F) -> T
56 where
57 F: FnOnce(&Workspace, &ViewContext<Workspace>) -> T,
58 {
59 self.cx.workspace.read_with(self.cx.cx.cx, read)
60 }
61
62 pub fn enable_vim(&mut self) {
63 self.cx.update(|cx| {
64 cx.update_global(|store: &mut SettingsStore, cx| {
65 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
66 });
67 })
68 }
69
70 pub fn disable_vim(&mut self) {
71 self.cx.update(|cx| {
72 cx.update_global(|store: &mut SettingsStore, cx| {
73 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
74 });
75 })
76 }
77
78 pub fn mode(&mut self) -> Mode {
79 self.cx.read(|cx| cx.global::<Vim>().state.mode)
80 }
81
82 pub fn active_operator(&mut self) -> Option<Operator> {
83 self.cx
84 .read(|cx| cx.global::<Vim>().state.operator_stack.last().copied())
85 }
86
87 pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
88 let window_id = self.window.id();
89 self.update_window(window_id, |cx| {
90 Vim::update(cx, |vim, cx| {
91 vim.switch_mode(mode, false, cx);
92 })
93 });
94 self.cx.set_state(text)
95 }
96
97 #[track_caller]
98 pub fn assert_state(&mut self, text: &str, mode: Mode) {
99 self.assert_editor_state(text);
100 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
101 }
102
103 pub fn assert_binding<const COUNT: usize>(
104 &mut self,
105 keystrokes: [&str; COUNT],
106 initial_state: &str,
107 initial_mode: Mode,
108 state_after: &str,
109 mode_after: Mode,
110 ) {
111 self.set_state(initial_state, initial_mode);
112 self.cx.simulate_keystrokes(keystrokes);
113 self.cx.assert_editor_state(state_after);
114 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
115 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
116 }
117
118 pub fn binding<const COUNT: usize>(
119 mut self,
120 keystrokes: [&'static str; COUNT],
121 ) -> VimBindingTestContext<'a, COUNT> {
122 let mode = self.mode();
123 VimBindingTestContext::new(keystrokes, mode, mode, self)
124 }
125}
126
127impl<'a> Deref for VimTestContext<'a> {
128 type Target = EditorTestContext<'a>;
129
130 fn deref(&self) -> &Self::Target {
131 &self.cx
132 }
133}
134
135impl<'a> DerefMut for VimTestContext<'a> {
136 fn deref_mut(&mut self) -> &mut Self::Target {
137 &mut self.cx
138 }
139}