Merge pull request #2017 from zed-industries/dont-save-single-file-workspaces

Kay Simmons created

Don't save single file worktrees

Change summary

crates/workspace/src/persistence.rs | 19 ++++++++++---------
crates/workspace/src/workspace.rs   |  4 ++--
crates/zed/src/main.rs              | 22 ++++++++++++++--------
3 files changed, 26 insertions(+), 19 deletions(-)

Detailed changes

crates/workspace/src/persistence.rs 🔗

@@ -216,7 +216,9 @@ impl WorkspaceDb {
         let mut result = Vec::new();
         let mut delete_tasks = Vec::new();
         for (id, location) in self.recent_workspaces()? {
-            if location.paths().iter().all(|path| path.exists()) {
+            if location.paths().iter().all(|path| path.exists())
+                && location.paths().iter().any(|path| path.is_dir())
+            {
                 result.push((id, location));
             } else {
                 delete_tasks.push(self.delete_stale_workspace(id));
@@ -227,14 +229,13 @@ impl WorkspaceDb {
         Ok(result)
     }
 
-    query! {
-        pub fn last_workspace() -> Result<Option<WorkspaceLocation>> {
-            SELECT workspace_location
-            FROM workspaces
-            WHERE workspace_location IS NOT NULL
-            ORDER BY timestamp DESC
-            LIMIT 1
-        }
+    pub async fn last_workspace(&self) -> Result<Option<WorkspaceLocation>> {
+        Ok(self
+            .recent_workspaces_on_disk()
+            .await?
+            .into_iter()
+            .next()
+            .map(|(_, location)| location))
     }
 
     fn get_center_pane_group(&self, workspace_id: WorkspaceId) -> Result<SerializedPaneGroup> {

crates/workspace/src/workspace.rs 🔗

@@ -2680,8 +2680,8 @@ pub fn activate_workspace_for_project(
     None
 }
 
-pub fn last_opened_workspace_paths() -> Option<WorkspaceLocation> {
-    DB.last_workspace().log_err().flatten()
+pub async fn last_opened_workspace_paths() -> Option<WorkspaceLocation> {
+    DB.last_workspace().await.log_err().flatten()
 }
 
 #[allow(clippy::type_complexity)]

crates/zed/src/main.rs 🔗

@@ -170,7 +170,8 @@ fn main() {
             cx.platform().activate(true);
             let paths = collect_path_args();
             if paths.is_empty() {
-                restore_or_create_workspace(cx);
+                cx.spawn(|cx| async move { restore_or_create_workspace(cx).await })
+                    .detach()
             } else {
                 cx.dispatch_global_action(OpenPaths { paths });
             }
@@ -179,7 +180,8 @@ fn main() {
                 cx.spawn(|cx| handle_cli_connection(connection, app_state.clone(), cx))
                     .detach();
             } else {
-                restore_or_create_workspace(cx);
+                cx.spawn(|cx| async move { restore_or_create_workspace(cx).await })
+                    .detach()
             }
             cx.spawn(|cx| async move {
                 while let Some(connection) = cli_connections_rx.next().await {
@@ -203,13 +205,17 @@ fn main() {
     });
 }
 
-fn restore_or_create_workspace(cx: &mut gpui::MutableAppContext) {
-    if let Some(location) = workspace::last_opened_workspace_paths() {
-        cx.dispatch_global_action(OpenPaths {
-            paths: location.paths().as_ref().clone(),
-        })
+async fn restore_or_create_workspace(mut cx: AsyncAppContext) {
+    if let Some(location) = workspace::last_opened_workspace_paths().await {
+        cx.update(|cx| {
+            cx.dispatch_global_action(OpenPaths {
+                paths: location.paths().as_ref().clone(),
+            })
+        });
     } else {
-        cx.dispatch_global_action(NewFile);
+        cx.update(|cx| {
+            cx.dispatch_global_action(NewFile);
+        });
     }
 }