From 9884ba5d46e6f2b2248ad7053a3c76247a88db1c Mon Sep 17 00:00:00 2001 From: CharlesChen0823 Date: Thu, 2 Jan 2025 13:49:48 +0800 Subject: [PATCH 1/2] init --- crates/workspace/src/pane.rs | 18 +++++++++++++-- crates/workspace/src/workspace.rs | 37 ++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 33808ffb3cfe3..2fe63caabab3d 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -1016,6 +1016,17 @@ impl Pane { self.items.get(self.active_item_index).cloned() } + pub fn active_nearest_item(&self) -> Option> { + if self.items_len() == 1 { + return None; + } + let index = match self.active_item_index { + 0 => self.active_item_index + 1, + _ => self.active_item_index - 1, + }; + self.items.get(index).cloned() + } + pub fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option> { self.items .get(self.active_item_index)? @@ -1493,6 +1504,7 @@ impl Pane { item_ix, &*item_to_close, save_intent, + None, &mut cx, ) .await? @@ -1657,6 +1669,7 @@ impl Pane { item_ix: usize, item: &dyn ItemHandle, save_intent: SaveIntent, + relative_project_path: Option, cx: &mut AsyncWindowContext, ) -> Result { const CONFLICT_MESSAGE: &str = @@ -1806,8 +1819,9 @@ impl Pane { .await?; } else if can_save_as { let abs_path = pane.update(cx, |pane, cx| { - pane.workspace - .update(cx, |workspace, cx| workspace.prompt_for_new_path(cx)) + pane.workspace.update(cx, |workspace, cx| { + workspace.prompt_for_new_path(relative_project_path, cx) + }) })??; if let Some(abs_path) = abs_path.await.ok().flatten() { pane.update(cx, |pane, cx| { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 1e285276ccb6b..6dfa11798200b 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -1582,6 +1582,7 @@ impl Workspace { pub fn prompt_for_new_path( &mut self, + relative_project_path: Option, cx: &mut ViewContext, ) -> oneshot::Receiver> { if (self.project.read(cx).is_via_collab() || self.project.read(cx).is_via_ssh()) @@ -1595,8 +1596,17 @@ impl Workspace { let start_abs_path = self .project .update(cx, |project, cx| { - let worktree = project.visible_worktrees(cx).next()?; - Some(worktree.read(cx).as_local()?.abs_path().to_path_buf()) + let relative_path = relative_project_path.and_then(|relative_path| { + project + .absolute_path(&relative_path, cx) + .and_then(|p| p.parent().map(PathBuf::from)) + }); + if relative_path.is_none() { + let worktree = project.visible_worktrees(cx).next()?; + Some(worktree.read(cx).as_local()?.abs_path().to_path_buf()) + } else { + relative_path + } }) .unwrap_or_else(|| Path::new("").into()); @@ -1936,6 +1946,7 @@ impl Workspace { ix, &*item, save_intent, + None, &mut cx, ) .await? @@ -2184,6 +2195,13 @@ impl Workspace { self.active_item(cx).and_then(|item| item.project_path(cx)) } + fn active_nearest_project_path(&self, cx: &AppContext) -> Option { + self.active_pane() + .read(cx) + .active_nearest_item() + .and_then(|item| item.project_path(cx)) + } + pub fn save_active_item( &mut self, save_intent: SaveIntent, @@ -2194,12 +2212,21 @@ impl Workspace { let item_ix = pane.read(cx).active_item_index(); let item = pane.read(cx).active_item(); let pane = pane.downgrade(); + let relative_project_path = self.active_nearest_project_path(cx); cx.spawn(|mut cx| async move { if let Some(item) = item { - Pane::save_item(project, &pane, item_ix, item.as_ref(), save_intent, &mut cx) - .await - .map(|_| ()) + Pane::save_item( + project, + &pane, + item_ix, + item.as_ref(), + save_intent, + relative_project_path, + &mut cx, + ) + .await + .map(|_| ()) } else { Ok(()) } From 7ac933f9effc9234fcbb0136be9159e6cc5af467 Mon Sep 17 00:00:00 2001 From: CharlesChen0823 Date: Sat, 4 Jan 2025 19:31:15 +0800 Subject: [PATCH 2/2] add --- crates/workspace/src/workspace.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 6dfa11798200b..9f491c002299f 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -2196,10 +2196,21 @@ impl Workspace { } fn active_nearest_project_path(&self, cx: &AppContext) -> Option { - self.active_pane() + let p = self + .active_pane() .read(cx) .active_nearest_item() - .and_then(|item| item.project_path(cx)) + .and_then(|item| item.project_path(cx)); + if p.is_some() { + return p; + } + self.last_active_center_pane.clone().and_then(|p| { + p.upgrade().and_then(|pane| { + pane.read(cx) + .active_item() + .and_then(|item| item.project_path(cx)) + }) + }) } pub fn save_active_item(