Skip to content

Commit

Permalink
fix panic when view of pending write is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck committed Oct 19, 2022
1 parent faa00d4 commit e5fd5e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 8 additions & 10 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn quit(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
buffers_remaining_impl(cx.editor)?
}

cx.block_try_flush_writes()?;
cx.editor.close(view!(cx.editor).id);

Ok(())
Expand Down Expand Up @@ -518,15 +519,7 @@ fn write_quit(
}

write_impl(cx, args.first(), false)?;

tokio::task::block_in_place(|| helix_lsp::block_on(cx.jobs.finish(Some(cx.editor), None)))?;

let doc = doc_mut!(cx.editor);

tokio::task::block_in_place(|| helix_lsp::block_on(doc.try_flush_saves()))
.map(|result| result.map(|_| ()))
.unwrap_or(Ok(()))?;

cx.block_try_flush_writes()?;
quit(cx, &[], event)
}

Expand All @@ -540,6 +533,7 @@ fn force_write_quit(
}

write_impl(cx, args.first(), true)?;
cx.block_try_flush_writes()?;
force_quit(cx, &[], event)
}

Expand Down Expand Up @@ -613,6 +607,8 @@ fn write_all_impl(
buffers_remaining_impl(cx.editor)?;
}

cx.block_try_flush_writes()?;

// close all views
let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
for view_id in views {
Expand Down Expand Up @@ -682,6 +678,7 @@ fn quit_all(
return Ok(());
}

cx.block_try_flush_writes()?;
quit_all_impl(cx.editor, false)
}

Expand Down Expand Up @@ -710,8 +707,9 @@ fn cquit(
.first()
.and_then(|code| code.parse::<i32>().ok())
.unwrap_or(1);
cx.editor.exit_code = exit_code;

cx.editor.exit_code = exit_code;
cx.block_try_flush_writes()?;
quit_all_impl(cx.editor, false)
}

Expand Down
18 changes: 18 additions & 0 deletions helix-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ pub struct Context<'a> {
pub jobs: &'a mut Jobs,
}

impl<'a> Context<'a> {
/// Waits on all pending jobs, and then tries to flush all pending write
/// operations for the current document.
pub fn block_try_flush_writes(&mut self) -> anyhow::Result<()> {
tokio::task::block_in_place(|| {
helix_lsp::block_on(self.jobs.finish(Some(self.editor), None))
})?;

let doc = doc_mut!(self.editor);

tokio::task::block_in_place(|| helix_lsp::block_on(doc.try_flush_saves()))
.map(|result| result.map(|_| ()))
.unwrap_or(Ok(()))?;

Ok(())
}
}

pub trait Component: Any + AnyComponent {
/// Process input events, return true if handled.
fn handle_event(&mut self, _event: &Event, _ctx: &mut Context) -> EventResult {
Expand Down

0 comments on commit e5fd5e2

Please sign in to comment.