1use gpui::ViewContext;
2use language::Point;
3use workspace::Workspace;
4
5use crate::{motion::Motion, normal::ChangeCase, Vim};
6
7pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext<Workspace>) {
8 Vim::update(cx, |vim, cx| {
9 let count = vim.pop_number_operator(cx);
10 vim.update_active_editor(cx, |editor, cx| {
11 editor.set_clip_at_line_ends(false, cx);
12 editor.transact(cx, |editor, cx| {
13 editor.change_selections(None, cx, |s| {
14 s.move_with(|map, selection| {
15 if selection.start == selection.end {
16 Motion::Right.expand_selection(map, selection, count, true);
17 }
18 })
19 });
20 let selections = editor.selections.all::<Point>(cx);
21 for selection in selections.into_iter().rev() {
22 let snapshot = editor.buffer().read(cx).snapshot(cx);
23 editor.buffer().update(cx, |buffer, cx| {
24 let range = selection.start..selection.end;
25 let text = snapshot
26 .text_for_range(selection.start..selection.end)
27 .flat_map(|s| s.chars())
28 .flat_map(|c| {
29 if c.is_lowercase() {
30 c.to_uppercase().collect::<Vec<char>>()
31 } else {
32 c.to_lowercase().collect::<Vec<char>>()
33 }
34 })
35 .collect::<String>();
36
37 buffer.edit([(range, text)], None, cx)
38 })
39 }
40 });
41 editor.set_clip_at_line_ends(true, cx);
42 });
43 })
44}
45
46#[cfg(test)]
47mod test {
48 use crate::{state::Mode, test::VimTestContext};
49 use indoc::indoc;
50
51 #[gpui::test]
52 async fn test_change_case(cx: &mut gpui::TestAppContext) {
53 let mut cx = VimTestContext::new(cx, true).await;
54 cx.set_state(indoc! {"ˇabC\n"}, Mode::Normal);
55 cx.simulate_keystrokes(["~"]);
56 cx.assert_editor_state("AˇbC\n");
57 cx.simulate_keystrokes(["2", "~"]);
58 cx.assert_editor_state("ABcˇ\n");
59
60 cx.set_state(indoc! {"a😀C«dÉ1*fˇ»\n"}, Mode::Normal);
61 cx.simulate_keystrokes(["~"]);
62 cx.assert_editor_state("a😀CDé1*Fˇ\n");
63 }
64}