toggle_comments.rs

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