Skip to content
Open
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
21 changes: 18 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ impl MappableCommand {
shell_pipe, "Pipe selections through shell command",
shell_pipe_to, "Pipe selections into shell command ignoring output",
shell_insert_output, "Insert shell command output before selections",
shell_insert_pipe, "Insert pipe command output before selections",
shell_append_output, "Append shell command output after selections",
shell_append_pipe, "Append pipe command output after selections",
shell_keep_pipe, "Filter selections with shell predicate",
suspend, "Suspend and return to shell",
rename_symbol, "Rename symbol",
Expand Down Expand Up @@ -6435,7 +6437,9 @@ enum ShellBehavior {
Replace,
Ignore,
Insert,
InsertPipe,
Append,
AppendPipe,
}

fn shell_pipe(cx: &mut Context) {
Expand All @@ -6450,10 +6454,18 @@ fn shell_insert_output(cx: &mut Context) {
shell_prompt_for_behavior(cx, "insert-output:".into(), ShellBehavior::Insert);
}

fn shell_insert_pipe(cx: &mut Context) {
shell_prompt_for_behavior(cx, "insert-pipe:".into(), ShellBehavior::InsertPipe);
}

fn shell_append_output(cx: &mut Context) {
shell_prompt_for_behavior(cx, "append-output:".into(), ShellBehavior::Append);
}

fn shell_append_pipe(cx: &mut Context) {
shell_prompt_for_behavior(cx, "append-pipe:".into(), ShellBehavior::AppendPipe);
}

fn shell_keep_pipe(cx: &mut Context) {
shell_prompt(cx, "keep-pipe:".into(), |cx, args| {
let shell = &cx.editor.config().shell;
Expand Down Expand Up @@ -6560,7 +6572,10 @@ async fn shell_impl_async(

fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
let pipe = match behavior {
ShellBehavior::Replace | ShellBehavior::Ignore => true,
ShellBehavior::Replace
| ShellBehavior::Ignore
| ShellBehavior::InsertPipe
| ShellBehavior::AppendPipe => true,
ShellBehavior::Insert | ShellBehavior::Append => false,
};

Expand Down Expand Up @@ -6605,8 +6620,8 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {

let (from, to, deleted_len) = match behavior {
ShellBehavior::Replace => (range.from(), range.to(), range.len()),
ShellBehavior::Insert => (range.from(), range.from(), 0),
ShellBehavior::Append => (range.to(), range.to(), 0),
ShellBehavior::Insert | ShellBehavior::InsertPipe => (range.from(), range.from(), 0),
ShellBehavior::Append | ShellBehavior::AppendPipe => (range.to(), range.to(), 0),
_ => (range.from(), range.from(), 0),
};

Expand Down
24 changes: 24 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,14 @@ fn insert_output(
Ok(())
}

fn append_pipe(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
pipe_impl(cx, args, event, &ShellBehavior::AppendPipe)
}

fn insert_pipe(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
pipe_impl(cx, args, event, &ShellBehavior::InsertPipe)
}

fn pipe_to(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
pipe_impl(cx, args, event, &ShellBehavior::Ignore)
}
Expand Down Expand Up @@ -3857,6 +3865,22 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
completer: SHELL_COMPLETER,
signature: SHELL_SIGNATURE,
},
TypableCommand {
name: "insert-pipe",
aliases: &[],
doc: "Pipe each selection to the shell command, insert output before selection.",
fun: insert_pipe,
completer: SHELL_COMPLETER,
signature: SHELL_SIGNATURE,
},
TypableCommand {
name: "append-pipe",
aliases: &[],
doc: "Pipe each selection to the shell command, append output after selection.",
fun: append_pipe,
completer: SHELL_COMPLETER,
signature: SHELL_SIGNATURE,
},
TypableCommand {
name: "run-shell-command",
aliases: &["sh", "!"],
Expand Down
32 changes: 32 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,38 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
))
.await?;

// insert-pipe
test((
indoc! {"\
#[|lorem]#
#(|ipsum)#
#(|dolor)#
"},
":insert-pipe cat<ret>",
indoc! {"\
#[|lorem]#lorem
#(|ipsum)#ipsum
#(|dolor)#dolor
"},
))
.await?;

// append-pipe
test((
indoc! {"\
#[|lorem]#
#(|ipsum)#
#(|dolor)#
"},
":append-pipe cat<ret>",
indoc! {"\
lorem#[|lorem]#
ipsum#(|ipsum)#
dolor#(|dolor)#
"},
))
.await?;

Ok(())
}

Expand Down