on master help sign in
Allow paste commands to take a count (#1261)

* Allow paste commands to take a count * Call `.repeat` within iterator methods * Implement counts for paste-replace

commit6da2174e14576ff07a0a2a28074fac5276b23d79 authorOmnikar <omnikar5@gmail.com> dateTue 14 Dec 2021, 03:49 (5 years ago) parent4527d63
1 file changed+49−19
helix-term/src/commands.rs+49−19
helix-term/src/commands.rs+49−19
@@ -2423,7 +2423,7 @@ pub mod cmd {
24232423 _args: &[Cow<str>],
24242424 _event: PromptEvent,
24252425 ) -> anyhow::Result<()> {
2426− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
2426+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
24272427 }
24282428
24292429 fn paste_clipboard_before(
@@ -2431,7 +2431,7 @@ pub mod cmd {
24312431 _args: &[Cow<str>],
24322432 _event: PromptEvent,
24332433 ) -> anyhow::Result<()> {
2434− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
2434+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
24352435 }
24362436
24372437 fn paste_primary_clipboard_after(
@@ -2439,7 +2439,7 @@ pub mod cmd {
24392439 _args: &[Cow<str>],
24402440 _event: PromptEvent,
24412441 ) -> anyhow::Result<()> {
2442− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
2442+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
24432443 }
24442444
24452445 fn paste_primary_clipboard_before(
@@ -2447,7 +2447,7 @@ pub mod cmd {
24472447 _args: &[Cow<str>],
24482448 _event: PromptEvent,
24492449 ) -> anyhow::Result<()> {
2450− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
2450+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
24512451 }
24522452
24532453 fn replace_selections_with_clipboard_impl(
@@ -4586,11 +4586,12 @@ fn paste_impl(
45864586 doc: &mut Document,
45874587 view: &View,
45884588 action: Paste,
4589+ count: usize,
45894590) -> Option<Transaction> {
45904591 let repeat = std::iter::repeat(
45914592 values
45924593 .last()
4593− .map(|value| Tendril::from_slice(value))
4594+ .map(|value| Tendril::from(value.repeat(count)))
45944595 .unwrap(),
45954596 );
45964597
@@ -4605,7 +4606,7 @@ fn paste_impl(
46054606 let mut values = values
46064607 .iter()
46074608 .map(|value| REGEX.replace_all(value, doc.line_ending.as_str()))
4608− .map(|value| Tendril::from(value.as_ref()))
4609+ .map(|value| Tendril::from(value.as_ref().repeat(count)))
46094610 .chain(repeat);
46104611
46114612 let text = doc.text();
@@ -4625,7 +4626,7 @@ fn paste_impl(
46254626 // paste append
46264627 (Paste::After, false) => range.to(),
46274628 };
4628− (pos, pos, Some(values.next().unwrap()))
4629+ (pos, pos, values.next())
46294630 });
46304631
46314632 Some(transaction)
@@ -4635,13 +4636,14 @@ fn paste_clipboard_impl(
46354636 editor: &mut Editor,
46364637 action: Paste,
46374638 clipboard_type: ClipboardType,
4639+ count: usize,
46384640) -> anyhow::Result<()> {
46394641 let (view, doc) = current!(editor);
46404642
46414643 match editor
46424644 .clipboard_provider
46434645 .get_contents(clipboard_type)
4644− .map(|contents| paste_impl(&[contents], doc, view, action))
4646+ .map(|contents| paste_impl(&[contents], doc, view, action, count))
46454647 {
46464648 Ok(Some(transaction)) => {
46474649 doc.apply(&transaction, view.id);
@@ -4654,22 +4656,43 @@ fn paste_clipboard_impl(
46544656}
46554657
46564658fn paste_clipboard_after(cx: &mut Context) {
4657− let _ = paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard);
4659+ let _ = paste_clipboard_impl(
4660+ cx.editor,
4661+ Paste::After,
4662+ ClipboardType::Clipboard,
4663+ cx.count(),
4664+ );
46584665}
46594666
46604667fn paste_clipboard_before(cx: &mut Context) {
4661− let _ = paste_clipboard_impl(cx.editor, Paste::Before, ClipboardType::Clipboard);
4668+ let _ = paste_clipboard_impl(
4669+ cx.editor,
4670+ Paste::Before,
4671+ ClipboardType::Clipboard,
4672+ cx.count(),
4673+ );
46624674}
46634675
46644676fn paste_primary_clipboard_after(cx: &mut Context) {
4665− let _ = paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection);
4677+ let _ = paste_clipboard_impl(
4678+ cx.editor,
4679+ Paste::After,
4680+ ClipboardType::Selection,
4681+ cx.count(),
4682+ );
46664683}
46674684
46684685fn paste_primary_clipboard_before(cx: &mut Context) {
4669− let _ = paste_clipboard_impl(cx.editor, Paste::Before, ClipboardType::Selection);
4686+ let _ = paste_clipboard_impl(
4687+ cx.editor,
4688+ Paste::Before,
4689+ ClipboardType::Selection,
4690+ cx.count(),
4691+ );
46704692}
46714693
46724694fn replace_with_yanked(cx: &mut Context) {
4695+ let count = cx.count();
46734696 let reg_name = cx.register.unwrap_or('"');
46744697 let (view, doc) = current!(cx.editor);
46754698 let registers = &mut cx.editor.registers;
@@ -4679,12 +4702,12 @@ fn replace_with_yanked(cx: &mut Context) {
46794702 let repeat = std::iter::repeat(
46804703 values
46814704 .last()
4682− .map(|value| Tendril::from_slice(value))
4705+ .map(|value| Tendril::from_slice(&value.repeat(count)))
46834706 .unwrap(),
46844707 );
46854708 let mut values = values
46864709 .iter()
4687− .map(|value| Tendril::from_slice(value))
4710+ .map(|value| Tendril::from_slice(&value.repeat(count)))
46884711 .chain(repeat);
46894712 let selection = doc.selection(view.id);
46904713 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
@@ -4704,6 +4727,7 @@ fn replace_with_yanked(cx: &mut Context) {
47044727fn replace_selections_with_clipboard_impl(
47054728 editor: &mut Editor,
47064729 clipboard_type: ClipboardType,
4730+ count: usize,
47074731) -> anyhow::Result<()> {
47084732 let (view, doc) = current!(editor);
47094733
@@ -4711,7 +4735,11 @@ fn replace_selections_with_clipboard_impl(
47114735 Ok(contents) => {
47124736 let selection = doc.selection(view.id);
47134737 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
4714− (range.from(), range.to(), Some(contents.as_str().into()))
4738+ (
4739+ range.from(),
4740+ range.to(),
4741+ Some(contents.repeat(count).as_str().into()),
4742+ )
47154743 });
47164744
47174745 doc.apply(&transaction, view.id);
@@ -4723,21 +4751,22 @@ fn replace_selections_with_clipboard_impl(
47234751}
47244752
47254753fn replace_selections_with_clipboard(cx: &mut Context) {
4726− let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Clipboard);
4754+ let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Clipboard, cx.count());
47274755}
47284756
47294757fn replace_selections_with_primary_clipboard(cx: &mut Context) {
4730− let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Selection);
4758+ let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Selection, cx.count());
47314759}
47324760
47334761fn paste_after(cx: &mut Context) {
4762+ let count = cx.count();
47344763 let reg_name = cx.register.unwrap_or('"');
47354764 let (view, doc) = current!(cx.editor);
47364765 let registers = &mut cx.editor.registers;
47374766
47384767 if let Some(transaction) = registers
47394768 .read(reg_name)
4740− .and_then(|values| paste_impl(values, doc, view, Paste::After))
4769+ .and_then(|values| paste_impl(values, doc, view, Paste::After, count))
47414770 {
47424771 doc.apply(&transaction, view.id);
47434772 doc.append_changes_to_history(view.id);
@@ -4745,13 +4774,14 @@ fn paste_after(cx: &mut Context) {
47454774}
47464775
47474776fn paste_before(cx: &mut Context) {
4777+ let count = cx.count();
47484778 let reg_name = cx.register.unwrap_or('"');
47494779 let (view, doc) = current!(cx.editor);
47504780 let registers = &mut cx.editor.registers;
47514781
47524782 if let Some(transaction) = registers
47534783 .read(reg_name)
4754− .and_then(|values| paste_impl(values, doc, view, Paste::Before))
4784+ .and_then(|values| paste_impl(values, doc, view, Paste::Before, count))
47554785 {
47564786 doc.apply(&transaction, view.id);
47574787 doc.append_changes_to_history(view.id);