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 });
47
48 Self { cx }
49 }
50
51 pub fn workspace<F, T>(&mut self, read: F) -> T
52 where
53 F: FnOnce(&Workspace, &ViewContext<Workspace>) -> T,
54 {
55 self.cx.workspace.read_with(self.cx.cx.cx, read)
56 }
57
58 pub fn enable_vim(&mut self) {
59 self.cx.update(|cx| {
60 cx.update_global(|store: &mut SettingsStore, cx| {
61 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
62 });
63 })
64 }
65
66 pub fn disable_vim(&mut self) {
67 self.cx.update(|cx| {
68 cx.update_global(|store: &mut SettingsStore, cx| {
69 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
70 });
71 })
72 }
73
74 pub fn mode(&mut self) -> Mode {
75 self.cx.read(|cx| cx.global::<Vim>().state.mode)
76 }
77
78 pub fn active_operator(&mut self) -> Option<Operator> {
79 self.cx
80 .read(|cx| cx.global::<Vim>().state.operator_stack.last().copied())
81 }
82
83 pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
84 let window_id = self.window_id;
85 self.update_window(window_id, |cx| {
86 Vim::update(cx, |vim, cx| {
87 vim.switch_mode(mode, false, cx);
88 })
89 });
90 self.cx.set_state(text)
91 }
92
93 #[track_caller]
94 pub fn assert_state(&mut self, text: &str, mode: Mode) {
95 self.assert_editor_state(text);
96 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
97 }
98
99 pub fn assert_binding<const COUNT: usize>(
100 &mut self,
101 keystrokes: [&str; COUNT],
102 initial_state: &str,
103 initial_mode: Mode,
104 state_after: &str,
105 mode_after: Mode,
106 ) {
107 self.set_state(initial_state, initial_mode);
108 self.cx.simulate_keystrokes(keystrokes);
109 self.cx.assert_editor_state(state_after);
110 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
111 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
112 }
113
114 pub fn binding<const COUNT: usize>(
115 mut self,
116 keystrokes: [&'static str; COUNT],
117 ) -> VimBindingTestContext<'a, COUNT> {
118 let mode = self.mode();
119 VimBindingTestContext::new(keystrokes, mode, mode, self)
120 }
121}
122
123impl<'a> Deref for VimTestContext<'a> {
124 type Target = EditorTestContext<'a>;
125
126 fn deref(&self) -> &Self::Target {
127 &self.cx
128 }
129}
130
131impl<'a> DerefMut for VimTestContext<'a> {
132 fn deref_mut(&mut self) -> &mut Self::Target {
133 &mut self.cx
134 }
135}