From 77dc22bff65d87f0b486c1cd231d70ff885f8141 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 26 Jun 2023 20:20:47 -0600 Subject: [PATCH 1/2] vim: Fix cursor restoration when undoing substitute --- crates/vim/src/normal/substitute.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/vim/src/normal/substitute.rs b/crates/vim/src/normal/substitute.rs index 87648f8b88..ef72baae31 100644 --- a/crates/vim/src/normal/substitute.rs +++ b/crates/vim/src/normal/substitute.rs @@ -6,14 +6,14 @@ use crate::{motion::Motion, Mode, Vim}; pub fn substitute(vim: &mut Vim, count: Option, cx: &mut WindowContext) { vim.update_active_editor(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); - editor.change_selections(None, cx, |s| { - s.move_with(|map, selection| { - if selection.start == selection.end { - Motion::Right.expand_selection(map, selection, count, true); - } - }) - }); editor.transact(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.move_with(|map, selection| { + if selection.start == selection.end { + Motion::Right.expand_selection(map, selection, count, true); + } + }) + }); let selections = editor.selections.all::(cx); for selection in selections.into_iter().rev() { editor.buffer().update(cx, |buffer, cx| { @@ -63,7 +63,11 @@ mod test { // it handles multibyte characters cx.set_state(indoc! {"ˇcàfé\n"}, Mode::Normal); - cx.simulate_keystrokes(["4", "s", "x"]); - cx.assert_editor_state("xˇ\n"); + cx.simulate_keystrokes(["4", "s"]); + cx.assert_editor_state("ˇ\n"); + + // should transactionally undo selection changes + cx.simulate_keystrokes(["escape", "u"]); + cx.assert_editor_state("ˇcàfé\n"); } } From a9aa5e5196e0b7e2abc67c6a567f1bfa90e893de Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 26 Jun 2023 20:19:28 -0600 Subject: [PATCH 2/2] vim: Add ~ to change case Fixes: zed-industries/community#1410 --- assets/keymaps/vim.json | 2 ++ crates/vim/src/normal.rs | 4 +++ crates/vim/src/normal/case.rs | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 crates/vim/src/normal/case.rs diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index 43b778d9b8..871ad13319 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -172,6 +172,7 @@ "^": "vim::FirstNonWhitespace", "o": "vim::InsertLineBelow", "shift-o": "vim::InsertLineAbove", + "~": "vim::ChangeCase", "v": [ "vim::SwitchMode", { @@ -309,6 +310,7 @@ "y": "vim::VisualYank", "p": "vim::VisualPaste", "s": "vim::Substitute", + "~": "vim::ChangeCase", "r": [ "vim::PushOperator", "Replace" diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index c54f739628..10251201c2 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -1,3 +1,4 @@ +mod case; mod change; mod delete; mod substitute; @@ -24,6 +25,7 @@ use serde::Deserialize; use workspace::Workspace; use self::{ + case::change_case, change::{change_motion, change_object}, delete::{delete_motion, delete_object}, substitute::substitute, @@ -48,6 +50,7 @@ actions!( Paste, Yank, Substitute, + ChangeCase, ] ); @@ -59,6 +62,7 @@ pub fn init(cx: &mut AppContext) { cx.add_action(insert_end_of_line); cx.add_action(insert_line_above); cx.add_action(insert_line_below); + cx.add_action(change_case); cx.add_action(|_: &mut Workspace, _: &Substitute, cx| { Vim::update(cx, |vim, cx| { let times = vim.pop_number_operator(cx); diff --git a/crates/vim/src/normal/case.rs b/crates/vim/src/normal/case.rs new file mode 100644 index 0000000000..ba527af0bb --- /dev/null +++ b/crates/vim/src/normal/case.rs @@ -0,0 +1,64 @@ +use gpui::ViewContext; +use language::Point; +use workspace::Workspace; + +use crate::{motion::Motion, normal::ChangeCase, Vim}; + +pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext) { + Vim::update(cx, |vim, cx| { + let count = vim.pop_number_operator(cx); + vim.update_active_editor(cx, |editor, cx| { + editor.set_clip_at_line_ends(false, cx); + editor.transact(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.move_with(|map, selection| { + if selection.start == selection.end { + Motion::Right.expand_selection(map, selection, count, true); + } + }) + }); + let selections = editor.selections.all::(cx); + for selection in selections.into_iter().rev() { + let snapshot = editor.buffer().read(cx).snapshot(cx); + editor.buffer().update(cx, |buffer, cx| { + let range = selection.start..selection.end; + let text = snapshot + .text_for_range(selection.start..selection.end) + .flat_map(|s| s.chars()) + .flat_map(|c| { + if c.is_lowercase() { + c.to_uppercase().collect::>() + } else { + c.to_lowercase().collect::>() + } + }) + .collect::(); + + buffer.edit([(range, text)], None, cx) + }) + } + }); + editor.set_clip_at_line_ends(true, cx); + }); + }) +} + +#[cfg(test)] +mod test { + use crate::{state::Mode, test::VimTestContext}; + use indoc::indoc; + + #[gpui::test] + async fn test_change_case(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + cx.set_state(indoc! {"ˇabC\n"}, Mode::Normal); + cx.simulate_keystrokes(["~"]); + cx.assert_editor_state("AˇbC\n"); + cx.simulate_keystrokes(["2", "~"]); + cx.assert_editor_state("ABcˇ\n"); + + cx.set_state(indoc! {"a😀C«dÉ1*fˇ»\n"}, Mode::Normal); + cx.simulate_keystrokes(["~"]); + cx.assert_editor_state("a😀CDé1*Fˇ\n"); + } +}