fix modified status with auto format
This commit is contained in:
parent
cb23399dee
commit
c9418582d2
3 changed files with 36 additions and 17 deletions
|
@ -52,7 +52,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::job::{self, Jobs};
|
use crate::job::{self, Jobs};
|
||||||
use futures_util::{FutureExt, StreamExt};
|
use futures_util::StreamExt;
|
||||||
use std::{collections::HashMap, fmt, future::Future};
|
use std::{collections::HashMap, fmt, future::Future};
|
||||||
use std::{collections::HashSet, num::NonZeroUsize};
|
use std::{collections::HashSet, num::NonZeroUsize};
|
||||||
|
|
||||||
|
@ -2513,6 +2513,7 @@ async fn make_format_callback(
|
||||||
doc_id: DocumentId,
|
doc_id: DocumentId,
|
||||||
doc_version: i32,
|
doc_version: i32,
|
||||||
format: impl Future<Output = Result<Transaction, FormatterError>> + Send + 'static,
|
format: impl Future<Output = Result<Transaction, FormatterError>> + Send + 'static,
|
||||||
|
write: Option<(Option<PathBuf>, bool)>,
|
||||||
) -> anyhow::Result<job::Callback> {
|
) -> anyhow::Result<job::Callback> {
|
||||||
let format = format.await?;
|
let format = format.await?;
|
||||||
let call: job::Callback = Box::new(move |editor, _compositor| {
|
let call: job::Callback = Box::new(move |editor, _compositor| {
|
||||||
|
@ -2523,11 +2524,25 @@ async fn make_format_callback(
|
||||||
let scrolloff = editor.config().scrolloff;
|
let scrolloff = editor.config().scrolloff;
|
||||||
let doc = doc_mut!(editor, &doc_id);
|
let doc = doc_mut!(editor, &doc_id);
|
||||||
let view = view_mut!(editor);
|
let view = view_mut!(editor);
|
||||||
|
let loader = editor.syn_loader.clone();
|
||||||
|
|
||||||
if doc.version() == doc_version {
|
if doc.version() == doc_version {
|
||||||
apply_transaction(&format, doc, view);
|
apply_transaction(&format, doc, view);
|
||||||
doc.append_changes_to_history(view.id);
|
doc.append_changes_to_history(view.id);
|
||||||
doc.detect_indent_and_line_ending();
|
doc.detect_indent_and_line_ending();
|
||||||
view.ensure_cursor_in_view(doc, scrolloff);
|
view.ensure_cursor_in_view(doc, scrolloff);
|
||||||
|
|
||||||
|
if let Some((path, force)) = write {
|
||||||
|
let refresh_lang = path.is_some();
|
||||||
|
|
||||||
|
if let Err(err) = doc.save(path, force) {
|
||||||
|
editor.set_error(format!("Error saving: {}", err));
|
||||||
|
} else if refresh_lang {
|
||||||
|
let id = doc.id();
|
||||||
|
doc.detect_language(loader);
|
||||||
|
let _ = editor.refresh_language_server(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log::info!("discarded formatting changes because the document changed");
|
log::info!("discarded formatting changes because the document changed");
|
||||||
}
|
}
|
||||||
|
|
|
@ -267,30 +267,32 @@ fn write_impl(
|
||||||
path: Option<&Cow<str>>,
|
path: Option<&Cow<str>>,
|
||||||
force: bool,
|
force: bool,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let auto_format = cx.editor.config().auto_format;
|
let editor_auto_fmt = cx.editor.config().auto_format;
|
||||||
let jobs = &mut cx.jobs;
|
let jobs = &mut cx.jobs;
|
||||||
let doc = doc_mut!(cx.editor);
|
let doc = doc_mut!(cx.editor);
|
||||||
|
let path = path.map(AsRef::as_ref);
|
||||||
|
|
||||||
if doc.path().is_none() {
|
if doc.path().is_none() {
|
||||||
bail!("cannot write a buffer without a filename");
|
bail!("cannot write a buffer without a filename");
|
||||||
}
|
}
|
||||||
let fmt = if auto_format {
|
|
||||||
|
let fmt = if editor_auto_fmt {
|
||||||
doc.auto_format().map(|fmt| {
|
doc.auto_format().map(|fmt| {
|
||||||
let shared = fmt.shared();
|
let callback = make_format_callback(
|
||||||
let callback = make_format_callback(doc.id(), doc.version(), shared.clone());
|
doc.id(),
|
||||||
|
doc.version(),
|
||||||
|
fmt,
|
||||||
|
Some((path.map(Into::into), force)),
|
||||||
|
);
|
||||||
|
|
||||||
jobs.add(Job::with_callback(callback).wait_before_exiting());
|
jobs.add(Job::with_callback(callback).wait_before_exiting());
|
||||||
shared
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
doc.format_and_save(fmt, path.map(AsRef::as_ref), force)?;
|
if fmt.is_none() {
|
||||||
|
doc.save(path, force)?;
|
||||||
if path.is_some() {
|
|
||||||
let id = doc.id();
|
|
||||||
doc.detect_language(cx.editor.syn_loader.clone());
|
|
||||||
let _ = cx.editor.refresh_language_server(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -345,7 +347,7 @@ fn format(
|
||||||
|
|
||||||
let doc = doc!(cx.editor);
|
let doc = doc!(cx.editor);
|
||||||
if let Some(format) = doc.format() {
|
if let Some(format) = doc.format() {
|
||||||
let callback = make_format_callback(doc.id(), doc.version(), format);
|
let callback = make_format_callback(doc.id(), doc.version(), format, None);
|
||||||
cx.jobs.callback(callback);
|
cx.jobs.callback(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -592,16 +594,17 @@ fn write_all_impl(
|
||||||
|
|
||||||
let fmt = if auto_format {
|
let fmt = if auto_format {
|
||||||
doc.auto_format().map(|fmt| {
|
doc.auto_format().map(|fmt| {
|
||||||
let shared = fmt.shared();
|
let callback =
|
||||||
let callback = make_format_callback(doc.id(), doc.version(), shared.clone());
|
make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
|
||||||
jobs.callback(callback);
|
jobs.callback(callback);
|
||||||
shared
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
doc.format_and_save::<_, PathBuf>(fmt, None, force)?;
|
if fmt.is_none() {
|
||||||
|
doc.save::<PathBuf>(None, force)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if quit {
|
if quit {
|
||||||
|
|
|
@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
|
||||||
|
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use helix_core::{
|
use helix_core::{
|
||||||
|
|
Loading…
Add table
Reference in a new issue