1use editor::{ClipboardSelection, Editor};
2use gpui::{AppContext, ClipboardItem};
3
4pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut AppContext) {
5 let selections = editor.selections.all_adjusted(cx);
6 let buffer = editor.buffer().read(cx).snapshot(cx);
7 let mut text = String::new();
8 let mut clipboard_selections = Vec::with_capacity(selections.len());
9 {
10 for selection in selections.iter() {
11 let initial_len = text.len();
12 let start = selection.start;
13 let end = selection.end;
14 for chunk in buffer.text_for_range(start..end) {
15 text.push_str(chunk);
16 }
17 clipboard_selections.push(ClipboardSelection {
18 len: text.len() - initial_len,
19 is_entire_line: linewise,
20 first_line_indent: buffer.indent_size_for_line(start.row).len,
21 });
22 }
23 }
24
25 cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
26}