Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix crash when copying and pasting using multiple cursors (#61545)

Tim Vermeulene12992cae3c0
diff --git a/crates/editor/src/clipboard.rs b/crates/editor/src/clipboard.rs
index b6875bee480..35977107376 100644
--- a/crates/editor/src/clipboard.rs
+++ b/crates/editor/src/clipboard.rs
@@ -586,6 +586,7 @@ impl Editor {
let max_point = buffer.max_point();
let mut is_first = true;
+ let mut prev_selection_was_entire_line = false;
for selection in &selections {
let mut start = selection.start;
let mut end = selection.end;
@@ -641,7 +642,6 @@ impl Editor {
let is_multiline_trim = trimmed_selections.len() > 1;
let mut selection_len: usize = 0;
- let prev_selection_was_entire_line = is_entire_line && !is_multiline_trim;
for trimmed_range in trimmed_selections {
if is_first {
@@ -661,6 +661,7 @@ impl Editor {
selection_len += 1;
}
}
+ prev_selection_was_entire_line = is_entire_line && !is_multiline_trim;
clipboard_selections.push(ClipboardSelection::for_buffer(
selection_len,
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index dbf3e48cf38..5e43b80e5f2 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -9690,6 +9690,32 @@ async fn test_clipboard(cx: &mut TestAppContext) {
tˇhe lazy dog"});
}
+#[gpui::test]
+async fn test_copy_and_paste_non_empty_selection_followed_by_empty_selection(
+ cx: &mut TestAppContext,
+) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+ cx.set_state(indoc! {"
+ «fooˇ»
+ barˇ
+ "});
+
+ cx.update_editor(|editor, window, cx| editor.copy(&Copy, window, cx));
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some("foo\nbar\n".to_string())
+ );
+
+ cx.update_editor(|editor, window, cx| editor.paste(&Paste, window, cx));
+ cx.assert_editor_state(indoc! {"
+ fooˇ
+ bar
+ barˇ
+ "});
+}
+
#[gpui::test]
async fn test_copy_trim(cx: &mut TestAppContext) {
init_test(cx, |_| {});