1use std::time::Duration;
2
3use editor::{ClipboardSelection, Editor};
4use gpui::{ClipboardItem, ViewContext};
5use language::{CharKind, Point};
6use multi_buffer::MultiBufferRow;
7use settings::Settings;
8
9use crate::{state::Mode, UseSystemClipboard, Vim, VimSettings};
10
11pub struct HighlightOnYank;
12
13pub fn yank_selections_content(
14 vim: &mut Vim,
15 editor: &mut Editor,
16 linewise: bool,
17 cx: &mut ViewContext<Editor>,
18) {
19 copy_selections_content_internal(vim, editor, linewise, true, cx);
20}
21
22pub fn copy_selections_content(
23 vim: &mut Vim,
24 editor: &mut Editor,
25 linewise: bool,
26 cx: &mut ViewContext<Editor>,
27) {
28 copy_selections_content_internal(vim, editor, linewise, false, cx);
29}
30
31fn copy_selections_content_internal(
32 vim: &mut Vim,
33 editor: &mut Editor,
34 linewise: bool,
35 is_yank: bool,
36 cx: &mut ViewContext<Editor>,
37) {
38 let selections = editor.selections.all_adjusted(cx);
39 let buffer = editor.buffer().read(cx).snapshot(cx);
40 let mut text = String::new();
41 let mut clipboard_selections = Vec::with_capacity(selections.len());
42 let mut ranges_to_highlight = Vec::new();
43
44 vim.update_state(|state| {
45 state.marks.insert(
46 "[".to_string(),
47 selections
48 .iter()
49 .map(|s| buffer.anchor_before(s.start))
50 .collect(),
51 );
52 state.marks.insert(
53 "]".to_string(),
54 selections
55 .iter()
56 .map(|s| buffer.anchor_after(s.end))
57 .collect(),
58 )
59 });
60
61 {
62 let mut is_first = true;
63 for selection in selections.iter() {
64 let mut start = selection.start;
65 let end = selection.end;
66 if is_first {
67 is_first = false;
68 } else {
69 text.push_str("\n");
70 }
71 let initial_len = text.len();
72
73 // if the file does not end with \n, and our line-mode selection ends on
74 // that line, we will have expanded the start of the selection to ensure it
75 // contains a newline (so that delete works as expected). We undo that change
76 // here.
77 let is_last_line = linewise
78 && end.row == buffer.max_buffer_row().0
79 && buffer.max_point().column > 0
80 && start.row < buffer.max_buffer_row().0
81 && start == Point::new(start.row, buffer.line_len(MultiBufferRow(start.row)));
82
83 if is_last_line {
84 start = Point::new(start.row + 1, 0);
85 }
86
87 let start_anchor = buffer.anchor_after(start);
88 let end_anchor = buffer.anchor_before(end);
89 ranges_to_highlight.push(start_anchor..end_anchor);
90
91 for chunk in buffer.text_for_range(start..end) {
92 text.push_str(chunk);
93 }
94 if is_last_line {
95 text.push_str("\n");
96 }
97 clipboard_selections.push(ClipboardSelection {
98 len: text.len() - initial_len,
99 is_entire_line: linewise,
100 first_line_indent: buffer.indent_size_for_line(MultiBufferRow(start.row)).len,
101 });
102 }
103 }
104
105 let setting = VimSettings::get_global(cx).use_system_clipboard;
106 if setting == UseSystemClipboard::Always || setting == UseSystemClipboard::OnYank && is_yank {
107 cx.write_to_clipboard(ClipboardItem::new(text.clone()).with_metadata(clipboard_selections));
108 vim.workspace_state
109 .registers
110 .insert(".system.".to_string(), text.clone());
111 } else {
112 vim.workspace_state.registers.insert(
113 ".system.".to_string(),
114 cx.read_from_clipboard()
115 .map(|item| item.text().clone())
116 .unwrap_or_default(),
117 );
118 }
119 vim.workspace_state.registers.insert("\"".to_string(), text);
120 if !is_yank || vim.state().mode == Mode::Visual {
121 return;
122 }
123
124 editor.highlight_background::<HighlightOnYank>(
125 &ranges_to_highlight,
126 |colors| colors.editor_document_highlight_read_background,
127 cx,
128 );
129 cx.spawn(|this, mut cx| async move {
130 cx.background_executor()
131 .timer(Duration::from_millis(200))
132 .await;
133 this.update(&mut cx, |editor, cx| {
134 editor.clear_background_highlights::<HighlightOnYank>(cx)
135 })
136 .ok();
137 })
138 .detach();
139}
140
141pub fn coerce_punctuation(kind: CharKind, treat_punctuation_as_word: bool) -> CharKind {
142 if treat_punctuation_as_word && kind == CharKind::Punctuation {
143 CharKind::Word
144 } else {
145 kind
146 }
147}