Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow paste commands to take a count #1261

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 49 additions & 19 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2413,31 +2413,31 @@ pub mod cmd {
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
}

fn paste_clipboard_before(
cx: &mut compositor::Context,
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard)
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard, 1)
}

fn paste_primary_clipboard_after(
cx: &mut compositor::Context,
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
}

fn paste_primary_clipboard_before(
cx: &mut compositor::Context,
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection)
paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection, 1)
}

fn replace_selections_with_clipboard_impl(
Expand Down Expand Up @@ -4575,11 +4575,12 @@ fn paste_impl(
doc: &mut Document,
view: &View,
action: Paste,
count: usize,
) -> Option<Transaction> {
let repeat = std::iter::repeat(
values
.last()
.map(|value| Tendril::from_slice(value))
.map(|value| Tendril::from(value.repeat(count)))
.unwrap(),
);

Expand All @@ -4594,7 +4595,7 @@ fn paste_impl(
let mut values = values
.iter()
.map(|value| REGEX.replace_all(value, doc.line_ending.as_str()))
.map(|value| Tendril::from(value.as_ref()))
Omnikar marked this conversation as resolved.
Show resolved Hide resolved
.map(|value| Tendril::from(value.as_ref().repeat(count)))
.chain(repeat);

let text = doc.text();
Expand All @@ -4614,7 +4615,7 @@ fn paste_impl(
// paste append
(Paste::After, false) => range.to(),
};
(pos, pos, Some(values.next().unwrap()))
(pos, pos, values.next())
});

Some(transaction)
Expand All @@ -4624,13 +4625,14 @@ fn paste_clipboard_impl(
editor: &mut Editor,
action: Paste,
clipboard_type: ClipboardType,
count: usize,
) -> anyhow::Result<()> {
let (view, doc) = current!(editor);

match editor
.clipboard_provider
.get_contents(clipboard_type)
.map(|contents| paste_impl(&[contents], doc, view, action))
.map(|contents| paste_impl(&[contents], doc, view, action, count))
{
Ok(Some(transaction)) => {
doc.apply(&transaction, view.id);
Expand All @@ -4643,22 +4645,43 @@ fn paste_clipboard_impl(
}

fn paste_clipboard_after(cx: &mut Context) {
let _ = paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard);
let _ = paste_clipboard_impl(
cx.editor,
Paste::After,
ClipboardType::Clipboard,
cx.count(),
);
}

fn paste_clipboard_before(cx: &mut Context) {
let _ = paste_clipboard_impl(cx.editor, Paste::Before, ClipboardType::Clipboard);
let _ = paste_clipboard_impl(
cx.editor,
Paste::Before,
ClipboardType::Clipboard,
cx.count(),
);
}

fn paste_primary_clipboard_after(cx: &mut Context) {
let _ = paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection);
let _ = paste_clipboard_impl(
cx.editor,
Paste::After,
ClipboardType::Selection,
cx.count(),
);
}

fn paste_primary_clipboard_before(cx: &mut Context) {
let _ = paste_clipboard_impl(cx.editor, Paste::Before, ClipboardType::Selection);
let _ = paste_clipboard_impl(
cx.editor,
Paste::Before,
ClipboardType::Selection,
cx.count(),
);
}

fn replace_with_yanked(cx: &mut Context) {
let count = cx.count();
let reg_name = cx.register.unwrap_or('"');
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;
Expand All @@ -4668,12 +4691,12 @@ fn replace_with_yanked(cx: &mut Context) {
let repeat = std::iter::repeat(
values
.last()
.map(|value| Tendril::from_slice(value))
.map(|value| Tendril::from_slice(&value.repeat(count)))
.unwrap(),
);
let mut values = values
.iter()
.map(|value| Tendril::from_slice(value))
.map(|value| Tendril::from_slice(&value.repeat(count)))
.chain(repeat);
let selection = doc.selection(view.id);
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
Expand All @@ -4693,14 +4716,19 @@ fn replace_with_yanked(cx: &mut Context) {
fn replace_selections_with_clipboard_impl(
editor: &mut Editor,
clipboard_type: ClipboardType,
count: usize,
) -> anyhow::Result<()> {
let (view, doc) = current!(editor);

match editor.clipboard_provider.get_contents(clipboard_type) {
Ok(contents) => {
let selection = doc.selection(view.id);
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
(range.from(), range.to(), Some(contents.as_str().into()))
(
range.from(),
range.to(),
Some(contents.repeat(count).as_str().into()),
)
});

doc.apply(&transaction, view.id);
Expand All @@ -4712,35 +4740,37 @@ fn replace_selections_with_clipboard_impl(
}

fn replace_selections_with_clipboard(cx: &mut Context) {
let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Clipboard);
let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Clipboard, cx.count());
}

fn replace_selections_with_primary_clipboard(cx: &mut Context) {
let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Selection);
let _ = replace_selections_with_clipboard_impl(cx.editor, ClipboardType::Selection, cx.count());
}

fn paste_after(cx: &mut Context) {
let count = cx.count();
let reg_name = cx.register.unwrap_or('"');
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(transaction) = registers
.read(reg_name)
.and_then(|values| paste_impl(values, doc, view, Paste::After))
.and_then(|values| paste_impl(values, doc, view, Paste::After, count))
{
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
}

fn paste_before(cx: &mut Context) {
let count = cx.count();
let reg_name = cx.register.unwrap_or('"');
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(transaction) = registers
.read(reg_name)
.and_then(|values| paste_impl(values, doc, view, Paste::Before))
.and_then(|values| paste_impl(values, doc, view, Paste::Before, count))
{
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
Expand Down