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