toggle_comments.rs

 1use crate::{motion::Motion, object::Object, Vim};
 2use collections::HashMap;
 3use editor::{display_map::ToDisplayPoint, Bias};
 4use language::SelectionGoal;
 5use ui::ViewContext;
 6
 7impl Vim {
 8    pub fn toggle_comments_motion(
 9        &mut self,
10        motion: Motion,
11        times: Option<usize>,
12        cx: &mut ViewContext<Self>,
13    ) {
14        self.stop_recording(cx);
15        self.update_editor(cx, |_, editor, cx| {
16            let text_layout_details = editor.text_layout_details(cx);
17            editor.transact(cx, |editor, cx| {
18                let mut selection_starts: HashMap<_, _> = Default::default();
19                editor.change_selections(None, cx, |s| {
20                    s.move_with(|map, selection| {
21                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
22                        selection_starts.insert(selection.id, anchor);
23                        motion.expand_selection(map, selection, times, false, &text_layout_details);
24                    });
25                });
26                editor.toggle_comments(&Default::default(), cx);
27                editor.change_selections(None, cx, |s| {
28                    s.move_with(|map, selection| {
29                        let anchor = selection_starts.remove(&selection.id).unwrap();
30                        selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
31                    });
32                });
33            });
34        });
35    }
36
37    pub fn toggle_comments_object(
38        &mut self,
39        object: Object,
40        around: bool,
41        cx: &mut ViewContext<Self>,
42    ) {
43        self.stop_recording(cx);
44        self.update_editor(cx, |_, editor, cx| {
45            editor.transact(cx, |editor, cx| {
46                let mut original_positions: HashMap<_, _> = Default::default();
47                editor.change_selections(None, cx, |s| {
48                    s.move_with(|map, selection| {
49                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
50                        original_positions.insert(selection.id, anchor);
51                        object.expand_selection(map, selection, around);
52                    });
53                });
54                editor.toggle_comments(&Default::default(), cx);
55                editor.change_selections(None, cx, |s| {
56                    s.move_with(|map, selection| {
57                        let anchor = original_positions.remove(&selection.id).unwrap();
58                        selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
59                    });
60                });
61            });
62        });
63    }
64}