vim_test_context.rs

  1use std::ops::{Deref, DerefMut};
  2
  3use editor::test::editor_test_context::EditorTestContext;
  4use gpui::{json::json, AppContext, ContextHandle, ViewHandle};
  5use project::Project;
  6use search::{BufferSearchBar, ProjectSearchBar};
  7use workspace::{pane, AppState, WorkspaceHandle};
  8
  9use crate::{state::Operator, *};
 10
 11use super::VimBindingTestContext;
 12
 13pub struct VimTestContext<'a> {
 14    cx: EditorTestContext<'a>,
 15    workspace: ViewHandle<Workspace>,
 16}
 17
 18impl<'a> VimTestContext<'a> {
 19    pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
 20        cx.update(|cx| {
 21            editor::init(cx);
 22            pane::init(cx);
 23            search::init(cx);
 24            crate::init(cx);
 25
 26            settings::KeymapFileContent::load("keymaps/vim.json", cx).unwrap();
 27        });
 28
 29        let params = cx.update(AppState::test);
 30        let project = Project::test(params.fs.clone(), [], cx).await;
 31
 32        cx.update(|cx| {
 33            cx.update_global(|settings: &mut Settings, _| {
 34                settings.vim_mode = enabled;
 35            });
 36        });
 37
 38        params
 39            .fs
 40            .as_fake()
 41            .insert_tree("/root", json!({ "dir": { "test.txt": "" } }))
 42            .await;
 43
 44        let (window_id, workspace) =
 45            cx.add_window(|cx| Workspace::new(project.clone(), |_, _| unimplemented!(), cx));
 46
 47        // Setup search toolbars
 48        workspace.update(cx, |workspace, cx| {
 49            workspace.active_pane().update(cx, |pane, cx| {
 50                pane.toolbar().update(cx, |toolbar, cx| {
 51                    let buffer_search_bar = cx.add_view(BufferSearchBar::new);
 52                    toolbar.add_item(buffer_search_bar, cx);
 53                    let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
 54                    toolbar.add_item(project_search_bar, cx);
 55                })
 56            });
 57        });
 58
 59        project
 60            .update(cx, |project, cx| {
 61                project.find_or_create_local_worktree("/root", true, cx)
 62            })
 63            .await
 64            .unwrap();
 65        cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
 66            .await;
 67
 68        let file = cx.read(|cx| workspace.file_project_paths(cx)[0].clone());
 69        let item = workspace
 70            .update(cx, |workspace, cx| workspace.open_path(file, true, cx))
 71            .await
 72            .expect("Could not open test file");
 73
 74        let editor = cx.update(|cx| {
 75            item.act_as::<Editor>(cx)
 76                .expect("Opened test file wasn't an editor")
 77        });
 78        editor.update(cx, |_, cx| cx.focus_self());
 79
 80        Self {
 81            cx: EditorTestContext {
 82                cx,
 83                window_id,
 84                editor,
 85            },
 86            workspace,
 87        }
 88    }
 89
 90    pub fn workspace<F, T>(&mut self, read: F) -> T
 91    where
 92        F: FnOnce(&Workspace, &AppContext) -> T,
 93    {
 94        self.workspace.read_with(self.cx.cx, read)
 95    }
 96
 97    pub fn enable_vim(&mut self) {
 98        self.cx.update(|cx| {
 99            cx.update_global(|settings: &mut Settings, _| {
100                settings.vim_mode = true;
101            });
102        })
103    }
104
105    pub fn disable_vim(&mut self) {
106        self.cx.update(|cx| {
107            cx.update_global(|settings: &mut Settings, _| {
108                settings.vim_mode = false;
109            });
110        })
111    }
112
113    pub fn mode(&mut self) -> Mode {
114        self.cx.read(|cx| cx.global::<Vim>().state.mode)
115    }
116
117    pub fn active_operator(&mut self) -> Option<Operator> {
118        self.cx
119            .read(|cx| cx.global::<Vim>().state.operator_stack.last().copied())
120    }
121
122    pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
123        self.cx.update(|cx| {
124            Vim::update(cx, |vim, cx| {
125                vim.switch_mode(mode, false, cx);
126            })
127        });
128        self.cx.set_state(text)
129    }
130
131    pub fn assert_state(&mut self, text: &str, mode: Mode) {
132        self.assert_editor_state(text);
133        assert_eq!(self.mode(), mode, "{}", self.assertion_context());
134    }
135
136    pub fn assert_binding<const COUNT: usize>(
137        &mut self,
138        keystrokes: [&str; COUNT],
139        initial_state: &str,
140        initial_mode: Mode,
141        state_after: &str,
142        mode_after: Mode,
143    ) {
144        self.set_state(initial_state, initial_mode);
145        self.cx.simulate_keystrokes(keystrokes);
146        self.cx.assert_editor_state(state_after);
147        assert_eq!(self.mode(), mode_after, "{}", self.assertion_context());
148        assert_eq!(self.active_operator(), None, "{}", self.assertion_context());
149    }
150
151    pub fn binding<const COUNT: usize>(
152        mut self,
153        keystrokes: [&'static str; COUNT],
154    ) -> VimBindingTestContext<'a, COUNT> {
155        let mode = self.mode();
156        VimBindingTestContext::new(keystrokes, mode, mode, self)
157    }
158}
159
160impl<'a> Deref for VimTestContext<'a> {
161    type Target = EditorTestContext<'a>;
162
163    fn deref(&self) -> &Self::Target {
164        &self.cx
165    }
166}
167
168impl<'a> DerefMut for VimTestContext<'a> {
169    fn deref_mut(&mut self) -> &mut Self::Target {
170        &mut self.cx
171    }
172}