From bbea3a21844107c9b0dc73c186d654ef7818e906 Mon Sep 17 00:00:00 2001 From: smit <0xtimsb@gmail.com> Date: Tue, 11 Feb 2025 22:07:44 +0530 Subject: [PATCH] editor: Fix crash caused by `editor::SelectPrevious` (#24660) Closes #24345 Release Notes: - Fixed a crash caused by calling `editor::SelectPrevious` twice in a row. Co-authored-by: conrad --- crates/editor/src/editor_tests.rs | 15 +++++++++++++++ crates/multi_buffer/src/multi_buffer.rs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 03388a19d0d8e04b71cb3cad2466bd69dd4b4e6d..0cc4ac46fa990400674902f81aec058eb208a51e 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -5362,6 +5362,21 @@ async fn test_select_previous_with_single_caret(cx: &mut gpui::TestAppContext) { cx.assert_editor_state("«abcˇ»\n«abcˇ» «abcˇ»\ndef«abcˇ»\n«abcˇ»"); } +#[gpui::test] +async fn test_select_previous_empty_buffer(cx: &mut gpui::TestAppContext) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + cx.set_state("aˇ"); + + cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx)) + .unwrap(); + cx.assert_editor_state("«aˇ»"); + cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx)) + .unwrap(); + cx.assert_editor_state("«aˇ»"); +} + #[gpui::test] async fn test_select_previous_with_multiple_carets(cx: &mut gpui::TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index 2adbb6a81c76b80b2122e277952277a3a0e2136e..ca52eadd9a402824f22b62d5c2152081c270a668 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -7121,7 +7121,7 @@ impl<'a> Iterator for ReversedMultiBufferChunks<'a> { self.offset -= 1; Some("\n") } else { - let chunk = self.current_chunks.as_mut().unwrap().next().unwrap(); + let chunk = self.current_chunks.as_mut().unwrap().next()?; self.offset -= chunk.len(); Some(chunk) }