1use editor::scroll::autoscroll::Autoscroll;
2use gpui::ViewContext;
3use language::{Bias, Point};
4use workspace::Workspace;
5
6use crate::{normal::ChangeCase, state::Mode, Vim};
7
8pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext<Workspace>) {
9 Vim::update(cx, |vim, cx| {
10 let count = vim.pop_number_operator(cx).unwrap_or(1) as u32;
11 vim.update_active_editor(cx, |editor, cx| {
12 let mut ranges = Vec::new();
13 let mut cursor_positions = Vec::new();
14 let snapshot = editor.buffer().read(cx).snapshot(cx);
15 for selection in editor.selections.all::<Point>(cx) {
16 match vim.state.mode {
17 Mode::Visual { line: true } => {
18 let start = Point::new(selection.start.row, 0);
19 let end =
20 Point::new(selection.end.row, snapshot.line_len(selection.end.row));
21 ranges.push(start..end);
22 cursor_positions.push(start..start);
23 }
24 Mode::Visual { line: false } => {
25 ranges.push(selection.start..selection.end);
26 cursor_positions.push(selection.start..selection.start);
27 }
28 Mode::Insert | Mode::Normal => {
29 let start = selection.start;
30 let mut end = start;
31 for _ in 0..count {
32 end = snapshot.clip_point(end + Point::new(0, 1), Bias::Right);
33 }
34 ranges.push(start..end);
35
36 if end.column == snapshot.line_len(end.row) {
37 end = snapshot.clip_point(end - Point::new(0, 1), Bias::Left);
38 }
39 cursor_positions.push(end..end)
40 }
41 }
42 }
43 editor.transact(cx, |editor, cx| {
44 for range in ranges.into_iter().rev() {
45 let snapshot = editor.buffer().read(cx).snapshot(cx);
46 editor.buffer().update(cx, |buffer, cx| {
47 let text = snapshot
48 .text_for_range(range.start..range.end)
49 .flat_map(|s| s.chars())
50 .flat_map(|c| {
51 if c.is_lowercase() {
52 c.to_uppercase().collect::<Vec<char>>()
53 } else {
54 c.to_lowercase().collect::<Vec<char>>()
55 }
56 })
57 .collect::<String>();
58
59 buffer.edit([(range, text)], None, cx)
60 })
61 }
62 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
63 s.select_ranges(cursor_positions)
64 })
65 });
66 });
67 vim.switch_mode(Mode::Normal, true, cx)
68 })
69}
70#[cfg(test)]
71mod test {
72 use crate::{state::Mode, test::NeovimBackedTestContext};
73
74 #[gpui::test]
75 async fn test_change_case(cx: &mut gpui::TestAppContext) {
76 let mut cx = NeovimBackedTestContext::new(cx).await;
77 cx.set_shared_state("ˇabC\n").await;
78 cx.simulate_shared_keystrokes(["~"]).await;
79 cx.assert_shared_state("AˇbC\n").await;
80 cx.simulate_shared_keystrokes(["2", "~"]).await;
81 cx.assert_shared_state("ABˇc\n").await;
82
83 // works in visual mode
84 cx.set_shared_state("a😀C«dÉ1*fˇ»\n").await;
85 cx.simulate_shared_keystrokes(["~"]).await;
86 cx.assert_shared_state("a😀CˇDé1*F\n").await;
87
88 // works with multibyte characters
89 cx.simulate_shared_keystrokes(["~"]).await;
90 cx.set_shared_state("aˇC😀é1*F\n").await;
91 cx.simulate_shared_keystrokes(["4", "~"]).await;
92 cx.assert_shared_state("ac😀É1ˇ*F\n").await;
93
94 // works with line selections
95 cx.set_shared_state("abˇC\n").await;
96 cx.simulate_shared_keystrokes(["shift-v", "~"]).await;
97 cx.assert_shared_state("ˇABc\n").await;
98
99 // works with multiple cursors (zed only)
100 cx.set_state("aˇßcdˇe\n", Mode::Normal);
101 cx.simulate_keystroke("~");
102 cx.assert_state("aSSˇcdˇE\n", Mode::Normal);
103 }
104}