From e8b4be7aef1af472031af3abf31d94eb4f99d3e0 Mon Sep 17 00:00:00 2001 From: So1aric Date: Thu, 5 Jun 2025 14:58:54 +0800 Subject: [PATCH 1/2] feat(term): add `shell_pipe_into_buffer` command. --- helix-term/src/commands.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 446337411..3cf893c88 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -587,6 +587,7 @@ impl MappableCommand { dap_disable_exceptions, "Disable exception breakpoints", shell_pipe, "Pipe selections through shell command", shell_pipe_to, "Pipe selections into shell command ignoring output", + shell_pipe_into_buffer, "Pipe selections into shell command and output in new buffer", shell_insert_output, "Insert shell command output before selections", shell_append_output, "Append shell command output after selections", shell_keep_pipe, "Filter selections with shell predicate", @@ -6148,6 +6149,7 @@ enum ShellBehavior { Ignore, Insert, Append, + OpenBuffer, } fn shell_pipe(cx: &mut Context) { @@ -6166,6 +6168,10 @@ fn shell_append_output(cx: &mut Context) { shell_prompt(cx, "append-output:".into(), ShellBehavior::Append); } +fn shell_pipe_into_buffer(cx: &mut Context) { + shell_prompt(cx, "open-buffer:".into(), ShellBehavior::OpenBuffer); +} + fn shell_keep_pipe(cx: &mut Context) { ui::prompt( cx, @@ -6285,7 +6291,7 @@ 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::Insert | ShellBehavior::Append => false, + ShellBehavior::Insert | ShellBehavior::Append | ShellBehavior::OpenBuffer => false, }; let config = cx.editor.config(); @@ -6331,6 +6337,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { ShellBehavior::Replace => (range.from(), range.to(), range.len()), ShellBehavior::Insert => (range.from(), range.from(), 0), ShellBehavior::Append => (range.to(), range.to(), 0), + ShellBehavior::OpenBuffer => (0, 0, 0), _ => (range.from(), range.from(), 0), }; @@ -6351,16 +6358,23 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { changes.push((from, to, Some(output))); } - if behavior != &ShellBehavior::Ignore { + if behavior != &ShellBehavior::Ignore && behavior != &ShellBehavior::OpenBuffer { let transaction = Transaction::change(doc.text(), changes.into_iter()) .with_selection(Selection::new(ranges, selection.primary_index())); doc.apply(&transaction, view.id); doc.append_changes_to_history(view); - } - // after replace cursor may be out of bounds, do this to - // make sure cursor is in view and update scroll as well - view.ensure_cursor_in_view(doc, config.scrolloff); + // after replace cursor may be out of bounds, do this to + // make sure cursor is in view and update scroll as well + view.ensure_cursor_in_view(doc, config.scrolloff); + } else if behavior == &ShellBehavior::OpenBuffer { + cx.editor.new_file(Action::Replace); + let (view, doc) = current!(cx.editor); + + let transaction = Transaction::change(doc.text(), changes.into_iter()); + doc.apply(&transaction, view.id); + doc.append_changes_to_history(view); + } } fn shell_prompt(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { From 962a27cab7e864e5d7b9423bcda3933c9ae7d92f Mon Sep 17 00:00:00 2001 From: So1aric Date: Thu, 5 Jun 2025 15:17:46 +0800 Subject: [PATCH 2/2] fix(term): fix pipe and prompt --- helix-term/src/commands.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3cf893c88..97cb9dd7c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -6169,7 +6169,7 @@ fn shell_append_output(cx: &mut Context) { } fn shell_pipe_into_buffer(cx: &mut Context) { - shell_prompt(cx, "open-buffer:".into(), ShellBehavior::OpenBuffer); + shell_prompt(cx, "pipe-into-buffer:".into(), ShellBehavior::OpenBuffer); } fn shell_keep_pipe(cx: &mut Context) { @@ -6290,8 +6290,8 @@ 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::Insert | ShellBehavior::Append | ShellBehavior::OpenBuffer => false, + ShellBehavior::Replace | ShellBehavior::Ignore | ShellBehavior::OpenBuffer => true, + ShellBehavior::Insert | ShellBehavior::Append => false, }; let config = cx.editor.config();