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
This commit is contained in:
Parker Loflin
2026-11-16 13:59:16 +00:00
committed by GitHub
parent f89b22bbea
commit 52b0e43064
2 changed files with 19 additions and 1 deletions
+18
View File
@@ -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, |_| {});
+1 -1
View File
@@ -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));
}
}