Highlight selections on vim yank (#7638)
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>
This commit is contained in:
parent
efe23ebfcd
commit
3635d2dced
2 changed files with 51 additions and 5 deletions
|
@ -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 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::<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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue