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