toggle_comments.rs

 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);
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(|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(|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(|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(|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}