on master help sign in
Add yank_joined command (#7195)

Resolves issue #6888 by adding a command to join all selections and yank them to the specified register. The typed command takes an argument as the separator to use when joining the selections.

commitd8b7232a475a7e79d86458096954b354fc4faeca authorspectre256 <72505298+spectre256@users.noreply.github.com> dateFri 16 Jun 2023, 15:13 (3 years ago) parent3fb9faf
3 files changed+60
book/src/generated/typable-cmd.md+1 helix-term/src/commands.rs+33 helix-term/src/commands/typed.rs+26
book/src/generated/typable-cmd.md+1
@@ -31,6 +31,7 @@
3131| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
3232| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
3333| `:theme` | Change the editor theme (show current theme if no name specified). |
34+| `:yank-join` | Yank joined selections. A separator can be provided as first argument. Default value is newline. |
3435| `:clipboard-yank` | Yank main selection into system clipboard. |
3536| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
3637| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
helix-term/src/commands.rs+33
@@ -376,6 +376,7 @@ impl MappableCommand {
376376 later, "Move forward in history",
377377 commit_undo_checkpoint, "Commit changes to new checkpoint",
378378 yank, "Yank selection",
379+ yank_joined, "Join and yank selections",
379380 yank_joined_to_clipboard, "Join and yank selections to clipboard",
380381 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
381382 yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
@@ -3724,6 +3725,38 @@ fn yank(cx: &mut Context) {
37243725 exit_select_mode(cx);
37253726}
37263727
3728+fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
3729+ let (view, doc) = current!(editor);
3730+ let text = doc.text().slice(..);
3731+
3732+ let selection = doc.selection(view.id);
3733+ let joined = selection
3734+ .fragments(text)
3735+ .fold(String::new(), |mut acc, fragment| {
3736+ if !acc.is_empty() {
3737+ acc.push_str(separator);
3738+ }
3739+ acc.push_str(&fragment);
3740+ acc
3741+ });
3742+
3743+ let msg = format!(
3744+ "joined and yanked {} selection(s) to register {}",
3745+ selection.len(),
3746+ register,
3747+ );
3748+
3749+ editor.registers.write(register, vec![joined]);
3750+ editor.set_status(msg);
3751+}
3752+
3753+fn yank_joined(cx: &mut Context) {
3754+ let line_ending = doc!(cx.editor).line_ending;
3755+ let register = cx.register.unwrap_or('"');
3756+ yank_joined_impl(cx.editor, line_ending.as_str(), register);
3757+ exit_select_mode(cx);
3758+}
3759+
37273760fn yank_joined_to_clipboard_impl(
37283761 editor: &mut Editor,
37293762 separator: &str,
helix-term/src/commands/typed.rs+26
@@ -895,6 +895,25 @@ fn yank_main_selection_to_clipboard(
895895 yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard)
896896}
897897
898+fn yank_joined(
899+ cx: &mut compositor::Context,
900+ args: &[Cow<str>],
901+ event: PromptEvent,
902+) -> anyhow::Result<()> {
903+ if event != PromptEvent::Validate {
904+ return Ok(());
905+ }
906+
907+ ensure!(args.len() <= 1, ":yank-join takes at most 1 argument");
908+
909+ let doc = doc!(cx.editor);
910+ let default_sep = Cow::Borrowed(doc.line_ending.as_str());
911+ let separator = args.first().unwrap_or(&default_sep);
912+ let register = cx.editor.selected_register.unwrap_or('"');
913+ yank_joined_impl(cx.editor, separator, register);
914+ Ok(())
915+}
916+
898917fn yank_joined_to_clipboard(
899918 cx: &mut compositor::Context,
900919 args: &[Cow<str>],
@@ -2474,6 +2493,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24742493 fun: theme,
24752494 signature: CommandSignature::positional(&[completers::theme]),
24762495 },
2496+ TypableCommand {
2497+ name: "yank-join",
2498+ aliases: &[],
2499+ doc: "Yank joined selections. A separator can be provided as first argument. Default value is newline.",
2500+ fun: yank_joined,
2501+ signature: CommandSignature::none(),
2502+ },
24772503 TypableCommand {
24782504 name: "clipboard-yank",
24792505 aliases: &[],