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