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