editor_events.rs

  1use crate::{insert::NormalBefore, Vim, VimModeSetting};
  2use editor::{Editor, EditorEvent};
  3use gpui::{Action, AppContext, Entity, EntityId, View, ViewContext, WindowContext};
  4use settings::{Settings, SettingsStore};
  5
  6pub fn init(cx: &mut AppContext) {
  7    cx.observe_new_views(|_, cx: &mut ViewContext<Editor>| {
  8        let editor = cx.view().clone();
  9        cx.subscribe(&editor, |_, editor, event: &EditorEvent, cx| match event {
 10            EditorEvent::Focused => cx.window_context().defer(|cx| focused(editor, cx)),
 11            EditorEvent::Blurred => cx.window_context().defer(|cx| blurred(editor, cx)),
 12            _ => {}
 13        })
 14        .detach();
 15
 16        let mut enabled = VimModeSetting::get_global(cx).0;
 17        cx.observe_global::<SettingsStore>(move |editor, cx| {
 18            if VimModeSetting::get_global(cx).0 != enabled {
 19                enabled = VimModeSetting::get_global(cx).0;
 20                if !enabled {
 21                    Vim::unhook_vim_settings(editor, cx);
 22                }
 23            }
 24        })
 25        .detach();
 26
 27        let id = cx.view().entity_id();
 28        cx.on_release(move |_, _, cx| released(id, cx)).detach();
 29    })
 30    .detach();
 31}
 32
 33fn focused(editor: View<Editor>, cx: &mut WindowContext) {
 34    Vim::update(cx, |vim, cx| {
 35        if !vim.enabled {
 36            return;
 37        }
 38        vim.activate_editor(editor.clone(), cx);
 39    });
 40}
 41
 42fn blurred(editor: View<Editor>, cx: &mut WindowContext) {
 43    Vim::update(cx, |vim, cx| {
 44        if let Some(previous_editor) = vim.active_editor.clone() {
 45            vim.stop_recording_immediately(NormalBefore.boxed_clone());
 46            if previous_editor
 47                .upgrade()
 48                .is_some_and(|previous| previous == editor.clone())
 49            {
 50                vim.sync_vim_settings(cx);
 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, BufferId};
 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(|_| Buffer::new(0, BufferId::new(1).unwrap(), "a = 1\nb = 2\n"));
 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(|_| Buffer::new(0, BufferId::new(1).unwrap(), "a = 1\nb = 2\n"));
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}