on master help sign in
fix modified status with auto format
commitc9418582d2a6d8dbb8b5bb1d3432a9087438e61d authorSkyler Hawthorne <skyler@dead10ck.com> dateTue 5 Jul 2022, 00:15 (4 years ago) parentcb23399
3 files changed+36−17
helix-term/src/commands.rs+16−1 helix-term/src/commands/typed.rs+19−16 helix-view/src/document.rs+1
helix-term/src/commands.rs+16−1
@@ -52,7 +52,7 @@ use crate::{
5252};
5353
5454use crate::job::{self, Jobs};
55−use futures_util::{FutureExt, StreamExt};
55+use futures_util::StreamExt;
5656use std::{collections::HashMap, fmt, future::Future};
5757use std::{collections::HashSet, num::NonZeroUsize};
5858
@@ -2513,6 +2513,7 @@ async fn make_format_callback(
25132513 doc_id: DocumentId,
25142514 doc_version: i32,
25152515 format: impl Future<Output = Result<Transaction, FormatterError>> + Send + 'static,
2516+ write: Option<(Option<PathBuf>, bool)>,
25162517) -> anyhow::Result<job::Callback> {
25172518 let format = format.await?;
25182519 let call: job::Callback = Box::new(move |editor, _compositor| {
@@ -2523,11 +2524,25 @@ async fn make_format_callback(
25232524 let scrolloff = editor.config().scrolloff;
25242525 let doc = doc_mut!(editor, &doc_id);
25252526 let view = view_mut!(editor);
2527+ let loader = editor.syn_loader.clone();
2528+
25262529 if doc.version() == doc_version {
25272530 apply_transaction(&format, doc, view);
25282531 doc.append_changes_to_history(view.id);
25292532 doc.detect_indent_and_line_ending();
25302533 view.ensure_cursor_in_view(doc, scrolloff);
2534+
2535+ if let Some((path, force)) = write {
2536+ let refresh_lang = path.is_some();
2537+
2538+ if let Err(err) = doc.save(path, force) {
2539+ editor.set_error(format!("Error saving: {}", err));
2540+ } else if refresh_lang {
2541+ let id = doc.id();
2542+ doc.detect_language(loader);
2543+ let _ = editor.refresh_language_server(id);
2544+ }
2545+ }
25312546 } else {
25322547 log::info!("discarded formatting changes because the document changed");
25332548 }
helix-term/src/commands/typed.rs+19−16
@@ -267,30 +267,32 @@ fn write_impl(
267267 path: Option<&Cow<str>>,
268268 force: bool,
269269) -> anyhow::Result<()> {
270− let auto_format = cx.editor.config().auto_format;
270+ let editor_auto_fmt = cx.editor.config().auto_format;
271271 let jobs = &mut cx.jobs;
272272 let doc = doc_mut!(cx.editor);
273+ let path = path.map(AsRef::as_ref);
273274
274275 if doc.path().is_none() {
275276 bail!("cannot write a buffer without a filename");
276277 }
277− let fmt = if auto_format {
278+
279+ let fmt = if editor_auto_fmt {
278280 doc.auto_format().map(|fmt| {
279− let shared = fmt.shared();
280− let callback = make_format_callback(doc.id(), doc.version(), shared.clone());
281+ let callback = make_format_callback(
282+ doc.id(),
283+ doc.version(),
284+ fmt,
285+ Some((path.map(Into::into), force)),
286+ );
287+
281288 jobs.add(Job::with_callback(callback).wait_before_exiting());
282− shared
283289 })
284290 } else {
285291 None
286292 };
287293
288− doc.format_and_save(fmt, path.map(AsRef::as_ref), force)?;
289−
290− if path.is_some() {
291− let id = doc.id();
292− doc.detect_language(cx.editor.syn_loader.clone());
293− let _ = cx.editor.refresh_language_server(id);
294+ if fmt.is_none() {
295+ doc.save(path, force)?;
294296 }
295297
296298 Ok(())
@@ -345,7 +347,7 @@ fn format(
345347
346348 let doc = doc!(cx.editor);
347349 if let Some(format) = doc.format() {
348− let callback = make_format_callback(doc.id(), doc.version(), format);
350+ let callback = make_format_callback(doc.id(), doc.version(), format, None);
349351 cx.jobs.callback(callback);
350352 }
351353
@@ -592,16 +594,17 @@ fn write_all_impl(
592594
593595 let fmt = if auto_format {
594596 doc.auto_format().map(|fmt| {
595− let shared = fmt.shared();
596− let callback = make_format_callback(doc.id(), doc.version(), shared.clone());
597+ let callback =
598+ make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
597599 jobs.callback(callback);
598− shared
599600 })
600601 } else {
601602 None
602603 };
603604
604− doc.format_and_save::<_, PathBuf>(fmt, None, force)?;
605+ if fmt.is_none() {
606+ doc.save::<PathBuf>(None, force)?;
607+ }
605608 }
606609
607610 if quit {
helix-view/src/document.rs+1
@@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
1414use std::str::FromStr;
1515use std::sync::Arc;
1616use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
17+
1718use tokio::sync::Mutex;
1819
1920use helix_core::{