aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/commands
diff options
context:
space:
mode:
authorBlaž Hrastnik2022-10-14 07:22:21 +0000
committerSkyler Hawthorne2022-10-19 02:31:39 +0000
commit30c93994b50888aaeb32c65c90426e997800ccea (patch)
treeb1c62f58120f58b3a12679e59e4afa1508aa8152 /helix-term/src/commands
parentbeb3427bfbaa88bec8b4c683e342f85eb53ad77d (diff)
Use a single save_queue on the editor
Diffstat (limited to 'helix-term/src/commands')
-rw-r--r--helix-term/src/commands/typed.rs88
1 files changed, 54 insertions, 34 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 070215cb..ef774256 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -79,12 +79,28 @@ fn buffer_close_by_ids_impl(
doc_ids: &[DocumentId],
force: bool,
) -> anyhow::Result<()> {
+ // TODO: deduplicate with ctx.block_try_flush_writes
+ tokio::task::block_in_place(|| {
+ helix_lsp::block_on(async {
+ while let Some(save_event) = editor.save_queue.next().await {
+ match &save_event {
+ Ok(event) => {
+ let doc = doc_mut!(editor, &event.doc_id);
+ doc.set_last_saved_revision(event.revision);
+ }
+ Err(err) => {
+ log::error!("error saving document: {}", err);
+ }
+ };
+ // TODO: if is_err: break?
+ }
+ })
+ });
+
let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
.iter()
.filter_map(|&doc_id| {
- if let Err(CloseError::BufferModified(name)) = tokio::task::block_in_place(|| {
- helix_lsp::block_on(editor.close_document(doc_id, force))
- }) {
+ if let Err(CloseError::BufferModified(name)) = editor.close_document(doc_id, force) {
Some((doc_id, name))
} else {
None
@@ -289,7 +305,8 @@ fn write_impl(
};
if fmt.is_none() {
- doc.save(path, force)?;
+ let id = doc.id();
+ cx.editor.save(id, path, force)?;
}
Ok(())
@@ -569,40 +586,45 @@ fn write_all_impl(
return Ok(());
}
- let mut errors: Option<String> = None;
+ let mut errors: Vec<&'static str> = Vec::new();
let auto_format = cx.editor.config().auto_format;
let jobs = &mut cx.jobs;
// save all documents
- for doc in &mut cx.editor.documents.values_mut() {
- if doc.path().is_none() {
- errors = errors
- .or_else(|| Some(String::new()))
- .map(|mut errs: String| {
- errs.push_str("cannot write a buffer without a filename\n");
- errs
- });
-
- continue;
- }
+ let saves: Vec<_> = cx
+ .editor
+ .documents
+ .values()
+ .filter_map(|doc| {
+ if doc.path().is_none() {
+ errors.push("cannot write a buffer without a filename\n");
+ return None;
+ }
- if !doc.is_modified() {
- continue;
- }
+ if !doc.is_modified() {
+ return None;
+ }
- let fmt = if auto_format {
- doc.auto_format().map(|fmt| {
- let callback =
- make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
- jobs.add(Job::with_callback(callback).wait_before_exiting());
- })
- } else {
+ let fmt = if auto_format {
+ doc.auto_format().map(|fmt| {
+ let callback =
+ make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
+ jobs.add(Job::with_callback(callback).wait_before_exiting());
+ })
+ } else {
+ None
+ };
+
+ if fmt.is_none() {
+ return Some(doc.id());
+ }
None
- };
+ })
+ .collect();
- if fmt.is_none() {
- doc.save::<PathBuf>(None, force)?;
- }
+ // manually call save for the rest of docs that don't have a formatter
+ for id in saves {
+ cx.editor.save::<PathBuf>(id, None, force)?;
}
if quit {
@@ -619,10 +641,8 @@ fn write_all_impl(
}
}
- if let Some(errs) = errors {
- if !force {
- bail!(errs);
- }
+ if !errors.is_empty() && !force {
+ bail!("{:?}", errors);
}
Ok(())