From 52b0e43064e47b4d3d7f8ddf9d5ab8966b869925 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Mon, 16 Nov 2026 13:59:16 +0000 Subject: [PATCH] editor: Don't write empty selections to the primary selection (#66986) Closes #64446 ## Summary On Linux, each local selection change copies the selected text to the primary selection and joins multiple selections with newlines. It skipped the write only when the joined text was empty. With two or more cursors and nothing selected, the text is one newline per extra cursor, so every cursor move or keystroke replaced the primary selection with newlines. It now skips the write when every selection is empty. ## Testing `cargo test -p editor --lib` passes on Linux. `test_empty_selections_keep_primary_selection` is new, compiled only on Linux and FreeBSD, and fails without the fix. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed multiple cursors overwriting the primary selection with newlines on Linux --- crates/editor/src/editor_tests.rs | 18 ++++++++++++++++++ crates/editor/src/selection.rs | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index ff4c0e33a12..67f75e6ead1 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -10927,6 +10927,24 @@ async fn test_editing_untitled_buffer_redetects_language(cx: &mut TestAppContext ); } +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +#[gpui::test] +async fn test_empty_selections_keep_primary_selection(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + let primary_text = |cx: &mut EditorTestContext| { + cx.update_editor(|_, _, cx| cx.read_from_primary()) + .and_then(|item| item.text()) + }; + + cx.set_state("«oneˇ» two"); + assert_eq!(primary_text(&mut cx), Some("one".to_string())); + + cx.set_selections_state("ˇone ˇtwo"); + assert_eq!(primary_text(&mut cx), Some("one".to_string())); +} + #[gpui::test] async fn test_clipboard(cx: &mut TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/editor/src/selection.rs b/crates/editor/src/selection.rs index 7e8990ab719..c378e4455aa 100644 --- a/crates/editor/src/selection.rs +++ b/crates/editor/src/selection.rs @@ -1562,7 +1562,7 @@ impl Editor { } } - if !text.is_empty() { + if selections.iter().any(|selection| !selection.is_empty()) { cx.write_to_primary(ClipboardItem::new_string(text)); } }