toggle_comments.rs

 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        window: &mut Window,
13        cx: &mut Context<Self>,
14    ) {
15        self.stop_recording(cx);
16        self.update_editor(window, cx, |_, editor, window, cx| {
17            let text_layout_details = editor.text_layout_details(window);
18            editor.transact(window, cx, |editor, window, cx| {
19                let mut selection_starts: HashMap<_, _> = Default::default();
20                editor.change_selections(None, window, cx, |s| {
21                    s.move_with(|map, selection| {
22                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
23                        selection_starts.insert(selection.id, anchor);
24                        motion.expand_selection(map, selection, times, &text_layout_details);
25                    });
26                });
27                editor.toggle_comments(&Default::default(), window, cx);
28                editor.change_selections(None, window, cx, |s| {
29                    s.move_with(|map, selection| {
30                        let anchor = selection_starts.remove(&selection.id).unwrap();
31                        selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
32                    });
33                });
34            });
35        });
36    }
37
38    pub fn toggle_comments_object(
39        &mut self,
40        object: Object,
41        around: bool,
42        window: &mut Window,
43        cx: &mut Context<Self>,
44    ) {
45        self.stop_recording(cx);
46        self.update_editor(window, cx, |_, editor, window, cx| {
47            editor.transact(window, cx, |editor, window, cx| {
48                let mut original_positions: HashMap<_, _> = Default::default();
49                editor.change_selections(None, window, cx, |s| {
50                    s.move_with(|map, selection| {
51                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
52                        original_positions.insert(selection.id, anchor);
53                        object.expand_selection(map, selection, around);
54                    });
55                });
56                editor.toggle_comments(&Default::default(), window, cx);
57                editor.change_selections(None, window, cx, |s| {
58                    s.move_with(|map, selection| {
59                        let anchor = original_positions.remove(&selection.id).unwrap();
60                        selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
61                    });
62                });
63            });
64        });
65    }
66}