1use crate::{Vim, motion::Motion, object::Object};
2use collections::HashMap;
3use editor::{Bias, SelectionEffects, display_map::ToDisplayPoint};
4use gpui::{Context, Window};
5use language::SelectionGoal;
6
7impl Vim {
8 pub fn toggle_comments_motion(
9 &mut self,
10 motion: Motion,
11 times: Option<usize>,
12 forced_motion: bool,
13 window: &mut Window,
14 cx: &mut Context<Self>,
15 ) {
16 self.stop_recording(cx);
17 self.update_editor(cx, |_, editor, cx| {
18 let text_layout_details = editor.text_layout_details(window, cx);
19 editor.transact(window, cx, |editor, window, cx| {
20 let mut selection_starts: HashMap<_, _> = Default::default();
21 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
22 s.move_with(&mut |map, selection| {
23 let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
24 selection_starts.insert(selection.id, anchor);
25 motion.expand_selection(
26 map,
27 selection,
28 times,
29 &text_layout_details,
30 forced_motion,
31 );
32 });
33 });
34 editor.toggle_comments(&Default::default(), window, cx);
35 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
36 s.move_with(&mut |map, selection| {
37 let anchor = selection_starts.remove(&selection.id).unwrap();
38 selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
39 });
40 });
41 });
42 });
43 }
44
45 pub fn toggle_comments_object(
46 &mut self,
47 object: Object,
48 around: bool,
49 times: Option<usize>,
50 window: &mut Window,
51 cx: &mut Context<Self>,
52 ) {
53 self.stop_recording(cx);
54 self.update_editor(cx, |_, editor, cx| {
55 editor.transact(window, cx, |editor, window, cx| {
56 let mut original_positions: HashMap<_, _> = Default::default();
57 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
58 s.move_with(&mut |map, selection| {
59 let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
60 original_positions.insert(selection.id, anchor);
61 object.expand_selection(map, selection, around, times);
62 });
63 });
64 editor.toggle_comments(&Default::default(), window, cx);
65 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
66 s.move_with(&mut |map, selection| {
67 let anchor = original_positions.remove(&selection.id).unwrap();
68 selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
69 });
70 });
71 });
72 });
73 }
74
75 pub fn toggle_block_comments_motion(
76 &mut self,
77 motion: Motion,
78 times: Option<usize>,
79 forced_motion: bool,
80 window: &mut Window,
81 cx: &mut Context<Self>,
82 ) {
83 self.stop_recording(cx);
84 self.update_editor(cx, |_, editor, cx| {
85 let text_layout_details = editor.text_layout_details(window, cx);
86 editor.transact(window, cx, |editor, window, cx| {
87 editor.set_clip_at_line_ends(false, cx);
88 let mut selection_starts: HashMap<_, _> = Default::default();
89 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
90 s.move_with(&mut |map, selection| {
91 let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
92 selection_starts.insert(selection.id, anchor);
93 motion.expand_selection(
94 map,
95 selection,
96 times,
97 &text_layout_details,
98 forced_motion,
99 );
100 });
101 });
102 editor.toggle_block_comments(&Default::default(), window, cx);
103 editor.set_clip_at_line_ends(true, cx);
104 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
105 s.move_with(&mut |map, selection| {
106 let anchor = selection_starts.remove(&selection.id).unwrap();
107 selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
108 });
109 });
110 });
111 });
112 }
113
114 pub fn toggle_block_comments_object(
115 &mut self,
116 object: Object,
117 around: bool,
118 times: Option<usize>,
119 window: &mut Window,
120 cx: &mut Context<Self>,
121 ) {
122 self.stop_recording(cx);
123 self.update_editor(cx, |_, editor, cx| {
124 editor.transact(window, cx, |editor, window, cx| {
125 editor.set_clip_at_line_ends(false, cx);
126 let mut original_positions: HashMap<_, _> = Default::default();
127 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
128 s.move_with(&mut |map, selection| {
129 let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
130 original_positions.insert(selection.id, anchor);
131 object.expand_selection(map, selection, around, times);
132 });
133 });
134 editor.toggle_block_comments(&Default::default(), window, cx);
135 editor.set_clip_at_line_ends(true, cx);
136 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
137 s.move_with(&mut |map, selection| {
138 let anchor = original_positions.remove(&selection.id).unwrap();
139 selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
140 });
141 });
142 });
143 });
144 }
145}