Skip to content

Commit 885b8ff

Browse files
committed
Add insert-pipe and append-pipe commands.
Both commands behave similary to `insert-output` and `append-output` commands, except that they also pipe the selections into the command. This PR is inspired by the now closed PR #12590.
1 parent eb49c5e commit 885b8ff

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

helix-term/src/commands.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ impl MappableCommand {
600600
dap_disable_exceptions, "Disable exception breakpoints",
601601
shell_pipe, "Pipe selections through shell command",
602602
shell_pipe_to, "Pipe selections into shell command ignoring output",
603+
shell_pipe_insert, "Insert pipe command output before selections",
604+
shell_pipe_append, "Append pipe command output after selections",
603605
shell_insert_output, "Insert shell command output before selections",
604606
shell_append_output, "Append shell command output after selections",
605607
shell_keep_pipe, "Filter selections with shell predicate",
@@ -6436,6 +6438,8 @@ enum ShellBehavior {
64366438
Ignore,
64376439
Insert,
64386440
Append,
6441+
InsertPipe,
6442+
AppendPipe,
64396443
}
64406444

64416445
fn shell_pipe(cx: &mut Context) {
@@ -6446,6 +6450,14 @@ fn shell_pipe_to(cx: &mut Context) {
64466450
shell_prompt_for_behavior(cx, "pipe-to:".into(), ShellBehavior::Ignore);
64476451
}
64486452

6453+
fn shell_pipe_insert(cx: &mut Context) {
6454+
shell_prompt_for_behavior(cx, "insert-pipe:".into(), ShellBehavior::InsertPipe);
6455+
}
6456+
6457+
fn shell_pipe_append(cx: &mut Context) {
6458+
shell_prompt_for_behavior(cx, "append-pipe:".into(), ShellBehavior::AppendPipe);
6459+
}
6460+
64496461
fn shell_insert_output(cx: &mut Context) {
64506462
shell_prompt_for_behavior(cx, "insert-output:".into(), ShellBehavior::Insert);
64516463
}
@@ -6560,7 +6572,7 @@ async fn shell_impl_async(
65606572

65616573
fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
65626574
let pipe = match behavior {
6563-
ShellBehavior::Replace | ShellBehavior::Ignore => true,
6575+
ShellBehavior::Replace | ShellBehavior::Ignore | ShellBehavior::InsertPipe | ShellBehavior::AppendPipe => true,
65646576
ShellBehavior::Insert | ShellBehavior::Append => false,
65656577
};
65666578

@@ -6605,8 +6617,8 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
66056617

66066618
let (from, to, deleted_len) = match behavior {
66076619
ShellBehavior::Replace => (range.from(), range.to(), range.len()),
6608-
ShellBehavior::Insert => (range.from(), range.from(), 0),
6609-
ShellBehavior::Append => (range.to(), range.to(), 0),
6620+
ShellBehavior::Insert | ShellBehavior::InsertPipe => (range.from(), range.from(), 0),
6621+
ShellBehavior::Append | ShellBehavior::AppendPipe => (range.to(), range.to(), 0),
66106622
_ => (range.from(), range.from(), 0),
66116623
};
66126624

helix-term/src/commands/typed.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,14 @@ fn insert_output(
25242524
Ok(())
25252525
}
25262526

2527+
fn append_pipe(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
2528+
pipe_impl(cx, args, event, &ShellBehavior::AppendPipe)
2529+
}
2530+
2531+
fn insert_pipe(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
2532+
pipe_impl(cx, args, event, &ShellBehavior::InsertPipe)
2533+
}
2534+
25272535
fn pipe_to(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
25282536
pipe_impl(cx, args, event, &ShellBehavior::Ignore)
25292537
}
@@ -3857,6 +3865,22 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
38573865
completer: SHELL_COMPLETER,
38583866
signature: SHELL_SIGNATURE,
38593867
},
3868+
TypableCommand {
3869+
name: "insert-pipe",
3870+
aliases: &[],
3871+
doc: "Pipe each selection to the shell command, insert output before selction.",
3872+
fun: insert_pipe,
3873+
completer: SHELL_COMPLETER,
3874+
signature: SHELL_SIGNATURE,
3875+
},
3876+
TypableCommand {
3877+
name: "append-pipe",
3878+
aliases: &[],
3879+
doc: "Pipe each selection to the shell command, append output after selction.",
3880+
fun: append_pipe,
3881+
completer: SHELL_COMPLETER,
3882+
signature: SHELL_SIGNATURE,
3883+
},
38603884
TypableCommand {
38613885
name: "run-shell-command",
38623886
aliases: &["sh", "!"],

helix-term/tests/test/commands.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,38 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
271271
))
272272
.await?;
273273

274+
// insert-pipe
275+
test((
276+
indoc! {"\
277+
#[|lorem]#
278+
#(|ipsum)#
279+
#(|dolor)#
280+
"},
281+
":insert-pipe cat<ret>",
282+
indoc! {"\
283+
#[|lorem]#lorem
284+
#(|ipsum)#ipsum
285+
#(|dolor)#dolor
286+
"},
287+
))
288+
.await?;
289+
290+
// append-pipe
291+
test((
292+
indoc! {"\
293+
#[|lorem]#
294+
#(|ipsum)#
295+
#(|dolor)#
296+
"},
297+
":insert-pipe cat<ret>",
298+
indoc! {"\
299+
lorem#[|lorem]#
300+
ipsum#(|ipsum)#
301+
dolor#(|dolor)#
302+
"},
303+
))
304+
.await?;
305+
274306
Ok(())
275307
}
276308

0 commit comments

Comments
 (0)