Make shell_impl concurrent (#3180)

pull/4518/head
Matthias Deiml 2 years ago committed by GitHub
parent c58e1729ce
commit 5e256e4a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,7 +8,7 @@ use tui::text::Spans;
pub use typed::*; pub use typed::*;
use helix_core::{ use helix_core::{
comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, comment, coords_at_pos, encoding, find_first_non_whitespace_char, find_root, graphemes,
history::UndoKind, history::UndoKind,
increment::date_time::DateTimeIncrementor, increment::date_time::DateTimeIncrementor,
increment::{number::NumberIncrementor, Increment}, increment::{number::NumberIncrementor, Increment},
@ -4630,7 +4630,7 @@ fn shell_keep_pipe(cx: &mut Context) {
for (i, range) in selection.ranges().iter().enumerate() { for (i, range) in selection.ranges().iter().enumerate() {
let fragment = range.slice(text); let fragment = range.slice(text);
let (_output, success) = match shell_impl(shell, input, Some(fragment)) { let (_output, success) = match shell_impl(shell, input, Some(fragment.into())) {
Ok(result) => result, Ok(result) => result,
Err(err) => { Err(err) => {
cx.editor.set_error(err.to_string()); cx.editor.set_error(err.to_string());
@ -4658,13 +4658,17 @@ fn shell_keep_pipe(cx: &mut Context) {
); );
} }
fn shell_impl( fn shell_impl(shell: &[String], cmd: &str, input: Option<Rope>) -> anyhow::Result<(Tendril, bool)> {
tokio::task::block_in_place(|| helix_lsp::block_on(shell_impl_async(shell, cmd, input)))
}
async fn shell_impl_async(
shell: &[String], shell: &[String],
cmd: &str, cmd: &str,
input: Option<RopeSlice>, input: Option<Rope>,
) -> anyhow::Result<(Tendril, bool)> { ) -> anyhow::Result<(Tendril, bool)> {
use std::io::Write; use std::process::Stdio;
use std::process::{Command, Stdio}; use tokio::process::Command;
ensure!(!shell.is_empty(), "No shell set"); ensure!(!shell.is_empty(), "No shell set");
let mut process = Command::new(&shell[0]); let mut process = Command::new(&shell[0]);
@ -4687,13 +4691,22 @@ fn shell_impl(
return Err(e.into()); return Err(e.into());
} }
}; };
if let Some(input) = input { let output = if let Some(mut stdin) = process.stdin.take() {
let mut stdin = process.stdin.take().unwrap(); let input_task = tokio::spawn(async move {
for chunk in input.chunks() { if let Some(input) = input {
stdin.write_all(chunk.as_bytes())?; helix_view::document::to_writer(&mut stdin, encoding::UTF_8, &input).await?;
} }
} Ok::<_, anyhow::Error>(())
let output = process.wait_with_output()?; });
let (output, _) = tokio::join! {
process.wait_with_output(),
input_task,
};
output?
} else {
// Process has no stdin, so we just take the output
process.wait_with_output().await?
};
if !output.status.success() { if !output.status.success() {
if !output.stderr.is_empty() { if !output.stderr.is_empty() {
@ -4731,7 +4744,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
for range in selection.ranges() { for range in selection.ranges() {
let fragment = range.slice(text); let fragment = range.slice(text);
let (output, success) = match shell_impl(shell, cmd, pipe.then(|| fragment)) { let (output, success) = match shell_impl(shell, cmd, pipe.then(|| fragment.into())) {
Ok(result) => result, Ok(result) => result,
Err(err) => { Err(err) => {
cx.editor.set_error(err.to_string()); cx.editor.set_error(err.to_string());

Loading…
Cancel
Save