Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
vim: Support s on selections and with multiple cursors
Conrad Irwine1f975e52ece
diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.jsonindex 389efcdd0b6..eb2d7ecf410 100644--- a/assets/keymaps/vim.json+++ b/assets/keymaps/vim.json@@ -302,6 +302,7 @@"x": "vim::VisualDelete","y": "vim::VisualYank","p": "vim::VisualPaste",+ "s": "vim::Substitute","r": ["vim::PushOperator","Replace"diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rsindex c72fffbc214..19d000d3970 100644--- a/crates/vim/src/normal.rs+++ b/crates/vim/src/normal.rs@@ -481,17 +481,17 @@ pub(crate) fn normal_replace(text: Arc<str>, cx: &mut WindowContext) {pub fn substitute(vim: &mut Vim, count: usize, cx: &mut WindowContext) {vim.update_active_editor(cx, |editor, cx| {editor.transact(cx, |editor, cx| {- let selection = editor.selections.newest::<Point>(cx);-- let end = if selection.start == selection.end {- selection.start + Point::new(0, 1)- } else {- selection.end- };-- editor.buffer().update(cx, |buffer, cx| {- buffer.edit([(selection.start..end, "")], None, cx)- })+ let selections = editor.selections.all::<Point>(cx);+ for selection in selections.into_iter().rev() {+ let end = if selection.start == selection.end {+ selection.start + Point::new(0, 1)+ } else {+ selection.end+ };+ editor.buffer().update(cx, |buffer, cx| {+ buffer.edit([(selection.start..end, "")], None, cx)+ })+ }})});vim.switch_mode(Mode::Insert, true, cx)diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rsindex ec8998adc93..293a4813e64 100644--- a/crates/vim/src/test.rs+++ b/crates/vim/src/test.rs@@ -99,12 +99,22 @@ async fn test_buffer_search(cx: &mut gpui::TestAppContext) {})}-#[gpui::test]async fn test_substitute(cx: &mut gpui::TestAppContext) {let mut cx = VimTestContext::new(cx, true).await;+ // supports a single cursorcx.set_state(indoc! {"ˇabc\n"}, Mode::Normal);cx.simulate_keystrokes(["s", "x"]);cx.assert_editor_state("xˇbc\n");++ // supports a selection+ cx.set_state(indoc! {"a«bcˇ»\n"}, Mode::Visual { line: false });+ cx.simulate_keystrokes(["s", "x"]);+ cx.assert_editor_state("axˇ\n");++ // supports multiple cursors+ cx.set_state(indoc! {"a«bcˇ»deˇfg\n"}, Mode::Normal);+ cx.simulate_keystrokes(["s", "x"]);+ cx.assert_editor_state("axˇdexˇg\n");}