@@ -2,7 +2,7 @@ use std::process::ExitStatus;
use anyhow::Result;
use collections::HashSet;
-use gpui::{AppContext, Context, Entity, Task};
+use gpui::{AppContext, AsyncWindowContext, Context, Entity, Task, WeakEntity};
use language::Buffer;
use project::{TaskSourceKind, WorktreeId};
use remote::ConnectionState;
@@ -78,26 +78,7 @@ impl Workspace {
if self.terminal_provider.is_some() {
let task = cx.spawn_in(window, async move |workspace, cx| {
- let save_action = match spawn_in_terminal.save {
- SaveStrategy::All => {
- let save_all = workspace.update_in(cx, |workspace, window, cx| {
- let task = workspace.save_all_internal(SaveIntent::SaveAll, window, cx);
- // Match the type of the other arm by ignoring the bool value returned
- cx.background_spawn(async { task.await.map(|_| ()) })
- });
- save_all.ok()
- }
- SaveStrategy::Current => {
- let save_current = workspace.update_in(cx, |workspace, window, cx| {
- workspace.save_active_item(SaveIntent::SaveAll, window, cx)
- });
- save_current.ok()
- }
- SaveStrategy::None => None,
- };
- if let Some(save_action) = save_action {
- save_action.log_err().await;
- }
+ Self::save_for_task(&workspace, spawn_in_terminal.save, cx).await;
let spawn_task = workspace.update_in(cx, |workspace, window, cx| {
workspace
@@ -132,6 +113,32 @@ impl Workspace {
}
}
+ pub async fn save_for_task(
+ workspace: &WeakEntity<Self>,
+ save_strategy: SaveStrategy,
+ cx: &mut AsyncWindowContext,
+ ) {
+ let save_action = match save_strategy {
+ SaveStrategy::All => {
+ let save_all = workspace.update_in(cx, |workspace, window, cx| {
+ let task = workspace.save_all_internal(SaveIntent::SaveAll, window, cx);
+ cx.background_spawn(async { task.await.map(|_| ()) })
+ });
+ save_all.ok()
+ }
+ SaveStrategy::Current => {
+ let save_current = workspace.update_in(cx, |workspace, window, cx| {
+ workspace.save_active_item(SaveIntent::SaveAll, window, cx)
+ });
+ save_current.ok()
+ }
+ SaveStrategy::None => None,
+ };
+ if let Some(save_action) = save_action {
+ save_action.log_err().await;
+ }
+ }
+
pub fn start_debug_session(
&mut self,
scenario: DebugScenario,
@@ -417,6 +424,69 @@ mod tests {
item
}
+ #[gpui::test]
+ async fn test_save_for_task_all(cx: &mut TestAppContext) {
+ let (fixture, cx) = create_fixture(cx, SaveStrategy::All).await;
+ let workspace = fixture.workspace.downgrade();
+ cx.run_until_parked();
+
+ assert!(cx.read(|cx| fixture.item.read(cx).is_dirty));
+ fixture.workspace.update_in(cx, |_workspace, window, cx| {
+ cx.spawn_in(window, {
+ let workspace = workspace.clone();
+ async move |_this, cx| {
+ Workspace::save_for_task(&workspace, SaveStrategy::All, cx).await;
+ }
+ })
+ .detach();
+ });
+ cx.run_until_parked();
+ assert!(cx.read(|cx| !fixture.item.read(cx).is_dirty));
+ }
+
+ #[gpui::test]
+ async fn test_save_for_task_none(cx: &mut TestAppContext) {
+ let (fixture, cx) = create_fixture(cx, SaveStrategy::None).await;
+ let workspace = fixture.workspace.downgrade();
+ cx.run_until_parked();
+
+ assert!(cx.read(|cx| fixture.item.read(cx).is_dirty));
+ fixture.workspace.update_in(cx, |_workspace, window, cx| {
+ cx.spawn_in(window, {
+ let workspace = workspace.clone();
+ async move |_this, cx| {
+ Workspace::save_for_task(&workspace, SaveStrategy::None, cx).await;
+ }
+ })
+ .detach();
+ });
+ cx.run_until_parked();
+ assert!(cx.read(|cx| fixture.item.read(cx).is_dirty));
+ }
+
+ #[gpui::test]
+ async fn test_save_for_task_current(cx: &mut TestAppContext) {
+ let (fixture, cx) = create_fixture(cx, SaveStrategy::Current).await;
+ let inactive = add_test_item(&fixture.workspace, "file2.txt", false, cx);
+ let workspace = fixture.workspace.downgrade();
+ cx.run_until_parked();
+
+ assert!(cx.read(|cx| fixture.item.read(cx).is_dirty));
+ assert!(cx.read(|cx| inactive.read(cx).is_dirty));
+ fixture.workspace.update_in(cx, |_workspace, window, cx| {
+ cx.spawn_in(window, {
+ let workspace = workspace.clone();
+ async move |_this, cx| {
+ Workspace::save_for_task(&workspace, SaveStrategy::Current, cx).await;
+ }
+ })
+ .detach();
+ });
+ cx.run_until_parked();
+ assert!(cx.read(|cx| !fixture.item.read(cx).is_dirty));
+ assert!(cx.read(|cx| inactive.read(cx).is_dirty));
+ }
+
struct TestTerminalProvider {
item: Entity<TestItem>,
dirty_before_spawn: Arc<Mutex<Option<bool>>>,