yank.rs

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