utils.rs

 1use editor::{ClipboardSelection, Editor};
 2use gpui::{ClipboardItem, MutableAppContext};
 3use language::Point;
 4
 5pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut MutableAppContext) {
 6    let selections = editor.selections.all::<Point>(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            });
22        }
23    }
24
25    cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
26}