on master help sign in
Allow paste commands to take a count (#1261)
6da2174Omnikar
helix-term/src/commands.rs+49−19
@@ -2423,7 +2423,7 @@ pub mod cmd {
2423 _args: &[Cow<str>],
2423 _args: &[Cow<str>],
2424 _event: PromptEvent,
2424 _event: PromptEvent,
2425 ) -> anyhow::Result<()> {
2425 ) -> anyhow::Result<()> {
2426− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
2426+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
2427 }
2427 }
2428
2428
2429 fn paste_clipboard_before(
2429 fn paste_clipboard_before(
@@ -2431,7 +2431,7 @@ pub mod cmd {
2431 _args: &[Cow<str>],
2431 _args: &[Cow<str>],
2432 _event: PromptEvent,
2432 _event: PromptEvent,
2433 ) -> anyhow::Result<()> {
2433 ) -> anyhow::Result<()> {
2434− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
2434+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
2435 }
2435 }
2436
2436
2437 fn paste_primary_clipboard_after(
2437 fn paste_primary_clipboard_after(
@@ -2439,7 +2439,7 @@ pub mod cmd {
2439 _args: &[Cow<str>],
2439 _args: &[Cow<str>],
2440 _event: PromptEvent,
2440 _event: PromptEvent,
2441 ) -> anyhow::Result<()> {
2441 ) -> anyhow::Result<()> {
2442− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
2442+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
2443 }
2443 }
2444
2444
2445 fn paste_primary_clipboard_before(
2445 fn paste_primary_clipboard_before(
@@ -2447,7 +2447,7 @@ pub mod cmd {
2447 _args: &[Cow<str>],
2447 _args: &[Cow<str>],
2448 _event: PromptEvent,
2448 _event: PromptEvent,
2449 ) -> anyhow::Result<()> {
2449 ) -> anyhow::Result<()> {
2450− paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
2450+ paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
2451 }
2451 }
2452
2452
2453 fn replace_selections_with_clipboard_impl(
2453 fn replace_selections_with_clipboard_impl(
@@ -4586,11 +4586,12 @@ fn paste_impl(
4586 doc: &mut Document,
4586 doc: &mut Document,
4587 view: &View,
4587 view: &View,
4588 action: Paste,
4588 action: Paste,
4589+ count: usize,
4589) -> Option<Transaction> {
4590) -> Option<Transaction> {
4590 let repeat = std::iter::repeat(
4591 let repeat = std::iter::repeat(
4591 values
4592 values
4592 .last()
4593 .last()
4593− .map(|value| Tendril::from_slice(value))
4594+ .map(|value| Tendril::from(value.repeat(count)))
4594 .unwrap(),
4595 .unwrap(),
4595 );
4596 );
4596
4597
@@ -4605,7 +4606,7 @@ fn paste_impl(
4605 let mut values = values
4606 let mut values = values
4606 .iter()
4607 .iter()
4607 .map(|value| REGEX.replace_all(value, doc.line_ending.as_str()))
4608 .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)))
4609 .chain(repeat);
4610 .chain(repeat);
4610
4611
4611 let text = doc.text();
4612 let text = doc.text();
@@ -4625,7 +4626,7 @@ fn paste_impl(
4625 // paste append
4626 // paste append
4626 (Paste::After, false) => range.to(),
4627 (Paste::After, false) => range.to(),
4627 };
4628 };
4628− (pos, pos, Some(values.next().unwrap()))
4629+ (pos, pos, values.next())
4629 });
4630 });
4630
4631
4631 Some(transaction)
4632 Some(transaction)
@@ -4635,13 +4636,14 @@ fn paste_clipboard_impl(
4635 editor: &mut Editor,
4636 editor: &mut Editor,
4636 action: Paste,
4637 action: Paste,
4637 clipboard_type: ClipboardType,
4638 clipboard_type: ClipboardType,
4639+ count: usize,
4638) -> anyhow::Result<()> {
4640) -> anyhow::Result<()> {
4639 let (view, doc) = current!(editor);
4641 let (view, doc) = current!(editor);
4640
4642
4641 match editor
4643 match editor
4642 .clipboard_provider
4644 .clipboard_provider
4643 .get_contents(clipboard_type)
4645 .get_contents(clipboard_type)
4644− .map(|contents| paste_impl(&[contents], doc, view, action))
4646+ .map(|contents| paste_impl(&[contents], doc, view, action, count))
4645 {
4647 {
4646 Ok(Some(transaction)) => {
4648 Ok(Some(transaction)) => {
4647 doc.apply(&transaction, view.id);
4649 doc.apply(&transaction, view.id);
@@ -4654,22 +4656,43 @@ fn paste_clipboard_impl(
4654}
4656}
4655
4657
4656fn paste_clipboard_after(cx: &mut Context) {
4658fn 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+ );
4658}
4665}
4659
4666
4660fn paste_clipboard_before(cx: &mut Context) {
4667fn 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+ );
4662}
4674}
4663
4675
4664fn paste_primary_clipboard_after(cx: &mut Context) {
4676fn 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+ );
4666}
4683}
4667
4684
4668fn paste_primary_clipboard_before(cx: &mut Context) {
4685fn 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+ );
4670}
4692}
4671
4693
4672fn replace_with_yanked(cx: &mut Context) {
4694fn replace_with_yanked(cx: &mut Context) {
4695+ let count = cx.count();
4673 let reg_name = cx.register.unwrap_or('"');
4696 let reg_name = cx.register.unwrap_or('"');
4674 let (view, doc) = current!(cx.editor);
4697 let (view, doc) = current!(cx.editor);
4675 let registers = &mut cx.editor.registers;
4698 let registers = &mut cx.editor.registers;
@@ -4679,12 +4702,12 @@ fn replace_with_yanked(cx: &mut Context) {
4679 let repeat = std::iter::repeat(
4702 let repeat = std::iter::repeat(
4680 values
4703 values
4681 .last()
4704 .last()
4682− .map(|value| Tendril::from_slice(value))
4705+ .map(|value| Tendril::from_slice(&value.repeat(count)))
4683 .unwrap(),
4706 .unwrap(),
4684 );
4707 );
4685 let mut values = values
4708 let mut values = values
4686 .iter()
4709 .iter()
4687− .map(|value| Tendril::from_slice(value))
4710+ .map(|value| Tendril::from_slice(&value.repeat(count)))
4688 .chain(repeat);
4711 .chain(repeat);
4689 let selection = doc.selection(view.id);
4712 let selection = doc.selection(view.id);
4690 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
4713 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
@@ -4704,6 +4727,7 @@ fn replace_with_yanked(cx: &mut Context) {
4704fn replace_selections_with_clipboard_impl(
4727fn replace_selections_with_clipboard_impl(
4705 editor: &mut Editor,
4728 editor: &mut Editor,
4706 clipboard_type: ClipboardType,
4729 clipboard_type: ClipboardType,
4730+ count: usize,
4707) -> anyhow::Result<()> {
4731) -> anyhow::Result<()> {
4708 let (view, doc) = current!(editor);
4732 let (view, doc) = current!(editor);
4709
4733
@@ -4711,7 +4735,11 @@ fn replace_selections_with_clipboard_impl(
4711 Ok(contents) => {
4735 Ok(contents) => {
4712 let selection = doc.selection(view.id);
4736 let selection = doc.selection(view.id);
4713 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
4737 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
4738+ (
4739+ range.from(),
4740+ range.to(),
4714− (range.from(), range.to(), Some(contents.as_str().into()))
4741+ Some(contents.repeat(count).as_str().into()),
4742+ )
4715 });
4743 });
4716
4744
4717 doc.apply(&transaction, view.id);
4745 doc.apply(&transaction, view.id);
@@ -4723,21 +4751,22 @@ fn replace_selections_with_clipboard_impl(
4723}
4751}
4724
4752
4725fn replace_selections_with_clipboard(cx: &mut Context) {
4753fn 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());
4727}
4755}
4728
4756
4729fn replace_selections_with_primary_clipboard(cx: &mut Context) {
4757fn 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());
4731}
4759}
4732
4760
4733fn paste_after(cx: &mut Context) {
4761fn paste_after(cx: &mut Context) {
4762+ let count = cx.count();
4734 let reg_name = cx.register.unwrap_or('"');
4763 let reg_name = cx.register.unwrap_or('"');
4735 let (view, doc) = current!(cx.editor);
4764 let (view, doc) = current!(cx.editor);
4736 let registers = &mut cx.editor.registers;
4765 let registers = &mut cx.editor.registers;
4737
4766
4738 if let Some(transaction) = registers
4767 if let Some(transaction) = registers
4739 .read(reg_name)
4768 .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))
4741 {
4770 {
4742 doc.apply(&transaction, view.id);
4771 doc.apply(&transaction, view.id);
4743 doc.append_changes_to_history(view.id);
4772 doc.append_changes_to_history(view.id);
@@ -4745,13 +4774,14 @@ fn paste_after(cx: &mut Context) {
4745}
4774}
4746
4775
4747fn paste_before(cx: &mut Context) {
4776fn paste_before(cx: &mut Context) {
4777+ let count = cx.count();
4748 let reg_name = cx.register.unwrap_or('"');
4778 let reg_name = cx.register.unwrap_or('"');
4749 let (view, doc) = current!(cx.editor);
4779 let (view, doc) = current!(cx.editor);
4750 let registers = &mut cx.editor.registers;
4780 let registers = &mut cx.editor.registers;
4751
4781
4752 if let Some(transaction) = registers
4782 if let Some(transaction) = registers
4753 .read(reg_name)
4783 .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))
4755 {
4785 {
4756 doc.apply(&transaction, view.id);
4786 doc.apply(&transaction, view.id);
4757 doc.append_changes_to_history(view.id);
4787 doc.append_changes_to_history(view.id);