From 910469d4ffa28556cc3e18a41134f23638a2b653 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Wed, 23 Jul 2025 14:45:14 +0530 Subject: [PATCH] move restorable_workspace_locations to workspace --- 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(-) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 4c70c52d5a18bc529498d1b3ac09a3e328208575..cd4adde339419b460112634f9bc7fd0816f30f2f 100644 --- a/crates/workspace/src/workspace.rs +++ b/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, +) -> Option> { + 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}; diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index d0b9c53397d8470ce2b7b1c1223dc9d7df4d1fa0..535be964fd32fc2be9d62cd0ca7d3c9e6c73b7c6 100644 --- a/crates/zed/src/main.rs +++ b/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 { } async fn restore_or_create_workspace(app_state: Arc, 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, cx: &mut AsyncApp Ok(()) } -pub(crate) async fn restorable_workspace_locations( - cx: &mut AsyncApp, - app_state: &Arc, -) -> Option> { - 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> { [ paths::config_dir(), diff --git a/crates/zed/src/zed/open_listener.rs b/crates/zed/src/zed/open_listener.rs index 2fd9b0a68c7c14fd6df0ba2a52c537e34cdd7ceb..70b4dc8d4a930df98f131aff92a5ae9a26adc166 100644 --- a/crates/zed/src/zed/open_listener.rs +++ b/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 {