editor_events.rs

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