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