Skip to content

Commit

Permalink
Editor::flush_writes returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck committed Oct 19, 2022
1 parent 52ba550 commit e645804
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
5 changes: 4 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,10 @@ impl Application {
errs.push(err);
};

self.editor.flush_writes().await;
if let Err(err) = self.editor.flush_writes().await {
log::error!("Error writing: {}", err);
errs.push(err);
}

if self.editor.close_language_servers(None).await.is_err() {
log::error!("Timed out waiting for language servers to shutdown");
Expand Down
23 changes: 11 additions & 12 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,16 @@ fn open(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
}

fn buffer_close_by_ids_impl(
editor: &mut Editor,
cx: &mut compositor::Context,
doc_ids: &[DocumentId],
force: bool,
) -> anyhow::Result<()> {
// TODO: deduplicate with ctx.block_try_flush_writes
tokio::task::block_in_place(|| helix_lsp::block_on(editor.flush_writes()));
cx.block_try_flush_writes()?;

let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
.iter()
.filter_map(|&doc_id| {
if let Err(CloseError::BufferModified(name)) = editor.close_document(doc_id, force) {
if let Err(CloseError::BufferModified(name)) = cx.editor.close_document(doc_id, force) {
Some((doc_id, name))
} else {
None
Expand All @@ -94,11 +93,11 @@ fn buffer_close_by_ids_impl(
.unzip();

if let Some(first) = modified_ids.first() {
let current = doc!(editor);
let current = doc!(cx.editor);
// If the current document is unmodified, and there are modified
// documents, switch focus to the first modified doc.
if !modified_ids.contains(&current.id()) {
editor.switch(*first, Action::Replace);
cx.editor.switch(*first, Action::Replace);
}
bail!(
"{} unsaved buffer(s) remaining: {:?}",
Expand Down Expand Up @@ -157,7 +156,7 @@ fn buffer_close(
}

let document_ids = buffer_gather_paths_impl(cx.editor, args);
buffer_close_by_ids_impl(cx.editor, &document_ids, false)
buffer_close_by_ids_impl(cx, &document_ids, false)
}

fn force_buffer_close(
Expand All @@ -170,7 +169,7 @@ fn force_buffer_close(
}

let document_ids = buffer_gather_paths_impl(cx.editor, args);
buffer_close_by_ids_impl(cx.editor, &document_ids, true)
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_gather_others_impl(editor: &mut Editor) -> Vec<DocumentId> {
Expand All @@ -192,7 +191,7 @@ fn buffer_close_others(
}

let document_ids = buffer_gather_others_impl(cx.editor);
buffer_close_by_ids_impl(cx.editor, &document_ids, false)
buffer_close_by_ids_impl(cx, &document_ids, false)
}

fn force_buffer_close_others(
Expand All @@ -205,7 +204,7 @@ fn force_buffer_close_others(
}

let document_ids = buffer_gather_others_impl(cx.editor);
buffer_close_by_ids_impl(cx.editor, &document_ids, true)
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_gather_all_impl(editor: &mut Editor) -> Vec<DocumentId> {
Expand All @@ -222,7 +221,7 @@ fn buffer_close_all(
}

let document_ids = buffer_gather_all_impl(cx.editor);
buffer_close_by_ids_impl(cx.editor, &document_ids, false)
buffer_close_by_ids_impl(cx, &document_ids, false)
}

fn force_buffer_close_all(
Expand All @@ -235,7 +234,7 @@ fn force_buffer_close_all(
}

let document_ids = buffer_gather_all_impl(cx.editor);
buffer_close_by_ids_impl(cx.editor, &document_ids, true)
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_next(
Expand Down
4 changes: 1 addition & 3 deletions helix-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ impl<'a> Context<'a> {
/// operations for all documents.
pub fn block_try_flush_writes(&mut self) -> anyhow::Result<()> {
tokio::task::block_in_place(|| helix_lsp::block_on(self.jobs.finish(self.editor, None)))?;

tokio::task::block_in_place(|| helix_lsp::block_on(self.editor.flush_writes()));

tokio::task::block_in_place(|| helix_lsp::block_on(self.editor.flush_writes()))?;
Ok(())
}
}
Expand Down
22 changes: 12 additions & 10 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tokio::{
time::{sleep, Duration, Instant, Sleep},
};

use anyhow::{anyhow, Error};
use anyhow::{anyhow, bail, Error};

pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
Expand Down Expand Up @@ -1355,22 +1355,24 @@ impl Editor {
}
}

pub async fn flush_writes(&mut self) {
pub async fn flush_writes(&mut self) -> anyhow::Result<()> {
while self.write_count > 0 {
if let Some(save_event) = self.save_queue.next().await {
match &save_event {
Ok(event) => {
let doc = doc_mut!(self, &event.doc_id);
doc.set_last_saved_revision(event.revision);
}
self.write_count -= 1;

let save_event = match save_event {
Ok(event) => event,
Err(err) => {
log::error!("error saving document: {}", err);
self.set_error(err.to_string());
bail!(err);
}
};
// TODO: if is_err: break?

self.write_count -= 1;
let doc = doc_mut!(self, &save_event.doc_id);
doc.set_last_saved_revision(save_event.revision);
}
}

Ok(())
}
}

0 comments on commit e645804

Please sign in to comment.