sidebar: Show Delta checkouts as worktrees of their source project

A Delta checkout is now a worktree of its source project in the agents sidebar, instead of a project of its own with the same name.

- Thread rows and the project menus label a checkout after its Delta namespace.
- Saved threads and terminals of an open checkout follow it into the source project, and a header of the old per-checkout project is removed once nothing is listed under it.
- A project keeps its header while closed threads of another checkout are listed under it, for example after "Add Folder to Project" in one checkout.
- "Create New Worktree…" names the repository it creates in when the open checkouts of a project are different repositories, and the default branch is cached per repository.
This commit is contained in:
Kirill Bulatov
2026-11-18 21:53:23 +02:00
parent 1f07d7302a
commit 035a21520c
4 changed files with 1854 additions and 135 deletions
+476 -122
View File
@@ -63,6 +63,7 @@ use ui::{
use unicode_segmentation::UnicodeSegmentation as _;
use util::ResultExt as _;
use util::path_list::PathList;
use util::paths::{PathExt as _, PathStyle};
use workspace::{
CloseWindow, FocusWorkspaceSidebar, MoveProjectDown, MoveProjectUp, MultiWorkspace,
MultiWorkspaceEvent, NextProject, NextThread, Open, OpenMode, PreviousProject, PreviousThread,
@@ -634,6 +635,13 @@ struct WorkspaceMenuWorktreeLabel {
}
impl WorkspaceMenuWorktreeLabel {
fn text(&self) -> String {
match &self.secondary_name {
Some(secondary_name) => format!("{}/{secondary_name}", self.primary_name),
None => self.primary_name.to_string(),
}
}
fn render(&self) -> impl IntoElement {
h_flex()
.min_w_0()
@@ -649,14 +657,207 @@ impl WorkspaceMenuWorktreeLabel {
}
}
fn legacy_delta_main_paths(worktree_paths: &WorktreePaths) -> Option<PathList> {
let legacy_main_path = |main_path: &PathBuf, folder_path: &PathBuf| {
let clone = worktree_paths.delta_clone(folder_path)?;
let legacy_main_path =
project::repo_identity_path(&clone.path, util::paths::PathStyle::local());
(legacy_main_path != main_path.as_path()).then(|| legacy_main_path.to_path_buf())
};
worktree_paths
.ordered_pairs()
.any(|(main_path, folder_path)| legacy_main_path(main_path, folder_path).is_some())
.then(|| {
PathList::new(
&worktree_paths
.ordered_pairs()
.map(|(main_path, folder_path)| {
legacy_main_path(main_path, folder_path)
.unwrap_or_else(|| main_path.clone())
})
.collect::<Vec<_>>(),
)
})
}
fn regroup_unused_legacy_delta_project_groups(
multi_workspace: &Entity<MultiWorkspace>,
cx: &mut App,
) {
let thread_store = ThreadMetadataStore::global(cx);
let terminal_store = TerminalThreadMetadataStore::global(cx);
if !thread_store.read(cx).is_loaded() || !terminal_store.read(cx).is_loaded() {
return;
}
let regrouped = {
let multi_workspace = multi_workspace.read(cx);
let thread_store = thread_store.read(cx);
let terminal_store = terminal_store.read(cx);
let mut group_paths_by_legacy_paths = HashMap::<PathList, Vec<PathList>>::default();
let saved_paths = thread_store
.entries()
.filter(|thread| thread.remote_connection.is_none() && !thread.archived)
.map(|thread| &thread.worktree_paths)
.chain(
terminal_store
.entries()
.filter(|terminal| terminal.remote_connection.is_none())
.map(|terminal| &terminal.worktree_paths),
)
.filter_map(|worktree_paths| {
Some((
legacy_delta_main_paths(worktree_paths)?,
worktree_paths.main_worktree_path_list().clone(),
))
});
let open_paths = multi_workspace
.workspaces()
.filter(|workspace| workspace.read(cx).project().read(cx).is_local())
.map(|workspace| {
(
git_project_group_key(workspace, cx).path_list().clone(),
workspace.read(cx).project_group_key(cx).path_list().clone(),
)
})
.filter(|(git_paths, group_paths)| git_paths != group_paths);
for (legacy_paths, group_paths) in saved_paths.chain(open_paths) {
let group_paths_for_legacy_paths =
group_paths_by_legacy_paths.entry(legacy_paths).or_default();
if !group_paths_for_legacy_paths.contains(&group_paths) {
group_paths_for_legacy_paths.push(group_paths);
}
}
multi_workspace
.project_group_keys()
.into_iter()
.filter(|key| key.host().is_none())
.filter_map(|key| {
let group_paths = group_paths_by_legacy_paths.get(key.path_list())?;
let is_used = multi_workspace
.workspaces_for_project_group(&key, cx)
.next()
.is_some()
|| thread_store
.entries_for_main_worktree_path(key.path_list(), None)
.next()
.is_some()
|| terminal_store
.entries_for_main_worktree_path(key.path_list(), None)
.next()
.is_some();
let mut group_keys = group_paths
.iter()
.map(|group_paths| ProjectGroupKey::new(None, group_paths.clone()))
.collect::<Vec<_>>();
group_keys.sort_by(|a, b| a.path_list().paths().cmp(b.path_list().paths()));
(!is_used).then_some((key, group_keys))
})
.collect::<Vec<_>>()
};
if regrouped.is_empty() {
return;
}
multi_workspace.update(cx, |multi_workspace, cx| {
for (legacy_key, group_keys) in regrouped {
multi_workspace.replace_project_group(&legacy_key, group_keys, cx);
}
});
}
fn git_project_group_key(workspace: &Entity<Workspace>, cx: &App) -> ProjectGroupKey {
let project = workspace.read(cx).project().read(cx);
ProjectGroupKey::from_worktree_paths(
&project.git_worktree_paths(cx),
project.remote_connection_options(cx).cloned(),
)
}
fn worktree_creation_base_workspaces(
project_group_key: &ProjectGroupKey,
active_workspace: Option<&Entity<Workspace>>,
open_workspaces: &[Entity<Workspace>],
cx: &App,
) -> Vec<(Entity<Workspace>, bool)> {
let mut git_project_group_keys = Vec::new();
let mut base_workspaces = Vec::new();
if let Some(active_workspace) =
active_workspace.filter(|workspace| open_workspaces.contains(workspace))
{
git_project_group_keys.push(git_project_group_key(active_workspace, cx));
base_workspaces.push(active_workspace.clone());
} else {
for workspace in open_workspaces {
let git_project_group_key = git_project_group_key(workspace, cx);
if !git_project_group_keys.contains(&git_project_group_key) {
git_project_group_keys.push(git_project_group_key);
base_workspaces.push(workspace.clone());
}
}
}
let names_every_base_workspace = base_workspaces.len() > 1;
base_workspaces
.into_iter()
.zip(git_project_group_keys)
.map(|(base_workspace, git_project_group_key)| {
let is_named =
names_every_base_workspace || git_project_group_key != *project_group_key;
(base_workspace, is_named)
})
.collect()
}
fn workspace_menu_worktree_labels(
workspace: &Entity<Workspace>,
cx: &App,
) -> Vec<WorkspaceMenuWorktreeLabel> {
worktree_menu_labels(root_worktree_names(workspace, cx))
}
fn worktree_menu_labels(
root_worktree_names: Vec<RootWorktreeName>,
) -> Vec<WorkspaceMenuWorktreeLabel> {
let show_folder_name = root_worktree_names.len() > 1;
root_worktree_names
.into_iter()
.map(|root| match root.worktree_name {
Some(worktree_name) if show_folder_name || root.in_checkout_subfolder => {
WorkspaceMenuWorktreeLabel {
icon: Some(IconName::GitWorktree),
primary_name: root.folder_name,
secondary_name: Some(worktree_name),
}
}
Some(worktree_name) => WorkspaceMenuWorktreeLabel {
icon: Some(IconName::GitWorktree),
primary_name: worktree_name,
secondary_name: None,
},
None => WorkspaceMenuWorktreeLabel {
icon: None,
primary_name: root.folder_name,
secondary_name: None,
},
})
.collect()
}
struct RootWorktreeName {
folder_name: SharedString,
worktree_name: Option<SharedString>,
in_checkout_subfolder: bool,
}
struct RootCheckout {
path: PathBuf,
common_dir: PathBuf,
is_linked_worktree: bool,
is_delta_checkout: bool,
}
fn root_worktree_names(workspace: &Entity<Workspace>, cx: &App) -> Vec<RootWorktreeName> {
let root_paths = workspace.read(cx).root_paths(cx);
let show_folder_name = root_paths.len() > 1;
let project = workspace.read(cx).project().clone();
let (path_style, repository_snapshots) = {
let (path_style, repository_snapshots, worktree_paths, git_worktree_paths) = {
let project = project.read(cx);
let path_style = project.path_style(cx);
let repository_snapshots = project
@@ -664,57 +865,98 @@ fn workspace_menu_worktree_labels(
.values()
.map(|repo| repo.read(cx).snapshot())
.collect::<Vec<_>>();
(path_style, repository_snapshots)
(
path_style,
repository_snapshots,
project.worktree_paths(cx),
project.git_worktree_paths(cx),
)
};
root_paths
.into_iter()
.map(|root_path| {
let root_path = root_path.as_ref();
let folder_name = path_style
.file_name(root_path)
.map(|name| SharedString::from(name.to_string_lossy().into_owned()))
.unwrap_or_default();
let repository_snapshot = repository_snapshots
let is_delta_checkout = worktree_paths
.ordered_pairs()
.zip(git_worktree_paths.ordered_pairs())
.any(|((main_path, folder_path), (git_main_path, _))| {
folder_path.as_path() == root_path.as_ref() && main_path != git_main_path
});
let checkout = repository_snapshots
.iter()
.find(|snapshot| snapshot.work_directory_abs_path.as_ref() == root_path);
if let Some(snapshot) = repository_snapshot {
let worktree_name = if snapshot.is_linked_worktree() {
project::linked_worktree_short_name(
snapshot.linked_worktree_anchor_path(),
root_path,
snapshot.path_style,
)
.unwrap_or_else(|| folder_name.clone())
} else {
"main".into()
};
if show_folder_name {
WorkspaceMenuWorktreeLabel {
icon: Some(IconName::GitWorktree),
primary_name: folder_name,
secondary_name: Some(worktree_name),
}
} else {
WorkspaceMenuWorktreeLabel {
icon: Some(IconName::GitWorktree),
primary_name: worktree_name,
secondary_name: None,
}
}
} else {
WorkspaceMenuWorktreeLabel {
icon: None,
primary_name: folder_name,
secondary_name: None,
}
}
.filter(|snapshot| {
path_style.starts_with(&root_path, &snapshot.work_directory_abs_path)
})
.max_by_key(|snapshot| snapshot.work_directory_abs_path.as_os_str().len())
.map(|snapshot| RootCheckout {
path: snapshot.work_directory_abs_path.to_path_buf(),
common_dir: snapshot.common_dir_abs_path.to_path_buf(),
is_linked_worktree: snapshot.is_linked_worktree(),
is_delta_checkout,
});
root_worktree_name(&root_path, checkout.as_ref(), path_style)
})
.collect()
}
fn root_worktree_name(
root_path: &Path,
checkout: Option<&RootCheckout>,
path_style: PathStyle,
) -> RootWorktreeName {
let folder_name = path_style
.file_name(root_path)
.map(|name| SharedString::from(name.to_string_lossy().into_owned()))
.unwrap_or_default();
let Some(checkout) =
checkout.filter(|checkout| checkout.path == root_path || checkout.is_delta_checkout)
else {
return RootWorktreeName {
folder_name,
worktree_name: None,
in_checkout_subfolder: false,
};
};
let checkout_name = path_style
.file_name(&checkout.path)
.map(|name| SharedString::from(name.to_string_lossy().into_owned()))
.unwrap_or_else(|| folder_name.clone());
let worktree_name = if checkout.is_delta_checkout {
project::delta_checkout_short_name(
&project::DeltaClone {
path: checkout.common_dir.clone(),
is_linked_worktree: checkout.is_linked_worktree,
checkout_path: Some(checkout.path.clone()),
},
&checkout.path,
path_style,
)
.unwrap_or(checkout_name)
} else if checkout.is_linked_worktree {
project::linked_worktree_short_name(
project::repo_identity_path(&checkout.common_dir, path_style),
&checkout.path,
path_style,
)
.unwrap_or(checkout_name)
} else {
"main".into()
};
RootWorktreeName {
in_checkout_subfolder: checkout.path != root_path,
folder_name,
worktree_name: Some(worktree_name),
}
}
fn root_paths_tooltip(root_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> SharedString {
root_paths
.into_iter()
.map(|root_path| root_path.as_ref().compact().to_string_lossy().into_owned())
.join("\n")
.into()
}
fn defer_menu_dismissal(menu: &WeakEntity<ContextMenu>, window: &mut Window, cx: &mut App) {
let menu = menu.clone();
window.defer(cx, move |_, cx| {
@@ -1123,8 +1365,15 @@ impl Sidebar {
!old_paths
.ordered_pairs()
.any(|(old_main, old_folder)| old_main == *main && old_folder == *folder)
|| old_paths.delta_clone(folder) != new_paths.delta_clone(folder)
})
.map(|(main, folder)| {
(
main.clone(),
folder.clone(),
new_paths.delta_clone(folder).cloned(),
)
})
.map(|(m, f)| (m.clone(), f.clone()))
.collect();
let new_folder_paths = new_paths.folder_path_list();
@@ -1141,8 +1390,9 @@ impl Sidebar {
let remote_connection = project.read(cx).remote_connection_options(cx).cloned();
let apply_path_changes = |paths: &mut WorktreePaths| {
for (main_path, folder_path) in &added_pairs {
for (main_path, folder_path, clone) in &added_pairs {
paths.add_path(main_path, folder_path);
paths.set_delta_clone(folder_path, clone.clone());
}
for path in &removed_folder_paths {
paths.remove_folder_path(path);
@@ -1166,6 +1416,24 @@ impl Sidebar {
});
}
fn move_saved_entries_of_open_checkouts(
&mut self,
multi_workspace: &Entity<MultiWorkspace>,
cx: &mut Context<Self>,
) {
let projects = multi_workspace
.read(cx)
.workspaces()
.map(|workspace| workspace.read(cx).project().clone())
.collect::<Vec<_>>();
for project in projects {
let git_worktree_paths = project.read(cx).git_worktree_paths(cx);
if git_worktree_paths != project.read(cx).worktree_paths(cx) {
self.move_entry_paths(&project, &git_worktree_paths, cx);
}
}
}
fn subscribe_to_agent_panel(
&mut self,
workspace: &Entity<Workspace>,
@@ -2077,6 +2345,9 @@ impl Sidebar {
return;
}
self.move_saved_entries_of_open_checkouts(&multi_workspace, cx);
regroup_unused_legacy_delta_project_groups(&multi_workspace, cx);
let had_notifications = self.has_notifications(cx);
let previous_shapes: Vec<EntryShape> =
self.entry_shapes(multi_workspace.read(cx)).collect();
@@ -2675,29 +2946,38 @@ impl Sidebar {
move |mut menu, _window, cx| {
menu = menu.header("New Thread In…");
for (workspace, labels) in open_workspaces
for (workspace_index, (workspace, labels)) in open_workspaces
.iter()
.cloned()
.zip(workspace_labels.iter().cloned())
.enumerate()
{
let is_active_workspace = active_workspace.as_ref() == Some(&workspace);
let tooltip = root_paths_tooltip(&workspace.read(cx).root_paths(cx));
menu = menu.custom_entry(
move |_window, _cx| {
h_flex()
.w_full()
.gap_2()
.justify_between()
.child(h_flex().min_w_0().gap_1().children(
labels.iter().enumerate().map(|(label_ix, label)| {
h_flex()
.gap_1()
.when(label_ix > 0, |this| {
this.child(Label::new("•").alpha(0.25))
})
.child(label.render())
.into_any_element()
}),
))
.child(
h_flex()
.id(("new-thread-worktree", workspace_index))
.min_w_0()
.gap_1()
.children(labels.iter().enumerate().map(
|(label_ix, label)| {
h_flex()
.gap_1()
.when(label_ix > 0, |this| {
this.child(Label::new("•").alpha(0.25))
})
.child(label.render())
.into_any_element()
},
))
.tooltip(Tooltip::text(tooltip.clone())),
)
.when(is_active_workspace, |this| {
this.child(
Icon::new(IconName::Check)
@@ -2722,23 +3002,44 @@ impl Sidebar {
);
}
let base_workspace = active_workspace
.as_ref()
.filter(|workspace| open_workspaces.contains(workspace))
.cloned()
.or_else(|| open_workspaces.first().cloned());
// Only offer worktree creation when the base project can
// actually create one; otherwise the submenu would expand to
// nothing. Mirrors the picker's `creation_blocked_reason`.
let creation_blocked = base_workspace.as_ref().is_none_or(|base_workspace| {
let project = base_workspace.read(cx).project().read(cx);
project.is_via_collab() || project.repositories(cx).is_empty()
});
if let Some(base_workspace) = base_workspace.filter(|_| !creation_blocked) {
menu = menu.separator().submenu("Create New Worktree…", {
let this = this.clone();
let base_workspaces = worktree_creation_base_workspaces(
&key,
active_workspace.as_ref(),
&open_workspaces,
cx,
);
let mut has_creation_submenu = false;
for (base_workspace, is_named) in base_workspaces {
// Only offer worktree creation when the base project can
// actually create one; otherwise the submenu would expand to
// nothing. Mirrors the picker's `creation_blocked_reason`.
let creation_blocked = {
let project = base_workspace.read(cx).project().read(cx);
project.is_via_collab() || project.repositories(cx).is_empty()
};
if creation_blocked {
continue;
}
let submenu_label = open_workspaces
.iter()
.position(|workspace| *workspace == base_workspace)
.and_then(|workspace_index| workspace_labels.get(workspace_index))
.filter(|_| is_named)
.map_or_else(
|| "Create New Worktree…".to_string(),
|labels| {
format!(
"Create New Worktree in {}…",
labels.iter().map(|label| label.text()).join(" • ")
)
},
);
if !has_creation_submenu {
menu = menu.separator();
has_creation_submenu = true;
}
let this = this.clone();
menu = menu.submenu(submenu_label, {
move |mut submenu, _window, submenu_cx| {
let project = base_workspace.read(submenu_cx).project().clone();
let project_ref = project.read(submenu_cx);
@@ -2752,13 +3053,8 @@ impl Sidebar {
.map(|branch| branch.name().to_string())
});
let default_branch = this
.read_with(submenu_cx, |sidebar, _| {
match sidebar.worktree_default_branches.get(&key) {
Some(DefaultBranchCache::Resolved(branch)) => {
branch.clone()
}
_ => None,
}
.read_with(submenu_cx, |sidebar, cx| {
sidebar.worktree_default_branch(&base_workspace, cx)
})
.ok()
.flatten();
@@ -2805,36 +3101,36 @@ impl Sidebar {
.into_any_element()
}
// Warms `worktree_default_branches` for every project group with at least one
// open workspace. The git query runs off the menu path so the submenu can read
// the result synchronously when it opens. Worktrees of a repository share the
// same default branch, so any workspace in the group yields the same answer.
fn prefetch_worktree_default_branches(&mut self, cx: &mut Context<Self>) {
let Some(multi_workspace) = self.multi_workspace.upgrade() else {
return;
};
let keys: Vec<ProjectGroupKey> = self
.contents
.entries
.iter()
.filter_map(|entry| match entry {
ListEntry::ProjectHeader { key, .. } => Some(key.clone()),
_ => None,
})
.collect();
for key in keys {
if self.worktree_default_branches.contains_key(&key) {
continue;
}
let Some(base) = multi_workspace
.read(cx)
.workspaces_for_project_group(&key, cx)
.next()
.cloned()
else {
continue;
};
self.prefetch_worktree_default_branch(&key, &base, cx);
let workspaces = multi_workspace
.read(cx)
.workspaces()
.cloned()
.collect::<Vec<_>>();
let mut default_branch_keys = HashSet::new();
for workspace in workspaces {
let default_branch_key = git_project_group_key(&workspace, cx);
self.prefetch_worktree_default_branch(&default_branch_key, &workspace, cx);
default_branch_keys.insert(default_branch_key);
}
self.worktree_default_branches
.retain(|default_branch_key, _| default_branch_keys.contains(default_branch_key));
}
fn worktree_default_branch(
&self,
workspace: &Entity<Workspace>,
cx: &App,
) -> Option<RemoteBranchName> {
match self
.worktree_default_branches
.get(&git_project_group_key(workspace, cx))
{
Some(DefaultBranchCache::Resolved(branch)) => branch.clone(),
_ => None,
}
}
@@ -3067,6 +3363,9 @@ impl Sidebar {
let activate_weak_menu = weak_menu.clone();
let close_weak_menu = weak_menu.clone();
let activate_workspace = workspace.clone();
let tooltip = root_paths_tooltip(
&workspace.read(menu_cx).root_paths(menu_cx),
);
menu = menu.custom_entry(
move |_window, _cx| {
@@ -3082,21 +3381,29 @@ impl Sidebar {
.w_full()
.gap_2()
.justify_between()
.child(h_flex().min_w_0().gap_1().children(
workspace_label.iter().enumerate().map(
|(label_ix, label)| {
h_flex()
.gap_1()
.when(label_ix > 0, |this| {
this.child(
Label::new("•").alpha(0.25),
)
})
.child(label.render())
.into_any_element()
},
),
))
.child(
h_flex()
.id(("open-worktree", workspace_index))
.min_w_0()
.gap_1()
.children(
workspace_label.iter().enumerate().map(
|(label_ix, label)| {
h_flex()
.gap_1()
.when(label_ix > 0, |this| {
this.child(
Label::new("•")
.alpha(0.25),
)
})
.child(label.render())
.into_any_element()
},
),
)
.tooltip(Tooltip::text(tooltip.clone())),
)
.when(is_active_workspace, |this| {
this.pr_1().child(
Icon::new(IconName::Check)
@@ -7894,6 +8201,53 @@ impl WorkspaceSidebar for Sidebar {
self.width
}
fn retains_project_group(
&self,
old_worktree_paths: &WorktreePaths,
new_git_worktree_paths: &WorktreePaths,
cx: &App,
) -> bool {
let thread_store = ThreadMetadataStore::global(cx).read(cx);
let terminal_store = TerminalThreadMetadataStore::global(cx).read(cx);
let git_paths = |worktree_paths: &WorktreePaths| {
legacy_delta_main_paths(worktree_paths)
.unwrap_or_else(|| worktree_paths.main_worktree_path_list().clone())
};
let old_group_paths = old_worktree_paths.main_worktree_path_list();
let old_folder_paths = old_worktree_paths.folder_path_list();
let old_git_paths = old_folder_paths
.ordered_paths()
.map(|old_folder_path| {
new_git_worktree_paths
.ordered_pairs()
.find(|(_, folder_path)| *folder_path == old_folder_path)
.map(|(git_main_path, _)| git_main_path.clone())
})
.collect::<Option<Vec<_>>>()
.map(|git_main_paths| PathList::new(&git_main_paths))
.unwrap_or_else(|| git_paths(old_worktree_paths));
let repository_changed = old_git_paths != *new_git_worktree_paths.main_worktree_path_list();
let stays_in_old_group = |worktree_paths: &WorktreePaths| {
worktree_paths.folder_path_list() != old_folder_paths
&& (!repository_changed || git_paths(worktree_paths) != old_git_paths)
};
thread_store
.entries_for_main_worktree_path(old_group_paths, None)
.any(|thread| {
stays_in_old_group(&thread.worktree_paths)
&& (!thread.is_draft()
|| agent_ui::draft_prompt_store::display_label_for_draft(
None,
thread.thread_id,
cx,
)
.is_some())
})
|| terminal_store
.entries_for_main_worktree_path(old_group_paths, None)
.any(|terminal| stays_in_old_group(&terminal.worktree_paths))
}
fn set_width(&mut self, width: Option<Pixels>, cx: &mut Context<Self>) {
// `None` is the reset gesture, which hands the width back to the setting.
self.width_set_by_user = width.is_some();
File diff suppressed because it is too large Load Diff
+101 -12
View File
@@ -7,7 +7,7 @@ use gpui::{
Window, WindowId, actions, deferred, px,
};
pub use project::ProjectGroupKey;
use project::{DisableAiSettings, Project};
use project::{DisableAiSettings, Project, WorktreePaths};
use remote::RemoteConnectionOptions;
use settings::Settings;
pub use settings::SidebarSide;
@@ -127,6 +127,14 @@ pub trait Sidebar: Focusable + Render + EventEmitter<SidebarEvent> + Sized {
fn is_threads_list_view_active(&self) -> bool {
true
}
fn retains_project_group(
&self,
_old_worktree_paths: &WorktreePaths,
_new_git_worktree_paths: &WorktreePaths,
_cx: &App,
) -> bool {
false
}
/// Makes focus reset back to the search editor upon toggling the sidebar from outside
fn prepare_for_focus(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
/// Opens or cycles the thread switcher popup.
@@ -173,6 +181,12 @@ pub trait SidebarHandle: 'static + Send + Sync {
fn cycle_thread(&self, forward: bool, window: &mut Window, cx: &mut App);
fn is_threads_list_view_active(&self, cx: &App) -> bool;
fn retains_project_group(
&self,
old_worktree_paths: &WorktreePaths,
new_git_worktree_paths: &WorktreePaths,
cx: &App,
) -> bool;
fn side(&self, cx: &App) -> SidebarSide;
fn serialized_state(&self, cx: &App) -> Option<String>;
@@ -253,6 +267,16 @@ impl<T: Sidebar> SidebarHandle for Entity<T> {
self.read(cx).is_threads_list_view_active()
}
fn retains_project_group(
&self,
old_worktree_paths: &WorktreePaths,
new_git_worktree_paths: &WorktreePaths,
cx: &App,
) -> bool {
self.read(cx)
.retains_project_group(old_worktree_paths, new_git_worktree_paths, cx)
}
fn side(&self, cx: &App) -> SidebarSide {
self.read(cx).side(cx)
}
@@ -613,7 +637,12 @@ impl MultiWorkspace {
.cloned();
let old_key =
ProjectGroupKey::from_worktree_paths(old_worktree_paths, host);
this.handle_project_group_key_change(&workspace, &old_key, cx);
this.handle_project_group_key_change(
&workspace,
&old_key,
old_worktree_paths,
cx,
);
}
}
_ => {}
@@ -633,6 +662,7 @@ impl MultiWorkspace {
&mut self,
workspace: &Entity<Workspace>,
old_key: &ProjectGroupKey,
old_worktree_paths: &WorktreePaths,
cx: &mut Context<Self>,
) {
if !self.is_workspace_retained(workspace) {
@@ -646,7 +676,13 @@ impl MultiWorkspace {
// The Project already emitted WorktreePathsChanged which the
// sidebar handles for thread migration.
self.rekey_project_group(old_key, &new_key, cx);
let project = workspace.read(cx).project().read(cx);
let new_git_worktree_paths = project.git_worktree_paths(cx);
let sidebar_retains_old_key = project.is_local()
&& self.sidebar.as_ref().is_some_and(|sidebar| {
sidebar.retains_project_group(old_worktree_paths, &new_git_worktree_paths, cx)
});
self.rekey_project_group(old_key, &new_key, sidebar_retains_old_key, cx);
self.serialize(cx);
cx.notify();
}
@@ -709,6 +745,7 @@ impl MultiWorkspace {
&mut self,
old_key: &ProjectGroupKey,
new_key: &ProjectGroupKey,
sidebar_retains_old_key: bool,
cx: &App,
) {
if old_key == new_key {
@@ -727,6 +764,32 @@ impl MultiWorkspace {
return;
}
// If another retained workspace still has the old key (e.g. a
// linked worktree workspace), re-create the old group so it
// remains reachable in the sidebar.
let other_workspace_needs_old_key = self
.held
.iter()
.any(|held| held.pinned && held.workspace.read(cx).project_group_key(cx) == *old_key);
if sidebar_retains_old_key && !other_workspace_needs_old_key {
let old_group = self
.project_groups
.iter()
.enumerate()
.find(|(_, group)| group.key == *old_key)
.map(|(index, group)| (index, group.expanded));
if !new_key_exists && let Some((old_index, expanded)) = old_group {
self.project_groups.insert(
old_index,
ProjectGroupState {
key: new_key.clone(),
expanded,
},
);
}
return;
}
if new_key_exists {
let active_key = self.workspace().read(cx).project_group_key(cx);
if active_key == *new_key {
@@ -743,13 +806,6 @@ impl MultiWorkspace {
}
}
// If another retained workspace still has the old key (e.g. a
// linked worktree workspace), re-create the old group so it
// remains reachable in the sidebar.
let other_workspace_needs_old_key = self
.held
.iter()
.any(|held| held.pinned && held.workspace.read(cx).project_group_key(cx) == *old_key);
if other_workspace_needs_old_key {
self.ensure_project_group_state(old_key.clone());
}
@@ -856,6 +912,31 @@ impl MultiWorkspace {
self.project_groups = restored;
}
pub fn replace_project_group(
&mut self,
old_key: &ProjectGroupKey,
new_keys: Vec<ProjectGroupKey>,
cx: &mut Context<Self>,
) {
let Some(mut index) = self
.project_groups
.iter()
.position(|group| group.key == *old_key)
else {
return;
};
let ProjectGroupState { expanded, .. } = self.project_groups.remove(index);
for key in new_keys {
if self.project_groups.iter().all(|group| group.key != key) {
self.project_groups
.insert(index, ProjectGroupState { key, expanded });
index += 1;
}
}
self.serialize(cx);
cx.notify();
}
pub fn project_group_keys(&self) -> Vec<ProjectGroupKey> {
self.project_groups
.iter()
@@ -1863,9 +1944,17 @@ impl MultiWorkspace {
.iter()
.map(|workspace| workspace.read(cx).project_group_key(cx))
.collect::<Vec<_>>();
let retained_keys = this
.held
.iter()
.filter(|held| held.pinned && !workspaces.contains(&held.workspace))
.map(|held| held.workspace.read(cx).project_group_key(cx))
.collect::<Vec<_>>();
this.project_groups
.retain(|group| !workspace_to_delete_keys.contains(&group.key));
this.project_groups.retain(|group| {
!workspace_to_delete_keys.contains(&group.key)
|| retained_keys.contains(&group.key)
});
cx.emit(MultiWorkspaceEvent::ProjectGroupsChanged);
}
@@ -1041,6 +1041,7 @@ async fn test_close_workspace_prefers_workspace_in_same_project_group(cx: &mut T
let project_a_2 = Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
let project_b = Project::test(fs, ["/project-b".as_ref()], cx).await;
let key_a = project_a_1.read_with(cx, |project, cx| project.project_group_key(cx));
let key_b = project_b.read_with(cx, |project, cx| project.project_group_key(cx));
let (multi_workspace, cx) = setup_multi_workspace(&[project_a_1, project_a_2, project_b], cx);
let (workspace_a_1, workspace_a_2) = multi_workspace.read_with(cx, |multi_workspace, cx| {
@@ -1094,6 +1095,7 @@ async fn test_close_workspace_prefers_workspace_in_same_project_group(cx: &mut T
vec![workspace_a_2],
"only the fallback workspace should remain in project group A"
);
assert_eq!(multi_workspace.project_group_keys(), vec![key_b, key_a]);
});
}