on master help sign in
Simplify write/write_all commands, we no longer need to excessively block
115754cBlaž Hrastnik
helix-term/src/commands.rs+18−21
@@ -31,7 +31,7 @@ use crate::{
31};
31};
32
32
33use crate::job::{self, Job, Jobs};
33use crate::job::{self, Job, Jobs};
34−use futures_util::{FutureExt, TryFutureExt};
34+use futures_util::FutureExt;
35use std::num::NonZeroUsize;
35use std::num::NonZeroUsize;
36use std::{fmt, future::Future};
36use std::{fmt, future::Future};
37
37
@@ -1389,7 +1389,7 @@ mod cmd {
1389 fn write_impl<P: AsRef<Path>>(
1389 fn write_impl<P: AsRef<Path>>(
1390 cx: &mut compositor::Context,
1390 cx: &mut compositor::Context,
1391 path: Option<P>,
1391 path: Option<P>,
1392− ) -> Result<tokio::task::JoinHandle<Result<(), anyhow::Error>>, anyhow::Error> {
1392+ ) -> anyhow::Result<()> {
1393 let jobs = &mut cx.jobs;
1393 let jobs = &mut cx.jobs;
1394 let (_, doc) = current!(cx.editor);
1394 let (_, doc) = current!(cx.editor);
1395
1395
@@ -1410,7 +1410,9 @@ mod cmd {
1410 jobs.callback(callback);
1410 jobs.callback(callback);
1411 shared
1411 shared
1412 });
1412 });
1413− Ok(tokio::spawn(doc.format_and_save(fmt)))
1413+ let future = doc.format_and_save(fmt);
1414+ cx.jobs.add(Job::new(future).wait_before_exiting());
1415+ Ok(())
1414 }
1416 }
1415
1417
1416 fn write(
1418 fn write(
@@ -1418,11 +1420,7 @@ mod cmd {
1418 args: &[&str],
1420 args: &[&str],
1419 _event: PromptEvent,
1421 _event: PromptEvent,
1420 ) -> anyhow::Result<()> {
1422 ) -> anyhow::Result<()> {
1421− let handle = write_impl(cx, args.first())?;
1423+ write_impl(cx, args.first())
1422− cx.jobs
1423− .add(Job::new(handle.unwrap_or_else(|e| Err(e.into()))).wait_before_exiting());
1424−
1425− Ok(())
1426 }
1424 }
1427
1425
1428 fn new_file(
1426 fn new_file(
@@ -1565,8 +1563,7 @@ mod cmd {
1565 args: &[&str],
1563 args: &[&str],
1566 event: PromptEvent,
1564 event: PromptEvent,
1567 ) -> anyhow::Result<()> {
1565 ) -> anyhow::Result<()> {
1568− let handle = write_impl(cx, args.first())?;
1566+ write_impl(cx, args.first())?;
1569− let _ = helix_lsp::block_on(handle)?;
1570 quit(cx, &[], event)
1567 quit(cx, &[], event)
1571 }
1568 }
1572
1569
@@ -1575,8 +1572,7 @@ mod cmd {
1575 args: &[&str],
1572 args: &[&str],
1576 event: PromptEvent,
1573 event: PromptEvent,
1577 ) -> anyhow::Result<()> {
1574 ) -> anyhow::Result<()> {
1578− let handle = write_impl(cx, args.first())?;
1575+ write_impl(cx, args.first())?;
1579− let _ = helix_lsp::block_on(handle)?;
1580 force_quit(cx, &[], event)
1576 force_quit(cx, &[], event)
1581 }
1577 }
1582
1578
@@ -1603,7 +1599,7 @@ mod cmd {
1603 }
1599 }
1604
1600
1605 fn write_all_impl(
1601 fn write_all_impl(
1606− editor: &mut Editor,
1602+ cx: &mut compositor::Context,
1607 _args: &[&str],
1603 _args: &[&str],
1608 _event: PromptEvent,
1604 _event: PromptEvent,
1609 quit: bool,
1605 quit: bool,
@@ -1612,25 +1608,26 @@ mod cmd {
1612 let mut errors = String::new();
1608 let mut errors = String::new();
1613
1609
1614 // save all documents
1610 // save all documents
1615− for (_, doc) in &mut editor.documents {
1611+ for (_, doc) in &mut cx.editor.documents {
1616 if doc.path().is_none() {
1612 if doc.path().is_none() {
1617 errors.push_str("cannot write a buffer without a filename\n");
1613 errors.push_str("cannot write a buffer without a filename\n");
1618 continue;
1614 continue;
1619 }
1615 }
1620
1616
1621 // TODO: handle error.
1617 // TODO: handle error.
1622− let _ = helix_lsp::block_on(tokio::spawn(doc.save()));
1618+ let handle = doc.save();
1619+ cx.jobs.add(Job::new(handle).wait_before_exiting());
1623 }
1620 }
1624
1621
1625 if quit {
1622 if quit {
1626 if !force {
1623 if !force {
1627− buffers_remaining_impl(editor)?;
1624+ buffers_remaining_impl(cx.editor)?;
1628 }
1625 }
1629
1626
1630 // close all views
1627 // close all views
1631− let views: Vec<_> = editor.tree.views().map(|(view, _)| view.id).collect();
1628+ let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
1632 for view_id in views {
1629 for view_id in views {
1633− editor.close(view_id, false);
1630+ cx.editor.close(view_id, false);
1634 }
1631 }
1635 }
1632 }
1636
1633
@@ -1642,7 +1639,7 @@ mod cmd {
1642 args: &[&str],
1639 args: &[&str],
1643 event: PromptEvent,
1640 event: PromptEvent,
1644 ) -> anyhow::Result<()> {
1641 ) -> anyhow::Result<()> {
1645− write_all_impl(&mut cx.editor, args, event, false, false)
1642+ write_all_impl(cx, args, event, false, false)
1646 }
1643 }
1647
1644
1648 fn write_all_quit(
1645 fn write_all_quit(
@@ -1650,7 +1647,7 @@ mod cmd {
1650 args: &[&str],
1647 args: &[&str],
1651 event: PromptEvent,
1648 event: PromptEvent,
1652 ) -> anyhow::Result<()> {
1649 ) -> anyhow::Result<()> {
1653− write_all_impl(&mut cx.editor, args, event, true, false)
1650+ write_all_impl(cx, args, event, true, false)
1654 }
1651 }
1655
1652
1656 fn force_write_all_quit(
1653 fn force_write_all_quit(
@@ -1658,7 +1655,7 @@ mod cmd {
1658 args: &[&str],
1655 args: &[&str],
1659 event: PromptEvent,
1656 event: PromptEvent,
1660 ) -> anyhow::Result<()> {
1657 ) -> anyhow::Result<()> {
1661− write_all_impl(&mut cx.editor, args, event, true, true)
1658+ write_all_impl(cx, args, event, true, true)
1662 }
1659 }
1663
1660
1664 fn quit_all_impl(
1661 fn quit_all_impl(