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