yank.rs

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