utils.rs

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