From 3635d2dcedd83f1b6702f33ca2673317f7fa4695 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 9 Feb 2024 16:18:09 -0700 Subject: [PATCH] Highlight selections on vim yank (#7638) Fixes: #7311 Co-Authored-By: WindSoilder Release Notes: - Added a highlight on yanked text in vim normal mode **or** - N/A Co-authored-by: WindSoilder --- crates/vim/src/normal/yank.rs | 6 ++--- crates/vim/src/utils.rs | 50 +++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/crates/vim/src/normal/yank.rs b/crates/vim/src/normal/yank.rs index 33833500fa..7792fc4dd7 100644 --- a/crates/vim/src/normal/yank.rs +++ b/crates/vim/src/normal/yank.rs @@ -1,4 +1,4 @@ -use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}; +use crate::{motion::Motion, object::Object, utils::copy_and_flash_selections_content, Vim}; use collections::HashMap; use gpui::WindowContext; @@ -15,7 +15,7 @@ pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &mut motion.expand_selection(map, selection, times, true, &text_layout_details); }); }); - copy_selections_content(editor, motion.linewise(), cx); + copy_and_flash_selections_content(editor, motion.linewise(), cx); editor.change_selections(None, cx, |s| { s.move_with(|_, selection| { let (head, goal) = original_positions.remove(&selection.id).unwrap(); @@ -38,7 +38,7 @@ pub fn yank_object(vim: &mut Vim, object: Object, around: bool, cx: &mut WindowC original_positions.insert(selection.id, original_position); }); }); - copy_selections_content(editor, false, cx); + copy_and_flash_selections_content(editor, false, cx); editor.change_selections(None, cx, |s| { s.move_with(|_, selection| { let (head, goal) = original_positions.remove(&selection.id).unwrap(); diff --git a/crates/vim/src/utils.rs b/crates/vim/src/utils.rs index 0ff857af9c..8b913fabbd 100644 --- a/crates/vim/src/utils.rs +++ b/crates/vim/src/utils.rs @@ -1,12 +1,34 @@ +use std::time::Duration; + use editor::{ClipboardSelection, Editor}; -use gpui::{AppContext, ClipboardItem}; +use gpui::{ClipboardItem, ViewContext}; use language::{CharKind, Point}; -pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut AppContext) { +pub struct HighlightOnYank; + +pub fn copy_and_flash_selections_content( + editor: &mut Editor, + linewise: bool, + cx: &mut ViewContext, +) { + copy_selections_content_internal(editor, linewise, true, cx); +} + +pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut ViewContext) { + copy_selections_content_internal(editor, linewise, false, cx); +} + +fn copy_selections_content_internal( + editor: &mut Editor, + linewise: bool, + highlight: bool, + cx: &mut ViewContext, +) { let selections = editor.selections.all_adjusted(cx); let buffer = editor.buffer().read(cx).snapshot(cx); let mut text = String::new(); let mut clipboard_selections = Vec::with_capacity(selections.len()); + let mut ranges_to_highlight = Vec::new(); { let mut is_first = true; for selection in selections.iter() { @@ -32,6 +54,11 @@ pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut App if is_last_line { start = Point::new(start.row + 1, 0); } + + let start_anchor = buffer.anchor_after(start); + let end_anchor = buffer.anchor_before(end); + ranges_to_highlight.push(start_anchor..end_anchor); + for chunk in buffer.text_for_range(start..end) { text.push_str(chunk); } @@ -47,6 +74,25 @@ pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut App } cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections)); + if !highlight { + return; + } + + editor.highlight_background::( + ranges_to_highlight, + |colors| colors.editor_document_highlight_read_background, + cx, + ); + cx.spawn(|this, mut cx| async move { + cx.background_executor() + .timer(Duration::from_millis(200)) + .await; + this.update(&mut cx, |editor, cx| { + editor.clear_background_highlights::(cx) + }) + .ok(); + }) + .detach(); } pub fn coerce_punctuation(kind: CharKind, treat_punctuation_as_word: bool) -> CharKind {