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

Add yank_joined command #7195

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
| `:theme` | Change the editor theme (show current theme if no name specified). |
| `:yank-join` | Yank joined selections. A separator can be provided as first argument. Default value is newline. |
| `:clipboard-yank` | Yank main selection into system clipboard. |
| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
Expand Down
33 changes: 33 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl MappableCommand {
later, "Move forward in history",
commit_undo_checkpoint, "Commit changes to new checkpoint",
yank, "Yank selection",
yank_joined, "Join and yank selections",
yank_joined_to_clipboard, "Join and yank selections to clipboard",
yank_main_selection_to_clipboard, "Yank main selection to clipboard",
yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
Expand Down Expand Up @@ -3647,6 +3648,38 @@ fn yank(cx: &mut Context) {
exit_select_mode(cx);
}

fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
let (view, doc) = current!(editor);
let text = doc.text().slice(..);

let selection = doc.selection(view.id);
let joined = selection
.fragments(text)
.fold(String::new(), |mut acc, fragment| {
if !acc.is_empty() {
acc.push_str(separator);
}
acc.push_str(&fragment);
acc
});

let msg = format!(
"joined and yanked {} selection(s) to register {}",
selection.len(),
register,
);

editor.registers.write(register, vec![joined]);
editor.set_status(msg);
}

fn yank_joined(cx: &mut Context) {
let line_ending = doc!(cx.editor).line_ending;
let register = cx.register.unwrap_or('"');
yank_joined_impl(cx.editor, line_ending.as_str(), register);
exit_select_mode(cx);
}

fn yank_joined_to_clipboard_impl(
editor: &mut Editor,
separator: &str,
Expand Down
26 changes: 26 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,25 @@ fn yank_main_selection_to_clipboard(
yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard)
}

fn yank_joined(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we do this consistently in typable commands but we can give some feedback when the command gets more arguments than expected:

ensure!(args.len() <= 1, ":yank-join takes at most 1 argument");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated it! Would it be helpful to make a pr to add this to the other commands as necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it would be good to have some feedback especially on the new/save file operations like new, write, format, etc 👍. Discarding arguments silently seems potentially confusing to me

ensure!(args.len() <= 1, ":yank-join takes at most 1 argument");

let doc = doc!(cx.editor);
let default_sep = Cow::Borrowed(doc.line_ending.as_str());
let separator = args.first().unwrap_or(&default_sep);
let register = cx.editor.selected_register.unwrap_or('"');
yank_joined_impl(cx.editor, separator, register);
Ok(())
}

fn yank_joined_to_clipboard(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -2453,6 +2472,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: theme,
signature: CommandSignature::positional(&[completers::theme]),
},
TypableCommand {
name: "yank-join",
aliases: &[],
doc: "Yank joined selections. A separator can be provided as first argument. Default value is newline.",
fun: yank_joined,
signature: CommandSignature::none(),
},
TypableCommand {
name: "clipboard-yank",
aliases: &[],
Expand Down