editor_events.rs

  1use crate::{insert::NormalBefore, Vim, VimModeSetting};
  2use editor::{Editor, EditorEvent};
  3use gpui::{
  4    Action, AppContext, BorrowAppContext, Entity, EntityId, View, ViewContext, WindowContext,
  5};
  6use settings::{Settings, SettingsStore};
  7
  8pub fn init(cx: &mut AppContext) {
  9    cx.observe_new_views(|_, cx: &mut ViewContext<Editor>| {
 10        let editor = cx.view().clone();
 11        cx.subscribe(&editor, |_, editor, event: &EditorEvent, cx| match event {
 12            EditorEvent::Focused => cx.window_context().defer(|cx| focused(editor, cx)),
 13            EditorEvent::Blurred => cx.window_context().defer(|cx| blurred(editor, cx)),
 14            _ => {}
 15        })
 16        .detach();
 17
 18        let mut enabled = VimModeSetting::get_global(cx).0;
 19        cx.observe_global::<SettingsStore>(move |editor, cx| {
 20            if VimModeSetting::get_global(cx).0 != enabled {
 21                enabled = VimModeSetting::get_global(cx).0;
 22                if !enabled {
 23                    Vim::unhook_vim_settings(editor, cx);
 24                }
 25            }
 26        })
 27        .detach();
 28
 29        let id = cx.view().entity_id();
 30        cx.on_release(move |_, _, cx| released(id, cx)).detach();
 31    })
 32    .detach();
 33}
 34fn focused(editor: View<Editor>, cx: &mut WindowContext) {
 35    Vim::update(cx, |vim, cx| {
 36        if !vim.enabled {
 37            return;
 38        }
 39        vim.activate_editor(editor.clone(), cx);
 40    });
 41}
 42
 43fn blurred(editor: View<Editor>, cx: &mut WindowContext) {
 44    Vim::update(cx, |vim, cx| {
 45        if !vim.enabled {
 46            return;
 47        }
 48        if let Some(previous_editor) = vim.active_editor.clone() {
 49            vim.stop_recording_immediately(NormalBefore.boxed_clone());
 50            if previous_editor
 51                .upgrade()
 52                .is_some_and(|previous| previous == editor.clone())
 53            {
 54                vim.clear_operator(cx);
 55            }
 56        }
 57        editor.update(cx, |editor, cx| {
 58            editor.set_cursor_shape(language::CursorShape::Hollow, cx);
 59        });
 60    });
 61}
 62
 63fn released(entity_id: EntityId, cx: &mut AppContext) {
 64    cx.update_global(|vim: &mut Vim, _| {
 65        if vim
 66            .active_editor
 67            .as_ref()
 68            .is_some_and(|previous| previous.entity_id() == entity_id)
 69        {
 70            vim.active_editor = None;
 71            vim.editor_subscription = None;
 72        }
 73        vim.editor_states.remove(&entity_id)
 74    });
 75}
 76
 77#[cfg(test)]
 78mod test {
 79    use crate::{test::VimTestContext, Vim};
 80    use editor::Editor;
 81    use gpui::{Context, Entity, VisualTestContext};
 82    use language::Buffer;
 83
 84    // regression test for blur called with a different active editor
 85    #[gpui::test]
 86    async fn test_blur_focus(cx: &mut gpui::TestAppContext) {
 87        let mut cx = VimTestContext::new(cx, true).await;
 88
 89        let buffer = cx.new_model(|cx| Buffer::local("a = 1\nb = 2\n", cx));
 90        let window2 = cx.add_window(|cx| Editor::for_buffer(buffer, None, cx));
 91        let editor2 = cx
 92            .update(|cx| {
 93                window2.update(cx, |_, cx| {
 94                    cx.activate_window();
 95                    cx.focus_self();
 96                    cx.view().clone()
 97                })
 98            })
 99            .unwrap();
100        cx.run_until_parked();
101
102        cx.update(|cx| {
103            let vim = Vim::read(cx);
104            assert_eq!(
105                vim.active_editor.as_ref().unwrap().entity_id(),
106                editor2.entity_id(),
107            )
108        });
109
110        // no panic when blurring an editor in a different window.
111        cx.update_editor(|editor1, cx| {
112            editor1.handle_blur(cx);
113        });
114    }
115
116    // regression test for focus_in/focus_out being called on window activation
117    #[gpui::test]
118    async fn test_focus_across_windows(cx: &mut gpui::TestAppContext) {
119        let mut cx = VimTestContext::new(cx, true).await;
120
121        let mut cx1 = VisualTestContext::from_window(cx.window, &cx);
122        let editor1 = cx.editor.clone();
123
124        let buffer = cx.new_model(|cx| Buffer::local("a = 1\nb = 2\n", cx));
125        let (editor2, cx2) = cx.add_window_view(|cx| Editor::for_buffer(buffer, None, cx));
126
127        editor2.update(cx2, |_, cx| {
128            cx.focus_self();
129            cx.activate_window();
130        });
131        cx.run_until_parked();
132
133        cx1.update(|cx| {
134            assert_eq!(
135                Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
136                editor2.entity_id(),
137            )
138        });
139
140        cx1.update(|cx| {
141            cx.activate_window();
142        });
143        cx.run_until_parked();
144
145        cx.update(|cx| {
146            assert_eq!(
147                Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
148                editor1.entity_id(),
149            )
150        });
151    }
152}