1use crate::{Vim, motion::Motion, object::Object};
2use collections::HashMap;
3use editor::{Bias, 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(window, cx, |_, editor, window, cx| {
18 let text_layout_details = editor.text_layout_details(window);
19 editor.transact(window, cx, |editor, window, cx| {
20 let mut selection_starts: HashMap<_, _> = Default::default();
21 editor.change_selections(None, window, cx, |s| {
22 s.move_with(|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(None, window, cx, |s| {
36 s.move_with(|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 window: &mut Window,
50 cx: &mut Context<Self>,
51 ) {
52 self.stop_recording(cx);
53 self.update_editor(window, cx, |_, editor, window, cx| {
54 editor.transact(window, cx, |editor, window, cx| {
55 let mut original_positions: HashMap<_, _> = Default::default();
56 editor.change_selections(None, window, cx, |s| {
57 s.move_with(|map, selection| {
58 let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
59 original_positions.insert(selection.id, anchor);
60 object.expand_selection(map, selection, around);
61 });
62 });
63 editor.toggle_comments(&Default::default(), window, cx);
64 editor.change_selections(None, window, cx, |s| {
65 s.move_with(|map, selection| {
66 let anchor = original_positions.remove(&selection.id).unwrap();
67 selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
68 });
69 });
70 });
71 });
72 }
73}