1use crate::{insert::NormalBefore, Vim};
2use editor::{Editor, EditorEvent};
3use gpui::{Action, AppContext, Entity, EntityId, View, ViewContext, WindowContext};
4
5pub fn init(cx: &mut AppContext) {
6 cx.observe_new_views(|_, cx: &mut ViewContext<Editor>| {
7 let editor = cx.view().clone();
8 cx.subscribe(&editor, |_, editor, event: &EditorEvent, cx| match event {
9 EditorEvent::Focused => cx.window_context().defer(|cx| focused(editor, cx)),
10 EditorEvent::Blurred => cx.window_context().defer(|cx| blurred(editor, cx)),
11 _ => {}
12 })
13 .detach();
14
15 let id = cx.view().entity_id();
16 cx.on_release(move |_, _, cx| released(id, cx)).detach();
17 })
18 .detach();
19}
20
21fn focused(editor: View<Editor>, cx: &mut WindowContext) {
22 if Vim::read(cx).active_editor.clone().is_some() {
23 Vim::update(cx, |vim, cx| {
24 vim.update_active_editor(cx, |previously_active_editor, cx| {
25 vim.unhook_vim_settings(previously_active_editor, cx)
26 });
27 });
28 }
29
30 Vim::update(cx, |vim, cx| {
31 vim.set_active_editor(editor.clone(), cx);
32 });
33}
34
35fn blurred(editor: View<Editor>, cx: &mut WindowContext) {
36 Vim::update(cx, |vim, cx| {
37 vim.stop_recording_immediately(NormalBefore.boxed_clone());
38 if let Some(previous_editor) = vim.active_editor.clone() {
39 if previous_editor
40 .upgrade()
41 .is_some_and(|previous| previous == editor.clone())
42 {
43 vim.clear_operator(cx);
44 vim.active_editor = None;
45 vim.editor_subscription = None;
46 }
47 }
48
49 editor.update(cx, |editor, cx| vim.unhook_vim_settings(editor, cx))
50 });
51}
52
53fn released(entity_id: EntityId, cx: &mut AppContext) {
54 cx.update_global(|vim: &mut Vim, _| {
55 if vim
56 .active_editor
57 .as_ref()
58 .is_some_and(|previous| previous.entity_id() == entity_id)
59 {
60 vim.active_editor = None;
61 vim.editor_subscription = None;
62 }
63 vim.editor_states.remove(&entity_id)
64 });
65}
66
67#[cfg(test)]
68mod test {
69 use crate::{test::VimTestContext, Vim};
70 use editor::Editor;
71 use gpui::{Context, Entity, VisualTestContext};
72 use language::Buffer;
73
74 // regression test for blur called with a different active editor
75 #[gpui::test]
76 async fn test_blur_focus(cx: &mut gpui::TestAppContext) {
77 let mut cx = VimTestContext::new(cx, true).await;
78
79 let buffer = cx.new_model(|_| Buffer::new(0, 0, "a = 1\nb = 2\n"));
80 let window2 = cx.add_window(|cx| Editor::for_buffer(buffer, None, cx));
81 let editor2 = cx
82 .update(|cx| {
83 window2.update(cx, |_, cx| {
84 cx.activate_window();
85 cx.focus_self();
86 cx.view().clone()
87 })
88 })
89 .unwrap();
90 cx.run_until_parked();
91
92 cx.update(|cx| {
93 let vim = Vim::read(cx);
94 assert_eq!(
95 vim.active_editor.as_ref().unwrap().entity_id(),
96 editor2.entity_id(),
97 )
98 });
99
100 // no panic when blurring an editor in a different window.
101 cx.update_editor(|editor1, cx| {
102 editor1.handle_blur(cx);
103 });
104 }
105
106 // regression test for focus_in/focus_out being called on window activation
107 #[gpui::test]
108 async fn test_focus_across_windows(cx: &mut gpui::TestAppContext) {
109 let mut cx = VimTestContext::new(cx, true).await;
110
111 let mut cx1 = VisualTestContext::from_window(cx.window, &cx);
112 let editor1 = cx.editor.clone();
113
114 let buffer = cx.new_model(|_| Buffer::new(0, 0, "a = 1\nb = 2\n"));
115 let (editor2, cx2) = cx.add_window_view(|cx| Editor::for_buffer(buffer, None, cx));
116
117 editor2.update(cx2, |_, cx| {
118 cx.focus_self();
119 cx.activate_window();
120 });
121 cx.run_until_parked();
122
123 cx1.update(|cx| {
124 assert_eq!(
125 Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
126 editor2.entity_id(),
127 )
128 });
129
130 cx1.update(|cx| {
131 cx.activate_window();
132 });
133 cx.run_until_parked();
134
135 cx.update(|cx| {
136 assert_eq!(
137 Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
138 editor1.entity_id(),
139 )
140 });
141 }
142}