Support formatting unsaved buffers with external formatter (#12597)

Closes #4529


https://github.com/zed-industries/zed/assets/53836821/b84efd5e-89da-4ff7-9a29-2b2f7285d427

Release Notes:

- Added ability to format unsaved buffers with external formatters
([#4529](https://github.com/zed-industries/zed/issues/4529))
This commit is contained in:
Bennet Bo Fenner 2024-06-03 16:32:16 +02:00 committed by GitHub
parent 338df5de1d
commit d0fa012bf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4989,7 +4989,7 @@ impl Project {
FormatOnSave::On | FormatOnSave::Off,
)
| (_, FormatOnSave::External { command, arguments }) => {
if let Some(buffer_abs_path) = buffer_abs_path {
let buffer_abs_path = buffer_abs_path.as_ref().map(|path| path.as_path());
format_operation = Self::format_via_external_command(
buffer,
buffer_abs_path,
@ -5004,7 +5004,6 @@ impl Project {
))?
.map(FormatOperation::External);
}
}
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
let prettier = if prettier_settings.allowed {
prettier_support::format_with_prettier(&project, buffer, &mut cx)
@ -5142,7 +5141,7 @@ impl Project {
async fn format_via_external_command(
buffer: &Model<Buffer>,
buffer_abs_path: &Path,
buffer_abs_path: Option<&Path>,
command: &str,
arguments: &[String],
cx: &mut AsyncAppContext,
@ -5157,17 +5156,25 @@ impl Project {
Some(worktree_path)
})?;
let mut child = smol::process::Command::new(command);
if let Some(working_dir_path) = working_dir_path {
let mut child =
smol::process::Command::new(command)
child.current_dir(working_dir_path);
}
let mut child = child
.args(arguments.iter().map(|arg| {
if let Some(buffer_abs_path) = buffer_abs_path {
arg.replace("{buffer_path}", &buffer_abs_path.to_string_lossy())
} else {
arg.replace("{buffer_path}", "Untitled")
}
}))
.current_dir(&working_dir_path)
.stdin(smol::process::Stdio::piped())
.stdout(smol::process::Stdio::piped())
.stderr(smol::process::Stdio::piped())
.spawn()?;
let stdin = child
.stdin
.as_mut()
@ -5194,9 +5201,6 @@ impl Project {
.update(cx, |buffer, cx| buffer.diff(stdout, cx))?
.await,
))
} else {
Ok(None)
}
}
#[inline(never)]