Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/common/workspaceInfo.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type * as vscode from 'vscode';
7
import { extUriBiasedIgnorePathCase } from '../../../util/vs/base/common/resources';
8
import { RepositoryProperties } from './chatSessionMetadataStore';
9
import { ChatSessionWorktreeProperties } from './chatSessionWorktreeService';
10
11
export interface IWorkspaceInfo {
12
/**
13
* The folder URI selected for this session.
14
* This could be a workspace folder or a git repository root.
15
*/
16
readonly folder: vscode.Uri | undefined;
17
18
/**
19
* The git repository root URI if the selected folder contains a git repository.
20
* `undefined` if the folder is not a git repository.
21
*/
22
readonly repository: vscode.Uri | undefined;
23
24
/**
25
* The git repository properties associated with this session.
26
*/
27
readonly repositoryProperties?: RepositoryProperties;
28
29
/**
30
* The worktree path if a worktree was created for this session.
31
* `undefined` if no worktree exists (e.g., plain folder or worktree creation failed).
32
*/
33
readonly worktree: vscode.Uri | undefined;
34
35
/**
36
* The worktree properties associated with this session.
37
*/
38
readonly worktreeProperties: ChatSessionWorktreeProperties | undefined;
39
}
40
41
export function getWorkingDirectory(workspaceInfo: IWorkspaceInfo): vscode.Uri | undefined {
42
// Give the folder higher priority over repository, as the user may have selected the folder directly,
43
// & if we don't create a worktree, then the folder is the working directory.
44
return workspaceInfo.worktree ?? workspaceInfo.folder ?? workspaceInfo.repository;
45
}
46
47
export function isIsolationEnabled(workspaceInfo: IWorkspaceInfo): boolean {
48
return !!workspaceInfo.worktreeProperties;
49
}
50
51
export function emptyWorkspaceInfo(): IWorkspaceInfo {
52
return {
53
folder: undefined,
54
repository: undefined,
55
repositoryProperties: undefined,
56
worktree: undefined,
57
worktreeProperties: undefined,
58
};
59
}
60
61
/**
62
* Given a file URI, finds which workspace (primary or additional) owns it.
63
* Returns the matching IWorkspaceInfo or undefined if no match.
64
*/
65
export function findOwningWorkspace(
66
file: vscode.Uri,
67
primaryWorkspace: IWorkspaceInfo,
68
additionalWorkspaces: IWorkspaceInfo[]
69
): IWorkspaceInfo | undefined {
70
for (const ws of [primaryWorkspace, ...additionalWorkspaces]) {
71
const wd = getWorkingDirectory(ws);
72
if (wd && extUriBiasedIgnorePathCase.isEqualOrParent(file, wd)) {
73
return ws;
74
}
75
if (ws.folder && extUriBiasedIgnorePathCase.isEqualOrParent(file, ws.folder)) {
76
return ws;
77
}
78
if (ws.worktree && ws.repository && extUriBiasedIgnorePathCase.isEqualOrParent(file, ws.repository)) {
79
return ws;
80
}
81
}
82
return undefined;
83
}
84
85