From 9ee146567fb7805200e19ff3b3ee182a9e11da27 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Wed, 17 Aug 2022 14:02:09 +0800 Subject: [PATCH] remove duplicated shell calls --- helix-term/src/commands.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ae50ed7c5ebd..d09c447c2621 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4604,13 +4604,22 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { let mut changes = Vec::with_capacity(selection.len()); let text = doc.text().slice(..); + let mut shell_output: Option = None; for range in selection.ranges() { - let fragment = range.slice(text); - let (output, success) = match shell_impl(shell, cmd, pipe.then(|| fragment)) { - Ok(result) => result, - Err(err) => { - cx.editor.set_error(err.to_string()); - return; + let (output, success) = if let Some(output) = shell_output.as_ref() { + (output.clone(), true) + } else { + match shell_impl(shell, cmd, pipe.then(|| range.slice(text))) { + Ok(result) => { + if !pipe { + shell_output = Some(result.0.clone()); + } + result + } + Err(err) => { + cx.editor.set_error(err.to_string()); + return; + } } };