yank.rs

 1use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
 2use collections::HashMap;
 3use gpui::WindowContext;
 4
 5pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
 6    vim.update_active_editor(cx, |editor, cx| {
 7        editor.transact(cx, |editor, cx| {
 8            editor.set_clip_at_line_ends(false, cx);
 9            let mut original_positions: HashMap<_, _> = Default::default();
10            editor.change_selections(None, cx, |s| {
11                s.move_with(|map, selection| {
12                    let original_position = (selection.head(), selection.goal);
13                    original_positions.insert(selection.id, original_position);
14                    motion.expand_selection(map, selection, times, true);
15                });
16            });
17            copy_selections_content(editor, motion.linewise(), cx);
18            editor.change_selections(None, cx, |s| {
19                s.move_with(|_, selection| {
20                    let (head, goal) = original_positions.remove(&selection.id).unwrap();
21                    selection.collapse_to(head, goal);
22                });
23            });
24        });
25    });
26}
27
28pub fn yank_object(vim: &mut Vim, object: Object, around: bool, cx: &mut WindowContext) {
29    vim.update_active_editor(cx, |editor, cx| {
30        editor.transact(cx, |editor, cx| {
31            editor.set_clip_at_line_ends(false, cx);
32            let mut original_positions: HashMap<_, _> = Default::default();
33            editor.change_selections(None, cx, |s| {
34                s.move_with(|map, selection| {
35                    let original_position = (selection.head(), selection.goal);
36                    object.expand_selection(map, selection, around);
37                    original_positions.insert(selection.id, original_position);
38                });
39            });
40            copy_selections_content(editor, false, cx);
41            editor.change_selections(None, cx, |s| {
42                s.move_with(|_, selection| {
43                    let (head, goal) = original_positions.remove(&selection.id).unwrap();
44                    selection.collapse_to(head, goal);
45                });
46            });
47        });
48    });
49}