From 1dba0f2b1ccc0c6a29e05876b7b7153373221f87 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 6 Oct 2020 16:00:23 +0900 Subject: Simple yank/paste registers. --- helix-view/src/commands.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'helix-view/src/commands.rs') diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index 9a6d2e5d..ca1e41c4 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -1,7 +1,7 @@ use helix_core::{ graphemes, regex::Regex, - selection, + register, selection, state::{Direction, Granularity, Mode, State}, ChangeSet, Range, Selection, Tendril, Transaction, }; @@ -443,3 +443,36 @@ pub fn undo(view: &mut View, _count: usize) { pub fn redo(view: &mut View, _count: usize) { view.history.redo(&mut view.state); } + +// Yank / Paste + +pub fn yank(view: &mut View, _count: usize) { + // TODO: should selections be made end inclusive? + let values = view + .state + .selection() + .fragments(&view.state.doc().slice(..)) + .map(|cow| cow.into_owned()) + .collect(); + + register::set('"', values); +} + +pub fn paste(view: &mut View, _count: usize) { + if let Some(values) = register::get('"') { + let repeat = std::iter::repeat( + values + .last() + .map(|value| Tendril::from_slice(value)) + .unwrap(), + ); + + let mut values = values.into_iter().map(Tendril::from).chain(repeat); + + let transaction = Transaction::change_by_selection(&view.state, |range| { + (range.head + 1, range.head + 1, Some(values.next().unwrap())) + }); + + transaction.apply(&mut view.state); + } +} -- cgit v1.2.3-70-g09d2