Skip to content

Commit

Permalink
Highlight selections on vim yank (#7638)
Browse files Browse the repository at this point in the history
Fixes: #7311

Co-Authored-By: WindSoilder <WindSoilder@outlook.com>

Release Notes:

- Added a highlight on yanked text in vim normal mode

**or**

- N/A

Co-authored-by: WindSoilder <WindSoilder@outlook.com>
  • Loading branch information
ConradIrwin and WindSoilder authored Feb 9, 2024
1 parent efe23eb commit 3635d2d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions crates/vim/src/normal/yank.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,7 +15,7 @@ pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, 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();
Expand All @@ -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();
Expand Down
50 changes: 48 additions & 2 deletions crates/vim/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down

0 comments on commit 3635d2d

Please sign in to comment.