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