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.

vim: Preserve system clipboard when pasting over visual selection (#52948)

João Soares179af67c0fa1
diff --git a/crates/vim/src/normal/paste.rs b/crates/vim/src/normal/paste.rs
index fab9b353e3e..f3df2ce1c07 100644
--- a/crates/vim/src/normal/paste.rs
+++ b/crates/vim/src/normal/paste.rs
@@ -25,7 +25,7 @@ pub struct Paste {
#[serde(default)]
before: bool,
#[serde(default)]
- preserve_clipboard: bool,
+ pub(crate) preserve_clipboard: bool,
}
impl Vim {
@@ -835,6 +835,41 @@ mod test {
);
}
+ #[gpui::test]
+ async fn test_editor_paste_visual_preserves_system_clipboard(cx: &mut gpui::TestAppContext) {
+ let mut cx = VimTestContext::new(cx, true).await;
+
+ cx.set_state(
+ indoc! {"
+ The quick brown
+ fox ˇjumps over
+ the lazy dog"},
+ Mode::Normal,
+ );
+
+ // Put known content on the system clipboard
+ cx.write_to_clipboard(ClipboardItem::new_string("from clipboard".to_string()));
+
+ // Select "jumps" in visual mode, then editor::Paste (Cmd-V / Ctrl-V)
+ cx.simulate_keystrokes("v i w");
+ cx.dispatch_action(editor::actions::Paste);
+
+ // The selected text should be replaced with clipboard content
+ cx.assert_state(
+ indoc! {"
+ The quick brown
+ fox from clipboarˇd over
+ the lazy dog"},
+ Mode::Normal,
+ );
+
+ // System clipboard must still hold the original value, not "jumps"
+ assert_eq!(
+ cx.read_from_clipboard().map(|item| item.text().unwrap()),
+ Some("from clipboard".into()),
+ );
+ }
+
#[gpui::test]
async fn test_numbered_registers(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs
index 987def8a1ce..d9617d34ecf 100644
--- a/crates/vim/src/vim.rs
+++ b/crates/vim/src/vim.rs
@@ -965,7 +965,9 @@ impl Vim {
Mode::Replace => vim.paste_replace(window, cx),
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
vim.selected_register.replace('+');
- vim.paste(&VimPaste::default(), window, cx);
+ let mut action = VimPaste::default();
+ action.preserve_clipboard = true;
+ vim.paste(&action, window, cx);
}
_ => {
vim.update_editor(cx, |_, editor, cx| editor.paste(&Paste, window, cx));