Compare commits

...

2 Commits

Author SHA1 Message Date
Bennet Bo Fenner
dd36c1682f Allow unsaved buffers to be formatted when using an external formatter 2024-06-03 10:44:29 +02:00
Bennet Bo Fenner
47b70bb112 Disable indent guides for single line editors by default. 2024-06-02 17:12:35 +02:00
2 changed files with 65 additions and 55 deletions

View File

@@ -1697,6 +1697,12 @@ impl Editor {
cx.on_focus(&focus_handle, Self::handle_focus).detach();
cx.on_blur(&focus_handle, Self::handle_blur).detach();
let show_indent_guides = if mode == EditorMode::SingleLine {
Some(false)
} else {
None
};
let mut this = Self {
focus_handle,
buffer: buffer.clone(),
@@ -1726,7 +1732,7 @@ impl Editor {
show_git_diff_gutter: None,
show_code_actions: None,
show_wrap_guides: None,
show_indent_guides: None,
show_indent_guides,
placeholder_text: None,
highlight_order: 0,
highlighted_rows: HashMap::default(),

View File

@@ -4989,21 +4989,20 @@ impl Project {
FormatOnSave::On | FormatOnSave::Off,
)
| (_, FormatOnSave::External { command, arguments }) => {
if let Some(buffer_abs_path) = buffer_abs_path {
format_operation = Self::format_via_external_command(
buffer,
buffer_abs_path,
command,
arguments,
&mut cx,
)
.await
.context(format!(
"failed to format via external command {:?}",
command
))?
.map(FormatOperation::External);
}
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,
command,
arguments,
&mut cx,
)
.await
.context(format!(
"failed to format via external command {:?}",
command
))?
.map(FormatOperation::External);
}
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
let prettier = if prettier_settings.allowed {
@@ -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,46 +5156,51 @@ impl Project {
Some(worktree_path)
})?;
if let Some(working_dir_path) = working_dir_path {
let mut child =
smol::process::Command::new(command)
.args(arguments.iter().map(|arg| {
arg.replace("{buffer_path}", &buffer_abs_path.to_string_lossy())
}))
.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()
.ok_or_else(|| anyhow!("failed to acquire stdin"))?;
let text = buffer.update(cx, |buffer, _| buffer.as_rope().clone())?;
for chunk in text.chunks() {
stdin.write_all(chunk.as_bytes()).await?;
}
stdin.flush().await?;
let mut child = smol::process::Command::new(command);
let output = child.output().await?;
if !output.status.success() {
return Err(anyhow!(
"command failed with exit code {:?}:\nstdout: {}\nstderr: {}",
output.status.code(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
));
}
let stdout = String::from_utf8(output.stdout)?;
Ok(Some(
buffer
.update(cx, |buffer, cx| buffer.diff(stdout, cx))?
.await,
))
} else {
Ok(None)
if let Some(buffer_abs_path) = buffer_abs_path {
child.args(
arguments
.iter()
.map(|arg| arg.replace("{buffer_path}", &buffer_abs_path.to_string_lossy())),
);
}
if let Some(working_dir_path) = working_dir_path {
child.current_dir(working_dir_path);
}
let mut child = child
.stdin(smol::process::Stdio::piped())
.stdout(smol::process::Stdio::piped())
.stderr(smol::process::Stdio::piped())
.spawn()?;
let stdin = child
.stdin
.as_mut()
.ok_or_else(|| anyhow!("failed to acquire stdin"))?;
let text = buffer.update(cx, |buffer, _| buffer.as_rope().clone())?;
for chunk in text.chunks() {
stdin.write_all(chunk.as_bytes()).await?;
}
stdin.flush().await?;
let output = child.output().await?;
if !output.status.success() {
return Err(anyhow!(
"command failed with exit code {:?}:\nstdout: {}\nstderr: {}",
output.status.code(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
));
}
let stdout = String::from_utf8(output.stdout)?;
Ok(Some(
buffer
.update(cx, |buffer, cx| buffer.diff(stdout, cx))?
.await,
))
}
#[inline(never)]