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