From c0c0abae568d5fad8cd8c796434aaf27f03066d1 Mon Sep 17 00:00:00 2001 From: Brooks Swinnerton Date: Sun, 28 Jan 2024 15:13:09 -0500 Subject: [PATCH] Add support for u and U in vim visual mode --- assets/keymaps/vim.json | 3 +- crates/vim/src/normal.rs | 6 +- crates/vim/src/normal/case.rs | 80 +++++++++++++++++-- .../test_data/test_convert_to_lower_case.json | 12 +++ .../test_data/test_convert_to_upper_case.json | 12 +++ 5 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 crates/vim/test_data/test_convert_to_lower_case.json create mode 100644 crates/vim/test_data/test_convert_to_upper_case.json diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index 32acb90d69..0bdf1aae32 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -400,7 +400,8 @@ { "context": "Editor && vim_mode == visual && !VimWaiting && !VimObject", "bindings": { - "u": "editor::Undo", + "u": "vim::ConvertToLowerCase", + "U": "vim::ConvertToUpperCase", "o": "vim::OtherEnd", "shift-o": "vim::OtherEnd", "d": "vim::VisualDelete", diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index c21f54f2d3..399180fea4 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -26,7 +26,7 @@ use log::error; use workspace::Workspace; use self::{ - case::change_case, + case::{change_case, convert_to_lower_case, convert_to_upper_case}, change::{change_motion, change_object}, delete::{delete_motion, delete_object}, yank::{yank_motion, yank_object}, @@ -48,6 +48,8 @@ actions!( Yank, YankLine, ChangeCase, + ConvertToUpperCase, + ConvertToLowerCase, JoinLines, ] ); @@ -60,6 +62,8 @@ pub(crate) fn register(workspace: &mut Workspace, cx: &mut ViewContext) { + transform_case(cx, |c| { + if c.is_lowercase() { + c.to_uppercase().collect::>() + } else { + c.to_lowercase().collect::>() + } + }) +} + +pub fn convert_to_upper_case( + _: &mut Workspace, + _: &ConvertToUpperCase, + cx: &mut ViewContext, +) { + transform_case(cx, |c| c.to_uppercase().collect::>()) +} + +pub fn convert_to_lower_case( + _: &mut Workspace, + _: &ConvertToLowerCase, + cx: &mut ViewContext, +) { + transform_case(cx, |c| c.to_lowercase().collect::>()) +} + +fn transform_case(cx: &mut ViewContext, transform: F) +where + F: Fn(char) -> Vec + Copy, +{ Vim::update(cx, |vim, cx| { vim.record_current_action(cx); let count = vim.take_count(cx).unwrap_or(1) as u32; @@ -54,13 +85,7 @@ pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext>() - } else { - c.to_lowercase().collect::>() - } - }) + .flat_map(|c| transform(c)) .collect::(); buffer.edit([(range, text)], None, cx) @@ -74,6 +99,7 @@ pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext