From 417f28effe8af393443e48eaa6b782af0ae36186 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 18 Sep 2023 12:12:23 -0600 Subject: [PATCH] Fix vim-related panic --- crates/vim/src/editor_events.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/vim/src/editor_events.rs b/crates/vim/src/editor_events.rs index ae6a2808cf..4b6046ae62 100644 --- a/crates/vim/src/editor_events.rs +++ b/crates/vim/src/editor_events.rs @@ -34,11 +34,11 @@ fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) { fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) { editor.window().update(cx, |cx| { Vim::update(cx, |vim, cx| { - vim.clear_operator(cx); vim.workspace_state.recording = false; vim.workspace_state.recorded_actions.clear(); if let Some(previous_editor) = vim.active_editor.clone() { if previous_editor == editor.clone() { + vim.clear_operator(cx); vim.active_editor = None; } } @@ -60,3 +60,31 @@ fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) { }); }); } + +#[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); + }); + } +}