Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Highlight selections on vim yank (#7638)
Conrad Irwin3635d2dcedd8
diff --git a/crates/vim/src/normal/yank.rs b/crates/vim/src/normal/yank.rsindex 33833500fab..7792fc4dd74 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<usize>, cx: &mutmotion.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 WindowCoriginal_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.rsindex 0ff857af9cd..8b913fabbdd 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<Editor>,+) {+ copy_selections_content_internal(editor, linewise, true, cx);+}++pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut ViewContext<Editor>) {+ copy_selections_content_internal(editor, linewise, false, cx);+}++fn copy_selections_content_internal(+ editor: &mut Editor,+ linewise: bool,+ highlight: bool,+ cx: &mut ViewContext<Editor>,+) {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 Appif 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::<HighlightOnYank>(+ 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::<HighlightOnYank>(cx)+ })+ .ok();+ })+ .detach();}pub fn coerce_punctuation(kind: CharKind, treat_punctuation_as_word: bool) -> CharKind {