minimize diff

A few edits here to minimize the overall (squashed) diff:

* Minimize borrow-checker-satisfying changes in write_impl,
  write_all_impl. We just need to "refresh" the bindings of `doc` and
  `view` It's nice to avoid extra blocks for the `&mut` bindings since
  they unnecessarily add indentation/nesting.
* Factor out the `Result<Vec<_>, _>` early in `Document::format`: this
  avoids changing the indentation for the rest of the block.

With these changes the stat (git diff --stat 9f3b193743) lowers from
+128,-82 to +51,-10.

Also there's a style change here to prefer `use`-ing the
`crate::expansion` module so it's clearer where the `expand` function
comes from.
pull/13429/head
Michael Davis 2025-04-28 18:07:39 -04:00
parent 3763fc3c54
commit 3d8a63e072
No known key found for this signature in database
2 changed files with 87 additions and 92 deletions

View File

@ -324,7 +324,6 @@ fn buffer_previous(
fn write_impl(cx: &mut compositor::Context, path: Option<&str>, force: bool) -> anyhow::Result<()> { fn write_impl(cx: &mut compositor::Context, path: Option<&str>, force: bool) -> anyhow::Result<()> {
let config = cx.editor.config(); let config = cx.editor.config();
let jobs = &mut cx.jobs; let jobs = &mut cx.jobs;
{
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
if doc.trim_trailing_whitespace() { if doc.trim_trailing_whitespace() {
@ -339,7 +338,6 @@ fn write_impl(cx: &mut compositor::Context, path: Option<&str>, force: bool) ->
// Save an undo checkpoint for any outstanding changes. // Save an undo checkpoint for any outstanding changes.
doc.append_changes_to_history(view); doc.append_changes_to_history(view);
}
let (view, doc) = current_ref!(cx.editor); let (view, doc) = current_ref!(cx.editor);
let fmt = if config.auto_format { let fmt = if config.auto_format {
@ -738,7 +736,6 @@ pub fn write_all_impl(
.collect(); .collect();
for (doc_id, target_view) in saves { for (doc_id, target_view) in saves {
{
let doc = doc_mut!(cx.editor, &doc_id); let doc = doc_mut!(cx.editor, &doc_id);
let view = view_mut!(cx.editor, target_view); let view = view_mut!(cx.editor, target_view);
@ -754,10 +751,9 @@ pub fn write_all_impl(
// Save an undo checkpoint for any outstanding changes. // Save an undo checkpoint for any outstanding changes.
doc.append_changes_to_history(view); doc.append_changes_to_history(view);
}
let doc = doc!(cx.editor, &doc_id);
let fmt = if options.auto_format && config.auto_format { let fmt = if options.auto_format && config.auto_format {
let doc = doc!(cx.editor, &doc_id);
doc.auto_format(cx.editor).map(|fmt| { doc.auto_format(cx.editor).map(|fmt| {
let callback = make_format_callback( let callback = make_format_callback(
doc_id, doc_id,

View File

@ -5,7 +5,6 @@ use futures_util::future::BoxFuture;
use futures_util::FutureExt; use futures_util::FutureExt;
use helix_core::auto_pairs::AutoPairs; use helix_core::auto_pairs::AutoPairs;
use helix_core::chars::char_is_word; use helix_core::chars::char_is_word;
use helix_core::command_line::Token; use helix_core::command_line::Token;
use helix_core::diagnostic::DiagnosticProvider; use helix_core::diagnostic::DiagnosticProvider;
use helix_core::doc_formatter::TextFormat; use helix_core::doc_formatter::TextFormat;
@ -44,10 +43,10 @@ use helix_core::{
ChangeSet, Diagnostic, LineEnding, Range, Rope, RopeBuilder, Selection, Syntax, Transaction, ChangeSet, Diagnostic, LineEnding, Range, Rope, RopeBuilder, Selection, Syntax, Transaction,
}; };
use crate::expansion::expand;
use crate::{ use crate::{
editor::Config, editor::Config,
events::{DocumentDidChange, SelectionDidChange}, events::{DocumentDidChange, SelectionDidChange},
expansion,
view::ViewPosition, view::ViewPosition,
DocumentId, Editor, Theme, View, ViewId, DocumentId, Editor, Theme, View, ViewId,
}; };
@ -823,22 +822,26 @@ impl Document {
process.current_dir(doc_dir); process.current_dir(doc_dir);
} }
let fmt_args = fmt_args let args = match fmt_args
.iter() .iter()
.map(|content| expand(editor, Token::expand(content))) .map(|content| expansion::expand(editor, Token::expand(content)))
.collect::<anyhow::Result<Vec<_>>>(); .collect::<Result<Vec<_>, _>>()
{
Ok(args) => args,
Err(err) => {
log::error!("Failed to expand formatter arguments: {err}");
return None;
}
};
match fmt_args {
Ok(fmt_args) => {
process process
.args(fmt_args.iter().map(Cow::as_ref)) .args(args.iter().map(AsRef::as_ref))
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()); .stderr(Stdio::piped());
let formatting_future = async move { let formatting_future = async move {
let mut process = let mut process = process
process
.spawn() .spawn()
.map_err(|e| FormatterError::SpawningFailed { .map_err(|e| FormatterError::SpawningFailed {
command: fmt_cmd.to_string_lossy().into(), command: fmt_cmd.to_string_lossy().into(),
@ -858,8 +861,7 @@ impl Document {
process.wait_with_output(), process.wait_with_output(),
}; };
let _ = input_result.map_err(|_| FormatterError::BrokenStdin)?; let _ = input_result.map_err(|_| FormatterError::BrokenStdin)?;
let output = let output = output_result.map_err(|_| FormatterError::WaitForOutputFailed)?;
output_result.map_err(|_| FormatterError::WaitForOutputFailed)?;
if !output.status.success() { if !output.status.success() {
if !output.stderr.is_empty() { if !output.stderr.is_empty() {
@ -882,9 +884,6 @@ impl Document {
Ok(helix_core::diff::compare_ropes(&text, &Rope::from(str))) Ok(helix_core::diff::compare_ropes(&text, &Rope::from(str)))
}; };
return Some(formatting_future.boxed()); return Some(formatting_future.boxed());
}
Err(e) => log::error!("{e}"),
}
}; };
let text = self.text.clone(); let text = self.text.clone();