utils.rs

 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        let mut is_first = true;
11        for selection in selections.iter() {
12            let start = selection.start;
13            let end = selection.end;
14            if is_first {
15                is_first = false;
16            } else {
17                text.push_str("\n");
18            }
19            let initial_len = text.len();
20            for chunk in buffer.text_for_range(start..end) {
21                text.push_str(chunk);
22            }
23            clipboard_selections.push(ClipboardSelection {
24                len: text.len() - initial_len,
25                is_entire_line: linewise,
26                first_line_indent: buffer.indent_size_for_line(start.row).len,
27            });
28        }
29    }
30
31    cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
32}