editor_events.rs

 1use crate::{Vim, VimEvent};
 2use editor::{EditorBlurred, EditorFocused, EditorReleased};
 3use gpui::AppContext;
 4
 5pub fn init(cx: &mut AppContext) {
 6    cx.subscribe_global(focused).detach();
 7    cx.subscribe_global(blurred).detach();
 8    cx.subscribe_global(released).detach();
 9}
10
11fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
12    if let Some(previously_active_editor) = Vim::read(cx).active_editor.clone() {
13        previously_active_editor.window().update(cx, |cx| {
14            Vim::update(cx, |vim, cx| {
15                vim.update_active_editor(cx, |previously_active_editor, cx| {
16                    vim.unhook_vim_settings(previously_active_editor, cx)
17                });
18            });
19        });
20    }
21
22    editor.window().update(cx, |cx| {
23        Vim::update(cx, |vim, cx| {
24            vim.set_active_editor(editor.clone(), cx);
25            if vim.enabled {
26                cx.emit_global(VimEvent::ModeChanged {
27                    mode: vim.state().mode,
28                });
29            }
30        });
31    });
32}
33
34fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
35    editor.window().update(cx, |cx| {
36        Vim::update(cx, |vim, cx| {
37            vim.workspace_state.recording = false;
38            vim.workspace_state.recorded_actions.clear();
39            if let Some(previous_editor) = vim.active_editor.clone() {
40                if previous_editor == editor.clone() {
41                    vim.clear_operator(cx);
42                    vim.active_editor = None;
43                }
44            }
45
46            editor.update(cx, |editor, cx| vim.unhook_vim_settings(editor, cx))
47        });
48    });
49}
50
51fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
52    editor.window().update(cx, |cx| {
53        cx.update_default_global(|vim: &mut Vim, _| {
54            if let Some(previous_editor) = vim.active_editor.clone() {
55                if previous_editor == editor.clone() {
56                    vim.active_editor = None;
57                }
58            }
59            vim.editor_states.remove(&editor.id())
60        });
61    });
62}
63
64#[cfg(test)]
65mod test {
66    use crate::{test::VimTestContext, Vim};
67    use editor::Editor;
68    use gpui::View;
69    use language::Buffer;
70
71    // regression test for blur called with a different active editor
72    #[gpui::test]
73    async fn test_blur_focus(cx: &mut gpui::TestAppContext) {
74        let mut cx = VimTestContext::new(cx, true).await;
75
76        let buffer = cx.add_model(|_| Buffer::new(0, 0, "a = 1\nb = 2\n"));
77        let window2 = cx.add_window(|cx| Editor::for_buffer(buffer, None, cx));
78        let editor2 = cx.read(|cx| window2.root(cx)).unwrap();
79
80        cx.update(|cx| {
81            let vim = Vim::read(cx);
82            assert_eq!(vim.active_editor.unwrap().id(), editor2.id())
83        });
84
85        // no panic when blurring an editor in a different window.
86        cx.update_editor(|editor1, cx| {
87            editor1.focus_out(cx.handle().into_any(), cx);
88        });
89    }
90}