Vim toggle case (#2648)

Release Notes:

- vim: Add ~ to toggle case
([#1410](https://github.com/zed-industries/community/issues/1410))
This commit is contained in:
Nathan Sobo 2023-06-27 04:13:24 -06:00 committed by GitHub
commit fd3ee0ebd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 9 deletions

View file

@ -168,6 +168,7 @@
"^": "vim::FirstNonWhitespace", "^": "vim::FirstNonWhitespace",
"o": "vim::InsertLineBelow", "o": "vim::InsertLineBelow",
"shift-o": "vim::InsertLineAbove", "shift-o": "vim::InsertLineAbove",
"~": "vim::ChangeCase",
"v": [ "v": [
"vim::SwitchMode", "vim::SwitchMode",
{ {
@ -297,6 +298,7 @@
"y": "vim::VisualYank", "y": "vim::VisualYank",
"p": "vim::VisualPaste", "p": "vim::VisualPaste",
"s": "vim::Substitute", "s": "vim::Substitute",
"~": "vim::ChangeCase",
"r": [ "r": [
"vim::PushOperator", "vim::PushOperator",
"Replace" "Replace"

View file

@ -1,3 +1,4 @@
mod case;
mod change; mod change;
mod delete; mod delete;
mod scroll; mod scroll;
@ -23,6 +24,7 @@ use log::error;
use workspace::Workspace; use workspace::Workspace;
use self::{ use self::{
case::change_case,
change::{change_motion, change_object}, change::{change_motion, change_object},
delete::{delete_motion, delete_object}, delete::{delete_motion, delete_object},
substitute::substitute, substitute::substitute,
@ -44,6 +46,7 @@ actions!(
Paste, Paste,
Yank, Yank,
Substitute, Substitute,
ChangeCase,
] ]
); );
@ -53,6 +56,7 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(insert_end_of_line); cx.add_action(insert_end_of_line);
cx.add_action(insert_line_above); cx.add_action(insert_line_above);
cx.add_action(insert_line_below); cx.add_action(insert_line_below);
cx.add_action(change_case);
cx.add_action(|_: &mut Workspace, _: &Substitute, cx| { cx.add_action(|_: &mut Workspace, _: &Substitute, cx| {
Vim::update(cx, |vim, cx| { Vim::update(cx, |vim, cx| {
let times = vim.pop_number_operator(cx); let times = vim.pop_number_operator(cx);

View file

@ -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<Workspace>) {
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::<Point>(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::<Vec<char>>()
} else {
c.to_lowercase().collect::<Vec<char>>()
}
})
.collect::<String>();
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");
}
}

View file

@ -6,6 +6,7 @@ use crate::{motion::Motion, Mode, Vim};
pub fn substitute(vim: &mut Vim, count: Option<usize>, cx: &mut WindowContext) { pub fn substitute(vim: &mut Vim, count: Option<usize>, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, cx| { vim.update_active_editor(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);
editor.transact(cx, |editor, cx| {
editor.change_selections(None, cx, |s| { editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| { s.move_with(|map, selection| {
if selection.start == selection.end { if selection.start == selection.end {
@ -13,7 +14,6 @@ pub fn substitute(vim: &mut Vim, count: Option<usize>, cx: &mut WindowContext) {
} }
}) })
}); });
editor.transact(cx, |editor, cx| {
let selections = editor.selections.all::<Point>(cx); let selections = editor.selections.all::<Point>(cx);
for selection in selections.into_iter().rev() { for selection in selections.into_iter().rev() {
editor.buffer().update(cx, |buffer, cx| { editor.buffer().update(cx, |buffer, cx| {
@ -63,7 +63,11 @@ mod test {
// it handles multibyte characters // it handles multibyte characters
cx.set_state(indoc! {"ˇcàfé\n"}, Mode::Normal); cx.set_state(indoc! {"ˇcàfé\n"}, Mode::Normal);
cx.simulate_keystrokes(["4", "s", "x"]); cx.simulate_keystrokes(["4", "s"]);
cx.assert_editor_state("\n"); cx.assert_editor_state("ˇ\n");
// should transactionally undo selection changes
cx.simulate_keystrokes(["escape", "u"]);
cx.assert_editor_state("ˇcàfé\n");
} }
} }