feat(vscode): support multi-root workspaces (#1493)
Add proper VS Code multi-root workspace support. New sessions now start in the workspace folder the user chooses instead of always using the first folder. The VS Code sidebar now shows one shared flat session list for the currently opened workspace folders, keeps that list synced when folders are added or removed, and excludes sessions from worktrees unless that worktree is opened as a workspace folder. Also keep the OpenCode server process independent from a specific workspace folder so changing the selected folder does not restart or interrupt existing sessions.
This commit is contained in:
committed by
GitHub
parent
49a1424e5f
commit
7b33805ea0
@@ -61,6 +61,7 @@ import { type SessionGroup, type SessionNode } from './sidebar/types';
|
||||
import {
|
||||
deriveActiveNowSessions,
|
||||
deriveLiveActiveNowSessions,
|
||||
getSessionUpdatedAtMs,
|
||||
} from './sidebar/activitySections';
|
||||
import { useActiveNowStore } from '@/stores/useActiveNowStore';
|
||||
import { useSessionPinnedStore } from '@/stores/useSessionPinnedStore';
|
||||
@@ -84,6 +85,8 @@ const PROJECT_COLLAPSE_STORAGE_KEY = 'oc.sessions.projectCollapse';
|
||||
const GROUP_ORDER_STORAGE_KEY = 'oc.sessions.groupOrder';
|
||||
const GROUP_COLLAPSE_STORAGE_KEY = 'oc.sessions.groupCollapse';
|
||||
const PROJECT_ACTIVE_SESSION_STORAGE_KEY = 'oc.sessions.activeSessionByProject';
|
||||
const VSCODE_RECENT_INITIAL_SESSION_COUNT = 20;
|
||||
const VSCODE_RECENT_SESSION_BATCH_SIZE = 20;
|
||||
// v2 key holds composite "${renderContext}:${active|archived}:${sessionId}"
|
||||
// entries so the same session in different render contexts (e.g. "Recent"
|
||||
// and a project's root) has independent expand state. v1 held bare session
|
||||
@@ -122,12 +125,16 @@ type PrIndicator = {
|
||||
const buildKnownSessionDirectories = (
|
||||
projects: Array<{ path: string }>,
|
||||
availableWorktreesByProject: Map<string, WorktreeMetadata[]>,
|
||||
options?: { includeWorktrees?: boolean },
|
||||
): Set<string> => {
|
||||
const directories = new Set<string>();
|
||||
for (const project of projects) {
|
||||
const normalized = normalizePath(project.path)?.toLowerCase();
|
||||
if (normalized) directories.add(normalized);
|
||||
}
|
||||
if (options?.includeWorktrees === false) {
|
||||
return directories;
|
||||
}
|
||||
for (const worktrees of availableWorktreesByProject.values()) {
|
||||
for (const worktree of worktrees) {
|
||||
const normalized = normalizePath(worktree.path)?.toLowerCase();
|
||||
@@ -137,11 +144,15 @@ const buildKnownSessionDirectories = (
|
||||
return directories;
|
||||
};
|
||||
|
||||
const isKnownActiveSessionDirectory = (session: Session, knownDirectories: Set<string>): boolean => {
|
||||
const isKnownActiveSessionDirectory = (
|
||||
session: Session,
|
||||
knownDirectories: Set<string>,
|
||||
options?: { allowUnknownDirectory?: boolean; allowEmptyDirectorySet?: boolean },
|
||||
): boolean => {
|
||||
if (session.time?.archived) return true;
|
||||
const directory = normalizePath(resolveGlobalSessionDirectory(session))?.toLowerCase();
|
||||
if (!directory) return true;
|
||||
if (knownDirectories.size === 0) return true;
|
||||
if (!directory) return options?.allowUnknownDirectory ?? true;
|
||||
if (knownDirectories.size === 0) return options?.allowEmptyDirectorySet ?? true;
|
||||
return knownDirectories.has(directory);
|
||||
};
|
||||
|
||||
@@ -305,6 +316,7 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
const sync = useSync();
|
||||
const liveSessions = useAllLiveSessions();
|
||||
const liveSessionStatuses = useAllSessionStatuses();
|
||||
const isVSCode = React.useMemo(() => isVSCodeRuntime(), []);
|
||||
const hasLoadedGlobalSessions = useGlobalSessionsStore((state) => state.hasLoaded);
|
||||
const globalActiveSessions = useGlobalSessionsStore((state) => state.activeSessions);
|
||||
const archivedSessions = useGlobalSessionsStore((state) => state.archivedSessions);
|
||||
@@ -332,8 +344,8 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
})));
|
||||
|
||||
const knownSessionDirectories = React.useMemo(
|
||||
() => buildKnownSessionDirectories(projects, availableWorktreesByProject),
|
||||
[availableWorktreesByProject, projects],
|
||||
() => buildKnownSessionDirectories(projects, availableWorktreesByProject, { includeWorktrees: !isVSCode }),
|
||||
[availableWorktreesByProject, isVSCode, projects],
|
||||
);
|
||||
|
||||
const sessions = React.useMemo(() => {
|
||||
@@ -351,8 +363,11 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
merged.push(session);
|
||||
});
|
||||
|
||||
return merged.filter((session) => isKnownActiveSessionDirectory(session, knownSessionDirectories));
|
||||
}, [globalActiveSessions, knownSessionDirectories, liveSessions]);
|
||||
return merged.filter((session) => isKnownActiveSessionDirectory(session, knownSessionDirectories, {
|
||||
allowUnknownDirectory: !isVSCode,
|
||||
allowEmptyDirectorySet: !isVSCode,
|
||||
}));
|
||||
}, [globalActiveSessions, isVSCode, knownSessionDirectories, liveSessions]);
|
||||
|
||||
const syncSessionStructureSignature = React.useMemo(
|
||||
() => liveSessions
|
||||
@@ -452,7 +467,6 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
|
||||
const isDesktopShellRuntime = React.useMemo(() => isDesktopShell(), []);
|
||||
|
||||
const isVSCode = React.useMemo(() => isVSCodeRuntime(), []);
|
||||
const { isTablet } = useDeviceInfo();
|
||||
const alwaysShowSidebarActions = mobileVariant || isTablet;
|
||||
|
||||
@@ -804,6 +818,9 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
const directories = new Set<string>();
|
||||
normalizedProjects.forEach((project) => {
|
||||
if (project.normalizedPath) directories.add(project.normalizedPath);
|
||||
if (isVSCode) {
|
||||
return;
|
||||
}
|
||||
const worktrees = availableWorktreesByProject.get(project.normalizedPath) ?? [];
|
||||
worktrees.forEach((worktree) => {
|
||||
const directory = normalizePath(worktree.path);
|
||||
@@ -811,7 +828,7 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
});
|
||||
});
|
||||
return [...directories].sort();
|
||||
}, [availableWorktreesByProject, normalizedProjects]);
|
||||
}, [availableWorktreesByProject, isVSCode, normalizedProjects]);
|
||||
|
||||
const knownProjectSessionDirectoriesRef = React.useRef<Set<string> | null>(null);
|
||||
React.useEffect(() => {
|
||||
@@ -819,6 +836,9 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
const previousDirectories = knownProjectSessionDirectoriesRef.current;
|
||||
knownProjectSessionDirectoriesRef.current = nextDirectories;
|
||||
if (!previousDirectories) {
|
||||
if (isVSCode && projectSessionDirectories.length > 0) {
|
||||
void refreshGlobalSessionsForDirectories(projectSessionDirectories, syncSessionsSnapshotRef.current);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -828,7 +848,7 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
}
|
||||
|
||||
void refreshGlobalSessionsForDirectories(addedDirectories, syncSessionsSnapshotRef.current);
|
||||
}, [projectSessionDirectories]);
|
||||
}, [isVSCode, projectSessionDirectories]);
|
||||
|
||||
const { github } = useRuntimeAPIs();
|
||||
const githubAuthStatus = useGitHubAuthStore((state) => state.status);
|
||||
@@ -1001,13 +1021,28 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
const showArchivedSessions = useSessionDisplayStore((state) => state.showArchivedSessions);
|
||||
|
||||
const activeNowSessions = React.useMemo(() => {
|
||||
if (!showRecentSection) {
|
||||
if (!showRecentSection || isVSCode) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return deriveActiveNowSessions(activeNowEntries, new Map(sessions.map((session) => [session.id, session])))
|
||||
.sort((a, b) => compareSessionsByPinnedAndTime(a, b, pinnedSessionIds));
|
||||
}, [activeNowEntries, pinnedSessionIds, sessions, showRecentSection]);
|
||||
}, [activeNowEntries, isVSCode, pinnedSessionIds, sessions, showRecentSection]);
|
||||
|
||||
const vscodeSharedSessions = React.useMemo(() => {
|
||||
if (!isVSCode) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return sessions
|
||||
.filter((session) => !session.time?.archived)
|
||||
.filter((session) => !(session as Session & { parentID?: string | null }).parentID)
|
||||
.sort((left, right) => {
|
||||
const timeDelta = getSessionUpdatedAtMs(right) - getSessionUpdatedAtMs(left);
|
||||
if (timeDelta !== 0) return timeDelta;
|
||||
return right.id.localeCompare(left.id);
|
||||
});
|
||||
}, [isVSCode, sessions]);
|
||||
|
||||
const liveActiveSessions = React.useMemo(() => {
|
||||
if (!showRecentSection) {
|
||||
@@ -1041,25 +1076,50 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
// Prefetch is wired below, after recentSessionIds is computed.
|
||||
|
||||
const activitySections = React.useMemo(() => {
|
||||
if (!showRecentSection) {
|
||||
if (!isVSCode && !showRecentSection) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const recentSessions = isVSCode ? vscodeSharedSessions : activeNowSessions;
|
||||
|
||||
const toItem = (session: Session) => {
|
||||
const existing = sessionSidebarMetaById.get(session.id);
|
||||
const sessionDirectory = normalizePath((session as Session & { directory?: string | null }).directory ?? null);
|
||||
const node = existing?.node ?? { session, children: [], worktree: null };
|
||||
const filteredNodes = hasSessionSearchQuery
|
||||
? filterSessionNodesForSearch([node], normalizedSessionSearchQuery)
|
||||
: [node];
|
||||
const filteredNode = filteredNodes[0];
|
||||
if (!filteredNode) {
|
||||
return null;
|
||||
}
|
||||
const secondaryMeta = existing?.secondaryMeta
|
||||
? {
|
||||
projectLabel: existing.secondaryMeta.projectLabel,
|
||||
branchLabel: isVSCode ? null : existing.secondaryMeta.branchLabel,
|
||||
}
|
||||
: null;
|
||||
return {
|
||||
node: existing?.node ?? { session, children: [], worktree: null },
|
||||
node: filteredNode,
|
||||
projectId: existing?.projectId ?? null,
|
||||
groupDirectory: existing?.groupDirectory ?? sessionDirectory,
|
||||
secondaryMeta: existing?.secondaryMeta ?? null,
|
||||
secondaryMeta,
|
||||
};
|
||||
};
|
||||
|
||||
const items = recentSessions
|
||||
.map(toItem)
|
||||
.filter((item): item is NonNullable<ReturnType<typeof toItem>> => item !== null);
|
||||
|
||||
return [
|
||||
{ key: 'active-now' as const, title: t('sessions.sidebar.activity.recentTitle'), items: activeNowSessions.map(toItem) },
|
||||
{ key: 'active-now' as const, title: t('sessions.sidebar.activity.recentTitle'), items },
|
||||
];
|
||||
}, [activeNowSessions, sessionSidebarMetaById, showRecentSection, t]);
|
||||
}, [activeNowSessions, filterSessionNodesForSearch, hasSessionSearchQuery, isVSCode, normalizedSessionSearchQuery, sessionSidebarMetaById, showRecentSection, t, vscodeSharedSessions]);
|
||||
|
||||
const hasActivitySectionItems = React.useMemo(
|
||||
() => activitySections.some((section) => section.items.length > 0),
|
||||
[activitySections],
|
||||
);
|
||||
|
||||
|
||||
const recentSessionIds = React.useMemo(() => {
|
||||
@@ -1399,10 +1459,13 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
],
|
||||
);
|
||||
|
||||
const topContent = showRecentSection && !isVSCode && !hasSessionSearchQuery ? (
|
||||
const topContent = (isVSCode || (showRecentSection && !hasSessionSearchQuery)) ? (
|
||||
<SidebarActivitySections
|
||||
sections={activitySections}
|
||||
renderSessionNode={renderSessionNode}
|
||||
variant={isVSCode ? 'flat' : 'section'}
|
||||
initialVisibleCount={isVSCode ? VSCODE_RECENT_INITIAL_SESSION_COUNT : undefined}
|
||||
batchSize={isVSCode ? VSCODE_RECENT_SESSION_BATCH_SIZE : undefined}
|
||||
/>
|
||||
) : null;
|
||||
const isInlineEditing = Boolean(renamingFolderId || editingId || editingProjectDialogId);
|
||||
@@ -1619,6 +1682,8 @@ export const SessionSidebar: React.FC<SessionSidebarProps> = ({
|
||||
|
||||
<SidebarProjectsList
|
||||
topContent={topContent}
|
||||
sharedSessionsOnly={isVSCode}
|
||||
hasSharedSessions={hasActivitySectionItems}
|
||||
sectionsForRender={sectionsForSidebarRender}
|
||||
projectSections={projectSections}
|
||||
activeProjectId={activeProjectId}
|
||||
|
||||
Reference in New Issue
Block a user