move restorable_workspace_locations to workspace

Smit Barmase created

Change summary

crates/workspace/src/workspace.rs   | 55 +++++++++++++++++++++++++
crates/zed/src/main.rs              | 66 ------------------------------
crates/zed/src/zed/open_listener.rs |  3 
3 files changed, 58 insertions(+), 66 deletions(-)

Detailed changes

crates/workspace/src/workspace.rs 🔗

@@ -8085,6 +8085,61 @@ pub fn with_active_or_new_workspace(
     }
 }
 
+pub async fn restorable_workspace_locations(
+    cx: &mut AsyncApp,
+    app_state: &Arc<AppState>,
+) -> Option<Vec<SerializedWorkspaceLocation>> {
+    let mut restore_behavior = cx
+        .update(|cx| WorkspaceSettings::get(None, cx).restore_on_startup)
+        .ok()?;
+
+    let session_handle = app_state.session.clone();
+    let (last_session_id, last_session_window_stack) = cx
+        .update(|cx| {
+            let session = session_handle.read(cx);
+
+            (
+                session.last_session_id().map(|id| id.to_string()),
+                session.last_session_window_stack(),
+            )
+        })
+        .ok()?;
+
+    if last_session_id.is_none()
+        && matches!(restore_behavior, RestoreOnStartupBehavior::LastSession)
+    {
+        restore_behavior = RestoreOnStartupBehavior::LastWorkspace;
+    }
+
+    match restore_behavior {
+        RestoreOnStartupBehavior::LastWorkspace => last_opened_workspace_location()
+            .await
+            .map(|location| vec![location]),
+        RestoreOnStartupBehavior::LastSession => {
+            if let Some(last_session_id) = last_session_id {
+                let ordered = last_session_window_stack.is_some();
+
+                let mut locations =
+                    last_session_workspace_locations(&last_session_id, last_session_window_stack)
+                        .filter(|locations| !locations.is_empty());
+
+                // Since last_session_window_order returns the windows ordered front-to-back
+                // we need to open the window that was frontmost last.
+                if ordered {
+                    if let Some(locations) = locations.as_mut() {
+                        locations.reverse();
+                    }
+                }
+
+                locations
+            } else {
+                None
+            }
+        }
+        _ => None,
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use std::{cell::RefCell, rc::Rc};

crates/zed/src/main.rs 🔗

@@ -46,7 +46,7 @@ use util::{ConnectionResult, ResultExt, TryFutureExt, maybe};
 use uuid::Uuid;
 use welcome::{FIRST_OPEN, show_welcome_view};
 use workspace::{
-    AppState, SerializedWorkspaceLocation, Toast, Workspace, WorkspaceSettings, WorkspaceStore,
+    AppState, SerializedWorkspaceLocation, Toast, Workspace, WorkspaceStore,
     notifications::NotificationId,
 };
 use zed::{
@@ -956,7 +956,7 @@ async fn installation_id() -> Result<IdType> {
 }
 
 async fn restore_or_create_workspace(app_state: Arc<AppState>, cx: &mut AsyncApp) -> Result<()> {
-    if let Some(locations) = restorable_workspace_locations(cx, &app_state).await {
+    if let Some(locations) = workspace::restorable_workspace_locations(cx, &app_state).await {
         let mut tasks = Vec::new();
 
         for location in locations {
@@ -1077,68 +1077,6 @@ async fn restore_or_create_workspace(app_state: Arc<AppState>, cx: &mut AsyncApp
     Ok(())
 }
 
-pub(crate) async fn restorable_workspace_locations(
-    cx: &mut AsyncApp,
-    app_state: &Arc<AppState>,
-) -> Option<Vec<SerializedWorkspaceLocation>> {
-    let mut restore_behavior = cx
-        .update(|cx| WorkspaceSettings::get(None, cx).restore_on_startup)
-        .ok()?;
-
-    let session_handle = app_state.session.clone();
-    let (last_session_id, last_session_window_stack) = cx
-        .update(|cx| {
-            let session = session_handle.read(cx);
-
-            (
-                session.last_session_id().map(|id| id.to_string()),
-                session.last_session_window_stack(),
-            )
-        })
-        .ok()?;
-
-    if last_session_id.is_none()
-        && matches!(
-            restore_behavior,
-            workspace::RestoreOnStartupBehavior::LastSession
-        )
-    {
-        restore_behavior = workspace::RestoreOnStartupBehavior::LastWorkspace;
-    }
-
-    match restore_behavior {
-        workspace::RestoreOnStartupBehavior::LastWorkspace => {
-            workspace::last_opened_workspace_location()
-                .await
-                .map(|location| vec![location])
-        }
-        workspace::RestoreOnStartupBehavior::LastSession => {
-            if let Some(last_session_id) = last_session_id {
-                let ordered = last_session_window_stack.is_some();
-
-                let mut locations = workspace::last_session_workspace_locations(
-                    &last_session_id,
-                    last_session_window_stack,
-                )
-                .filter(|locations| !locations.is_empty());
-
-                // Since last_session_window_order returns the windows ordered front-to-back
-                // we need to open the window that was frontmost last.
-                if ordered {
-                    if let Some(locations) = locations.as_mut() {
-                        locations.reverse();
-                    }
-                }
-
-                locations
-            } else {
-                None
-            }
-        }
-        _ => None,
-    }
-}
-
 fn init_paths() -> HashMap<io::ErrorKind, Vec<&'static Path>> {
     [
         paths::config_dir(),

crates/zed/src/zed/open_listener.rs 🔗

@@ -1,5 +1,4 @@
 use crate::handle_open_request;
-use crate::restorable_workspace_locations;
 use anyhow::{Context as _, Result, anyhow};
 use cli::{CliRequest, CliResponse, ipc::IpcSender};
 use cli::{IpcHandshake, ipc};
@@ -366,7 +365,7 @@ async fn open_workspaces(
         if open_new_workspace == Some(true) {
             Vec::new()
         } else {
-            let locations = restorable_workspace_locations(cx, &app_state).await;
+            let locations = workspace::restorable_workspace_locations(cx, &app_state).await;
             locations.unwrap_or_default()
         }
     } else {