utils.rs

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