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_focus(&focus_handle, Self::handle_focus).detach();
cx.on_blur(&focus_handle, Self::handle_blur).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 { let mut this = Self {
focus_handle, focus_handle,
buffer: buffer.clone(), buffer: buffer.clone(),
@@ -1726,7 +1732,7 @@ impl Editor {
show_git_diff_gutter: None, show_git_diff_gutter: None,
show_code_actions: None, show_code_actions: None,
show_wrap_guides: None, show_wrap_guides: None,
show_indent_guides: None, show_indent_guides,
placeholder_text: None, placeholder_text: None,
highlight_order: 0, highlight_order: 0,
highlighted_rows: HashMap::default(), highlighted_rows: HashMap::default(),

View File

@@ -4989,21 +4989,20 @@ impl Project {
FormatOnSave::On | FormatOnSave::Off, FormatOnSave::On | FormatOnSave::Off,
) )
| (_, FormatOnSave::External { command, arguments }) => { | (_, 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( format_operation = Self::format_via_external_command(
buffer, buffer,
buffer_abs_path, buffer_abs_path,
command, command,
arguments, arguments,
&mut cx, &mut cx,
) )
.await .await
.context(format!( .context(format!(
"failed to format via external command {:?}", "failed to format via external command {:?}",
command command
))? ))?
.map(FormatOperation::External); .map(FormatOperation::External);
}
} }
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => { (Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
let prettier = if prettier_settings.allowed { let prettier = if prettier_settings.allowed {
@@ -5142,7 +5141,7 @@ impl Project {
async fn format_via_external_command( async fn format_via_external_command(
buffer: &Model<Buffer>, buffer: &Model<Buffer>,
buffer_abs_path: &Path, buffer_abs_path: Option<&Path>,
command: &str, command: &str,
arguments: &[String], arguments: &[String],
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
@@ -5157,46 +5156,51 @@ impl Project {
Some(worktree_path) Some(worktree_path)
})?; })?;
if let Some(working_dir_path) = working_dir_path { let mut child = smol::process::Command::new(command);
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 output = child.output().await?; if let Some(buffer_abs_path) = buffer_abs_path {
if !output.status.success() { child.args(
return Err(anyhow!( arguments
"command failed with exit code {:?}:\nstdout: {}\nstderr: {}", .iter()
output.status.code(), .map(|arg| arg.replace("{buffer_path}", &buffer_abs_path.to_string_lossy())),
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(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)] #[inline(never)]