2023-08-17 19:35:32 +00:00
|
|
|
use crate::{Vim, VimEvent};
|
2023-04-20 21:25:11 +00:00
|
|
|
use editor::{EditorBlurred, EditorFocused, EditorReleased};
|
2023-08-08 17:08:37 +00:00
|
|
|
use gpui::AppContext;
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
pub fn init(cx: &mut AppContext) {
|
2023-02-08 23:01:42 +00:00
|
|
|
cx.subscribe_global(focused).detach();
|
|
|
|
cx.subscribe_global(blurred).detach();
|
|
|
|
cx.subscribe_global(released).detach();
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
|
2023-04-27 17:43:09 +00:00
|
|
|
if let Some(previously_active_editor) = Vim::read(cx).active_editor.clone() {
|
2023-08-08 17:08:37 +00:00
|
|
|
previously_active_editor.window().update(cx, |cx| {
|
2023-04-27 17:43:09 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |previously_active_editor, cx| {
|
2023-07-21 01:41:38 +00:00
|
|
|
vim.unhook_vim_settings(previously_active_editor, cx)
|
2023-04-27 17:43:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:08:37 +00:00
|
|
|
editor.window().update(cx, |cx| {
|
2023-04-20 15:29:14 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
2023-04-20 21:25:11 +00:00
|
|
|
vim.set_active_editor(editor.clone(), cx);
|
2023-08-18 21:50:34 +00:00
|
|
|
if vim.enabled {
|
|
|
|
cx.emit_global(VimEvent::ModeChanged {
|
|
|
|
mode: vim.state().mode,
|
|
|
|
});
|
|
|
|
}
|
2023-04-20 15:29:14 +00:00
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
|
2023-08-08 17:08:37 +00:00
|
|
|
editor.window().update(cx, |cx| {
|
2023-04-20 15:29:14 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
2023-08-21 22:10:13 +00:00
|
|
|
vim.workspace_state.recording = false;
|
2023-09-12 00:48:24 +00:00
|
|
|
vim.workspace_state.recorded_actions.clear();
|
2023-04-20 15:29:14 +00:00
|
|
|
if let Some(previous_editor) = vim.active_editor.clone() {
|
|
|
|
if previous_editor == editor.clone() {
|
2023-09-18 18:12:23 +00:00
|
|
|
vim.clear_operator(cx);
|
2023-04-20 15:29:14 +00:00
|
|
|
vim.active_editor = None;
|
2023-09-18 20:00:48 +00:00
|
|
|
vim.editor_subscription = None;
|
2023-04-20 15:29:14 +00:00
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
2023-04-17 09:17:35 +00:00
|
|
|
|
2023-07-21 01:41:38 +00:00
|
|
|
editor.update(cx, |editor, cx| vim.unhook_vim_settings(editor, cx))
|
2023-04-17 09:17:35 +00:00
|
|
|
});
|
2023-04-20 15:29:14 +00:00
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
|
2023-08-08 17:08:37 +00:00
|
|
|
editor.window().update(cx, |cx| {
|
2023-09-18 20:00:48 +00:00
|
|
|
Vim::update(cx, |vim, _| {
|
2023-04-20 15:29:14 +00:00
|
|
|
if let Some(previous_editor) = vim.active_editor.clone() {
|
|
|
|
if previous_editor == editor.clone() {
|
|
|
|
vim.active_editor = None;
|
2023-09-18 20:00:48 +00:00
|
|
|
vim.editor_subscription = None;
|
2023-04-20 15:29:14 +00:00
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
2023-08-17 19:35:32 +00:00
|
|
|
vim.editor_states.remove(&editor.id())
|
2023-04-20 15:29:14 +00:00
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
});
|
|
|
|
}
|
2023-09-18 18:12:23 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::{test::VimTestContext, Vim};
|
|
|
|
use editor::Editor;
|
|
|
|
use gpui::View;
|
|
|
|
use language::Buffer;
|
|
|
|
|
|
|
|
// regression test for blur called with a different active editor
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_blur_focus(cx: &mut gpui::TestAppContext) {
|
|
|
|
let mut cx = VimTestContext::new(cx, true).await;
|
|
|
|
|
|
|
|
let buffer = cx.add_model(|_| Buffer::new(0, 0, "a = 1\nb = 2\n"));
|
|
|
|
let window2 = cx.add_window(|cx| Editor::for_buffer(buffer, None, cx));
|
|
|
|
let editor2 = cx.read(|cx| window2.root(cx)).unwrap();
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
let vim = Vim::read(cx);
|
|
|
|
assert_eq!(vim.active_editor.unwrap().id(), editor2.id())
|
|
|
|
});
|
|
|
|
|
|
|
|
// no panic when blurring an editor in a different window.
|
|
|
|
cx.update_editor(|editor1, cx| {
|
|
|
|
editor1.focus_out(cx.handle().into_any(), cx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|