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 let Some(previous_editor) = vim.active_editor.clone() {
 46            vim.stop_recording_immediately(NormalBefore.boxed_clone());
 47            if previous_editor
 48                .upgrade()
 49                .is_some_and(|previous| previous == editor.clone())
 50            {
 51                vim.clear_operator(cx);
 52            }
 53        }
 54    });
 55}
 56
 57fn released(entity_id: EntityId, cx: &mut AppContext) {
 58    cx.update_global(|vim: &mut Vim, _| {
 59        if vim
 60            .active_editor
 61            .as_ref()
 62            .is_some_and(|previous| previous.entity_id() == entity_id)
 63        {
 64            vim.active_editor = None;
 65            vim.editor_subscription = None;
 66        }
 67        vim.editor_states.remove(&entity_id)
 68    });
 69}
 70
 71#[cfg(test)]
 72mod test {
 73    use crate::{test::VimTestContext, Vim};
 74    use editor::Editor;
 75    use gpui::{Context, Entity, VisualTestContext};
 76    use language::Buffer;
 77
 78    // regression test for blur called with a different active editor
 79    #[gpui::test]
 80    async fn test_blur_focus(cx: &mut gpui::TestAppContext) {
 81        let mut cx = VimTestContext::new(cx, true).await;
 82
 83        let buffer = cx.new_model(|cx| Buffer::local("a = 1\nb = 2\n", cx));
 84        let window2 = cx.add_window(|cx| Editor::for_buffer(buffer, None, cx));
 85        let editor2 = cx
 86            .update(|cx| {
 87                window2.update(cx, |_, cx| {
 88                    cx.activate_window();
 89                    cx.focus_self();
 90                    cx.view().clone()
 91                })
 92            })
 93            .unwrap();
 94        cx.run_until_parked();
 95
 96        cx.update(|cx| {
 97            let vim = Vim::read(cx);
 98            assert_eq!(
 99                vim.active_editor.as_ref().unwrap().entity_id(),
100                editor2.entity_id(),
101            )
102        });
103
104        // no panic when blurring an editor in a different window.
105        cx.update_editor(|editor1, cx| {
106            editor1.handle_blur(cx);
107        });
108    }
109
110    // regression test for focus_in/focus_out being called on window activation
111    #[gpui::test]
112    async fn test_focus_across_windows(cx: &mut gpui::TestAppContext) {
113        let mut cx = VimTestContext::new(cx, true).await;
114
115        let mut cx1 = VisualTestContext::from_window(cx.window, &cx);
116        let editor1 = cx.editor.clone();
117
118        let buffer = cx.new_model(|cx| Buffer::local("a = 1\nb = 2\n", cx));
119        let (editor2, cx2) = cx.add_window_view(|cx| Editor::for_buffer(buffer, None, cx));
120
121        editor2.update(cx2, |_, cx| {
122            cx.focus_self();
123            cx.activate_window();
124        });
125        cx.run_until_parked();
126
127        cx1.update(|cx| {
128            assert_eq!(
129                Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
130                editor2.entity_id(),
131            )
132        });
133
134        cx1.update(|cx| {
135            cx.activate_window();
136        });
137        cx.run_until_parked();
138
139        cx.update(|cx| {
140            assert_eq!(
141                Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
142                editor1.entity_id(),
143            )
144        });
145    }
146}