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 });
25
26 cx.update(|cx| {
27 cx.update_global(|store: &mut SettingsStore, cx| {
28 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
29 });
30 settings::KeymapFileContent::load_asset("keymaps/vim.json", cx).unwrap();
31 });
32
33 // Setup search toolbars and keypress hook
34 cx.update_workspace(|workspace, cx| {
35 observe_keystrokes(cx);
36 workspace.active_pane().update(cx, |pane, cx| {
37 pane.toolbar().update(cx, |toolbar, cx| {
38 let buffer_search_bar = cx.add_view(BufferSearchBar::new);
39 toolbar.add_item(buffer_search_bar, cx);
40 let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
41 toolbar.add_item(project_search_bar, cx);
42 })
43 });
44 });
45
46 Self { cx }
47 }
48
49 pub fn workspace<F, T>(&mut self, read: F) -> T
50 where
51 F: FnOnce(&Workspace, &ViewContext<Workspace>) -> T,
52 {
53 self.cx.workspace.read_with(self.cx.cx.cx, read)
54 }
55
56 pub fn enable_vim(&mut self) {
57 self.cx.update(|cx| {
58 cx.update_global(|store: &mut SettingsStore, cx| {
59 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
60 });
61 })
62 }
63
64 pub fn disable_vim(&mut self) {
65 self.cx.update(|cx| {
66 cx.update_global(|store: &mut SettingsStore, cx| {
67 store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
68 });
69 })
70 }
71
72 pub fn mode(&mut self) -> Mode {
73 self.cx.read(|cx| cx.global::<Vim>().state.mode)
74 }
75
76 pub fn active_operator(&mut self) -> Option<Operator> {
77 self.cx
78 .read(|cx| cx.global::<Vim>().state.operator_stack.last().copied())
79 }
80
81 pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
82 let window_id = self.window_id;
83 self.update_window(window_id, |cx| {
84 Vim::update(cx, |vim, cx| {
85 vim.switch_mode(mode, false, cx);
86 })
87 });
88 self.cx.set_state(text)
89 }
90
91 pub fn assert_state(&mut self, text: &str, mode: Mode) {
92 self.assert_editor_state(text);
93 assert_eq!(self.mode(), mode, "{}", self.assertion_context());
94 }
95
96 pub fn assert_binding<const COUNT: usize>(
97 &mut self,
98 keystrokes: [&str; COUNT],
99 initial_state: &str,
100 initial_mode: Mode,
101 state_after: &str,
102 mode_after: Mode,
103 ) {
104 self.set_state(initial_state, initial_mode);
105 self.cx.simulate_keystrokes(keystrokes);
106 self.cx.assert_editor_state(state_after);
107 assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
108 assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
109 }
110
111 pub fn binding<const COUNT: usize>(
112 mut self,
113 keystrokes: [&'static str; COUNT],
114 ) -> VimBindingTestContext<'a, COUNT> {
115 let mode = self.mode();
116 VimBindingTestContext::new(keystrokes, mode, mode, self)
117 }
118}
119
120impl<'a> Deref for VimTestContext<'a> {
121 type Target = EditorTestContext<'a>;
122
123 fn deref(&self) -> &Self::Target {
124 &self.cx
125 }
126}
127
128impl<'a> DerefMut for VimTestContext<'a> {
129 fn deref_mut(&mut self) -> &mut Self::Target {
130 &mut self.cx
131 }
132}