1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { Event } from 'vs/base/common/event';
import { extname } from 'vs/base/common/path';
import { TernarySearchTree } from 'vs/base/common/map';
import { extname as resourceExtname, basenameOrAuthority, joinPath, extUriBiasedIgnorePathCase } from 'vs/base/common/resources';
import { URI, UriComponents } from 'vs/base/common/uri';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { Schemas } from 'vs/base/common/network';
export const IWorkspaceContextService = createDecorator<IWorkspaceContextService>('contextService');
export interface IWorkspaceContextService {
readonly _serviceBrand: undefined;
/**
* An event which fires on workbench state changes.
*/
readonly onDidChangeWorkbenchState: Event<WorkbenchState>;
/**
* An event which fires on workspace name changes.
*/
readonly onDidChangeWorkspaceName: Event<void>;
/**
* An event which fires before workspace folders change.
*/
readonly onWillChangeWorkspaceFolders: Event<IWorkspaceFoldersWillChangeEvent>;
/**
* An event which fires on workspace folders change.
*/
readonly onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent>;
/**
* Provides access to the complete workspace object.
*/
getCompleteWorkspace(): Promise<IWorkspace>;
/**
* Provides access to the workspace object the window is running with.
* Use `getCompleteWorkspace` to get complete workspace object.
*/
getWorkspace(): IWorkspace;
/**
* Return the state of the workbench.
*
* WorkbenchState.EMPTY - if the workbench was opened with empty window or file
* WorkbenchState.FOLDER - if the workbench was opened with a folder
* WorkbenchState.WORKSPACE - if the workbench was opened with a workspace
*/
getWorkbenchState(): WorkbenchState;
/**
* Returns the folder for the given resource from the workspace.
* Can be null if there is no workspace or the resource is not inside the workspace.
*/
getWorkspaceFolder(resource: URI): IWorkspaceFolder | null;
/**
* Return `true` if the current workspace has the given identifier or root URI otherwise `false`.
*/
isCurrentWorkspace(workspaceIdOrFolder: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | URI): boolean;
/**
* Returns if the provided resource is inside the workspace or not.
*/
isInsideWorkspace(resource: URI): boolean;
}
export interface IResolvedWorkspace extends IWorkspaceIdentifier, IBaseWorkspace {
readonly folders: IWorkspaceFolder[];
}
export interface IBaseWorkspace {
/**
* If present, marks the window that opens the workspace
* as a remote window with the given authority.
*/
readonly remoteAuthority?: string;
/**
* Transient workspaces are meant to go away after being used
* once, e.g. a window reload of a transient workspace will
* open an empty window.
*
* See: https://github.com/microsoft/vscode/issues/119695
*/
readonly transient?: boolean;
}
export interface IBaseWorkspaceIdentifier {
/**
* Every workspace (multi-root, single folder or empty)
* has a unique identifier. It is not possible to open
* a workspace with the same `id` in multiple windows
*/
readonly id: string;
}
/**
* A single folder workspace identifier is a path to a folder + id.
*/
export interface ISingleFolderWorkspaceIdentifier extends IBaseWorkspaceIdentifier {
/**
* Folder path as `URI`.
*/
readonly uri: URI;
}
/**
* A multi-root workspace identifier is a path to a workspace file + id.
*/
export interface IWorkspaceIdentifier extends IBaseWorkspaceIdentifier {
/**
* Workspace config file path as `URI`.
*/
configPath: URI;
}
export interface IEmptyWorkspaceIdentifier extends IBaseWorkspaceIdentifier { }
export type IAnyWorkspaceIdentifier = IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IEmptyWorkspaceIdentifier;
export function isSingleFolderWorkspaceIdentifier(obj: unknown): obj is ISingleFolderWorkspaceIdentifier {
const singleFolderIdentifier = obj as ISingleFolderWorkspaceIdentifier | undefined;
return typeof singleFolderIdentifier?.id === 'string' && URI.isUri(singleFolderIdentifier.uri);
}
export function toWorkspaceIdentifier(workspace: IWorkspace): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined {
// Multi root
if (workspace.configuration) {
return {
id: workspace.id,
configPath: workspace.configuration
};
}
// Single folder
if (workspace.folders.length === 1) {
return {
id: workspace.id,
uri: workspace.folders[0].uri
};
}
// Empty workspace
return undefined;
}
export function isWorkspaceIdentifier(obj: unknown): obj is IWorkspaceIdentifier {
const workspaceIdentifier = obj as IWorkspaceIdentifier | undefined;
return typeof workspaceIdentifier?.id === 'string' && URI.isUri(workspaceIdentifier.configPath);
}
export interface ISerializedSingleFolderWorkspaceIdentifier extends IBaseWorkspaceIdentifier {
readonly uri: UriComponents;
}
export interface ISerializedWorkspaceIdentifier extends IBaseWorkspaceIdentifier {
readonly configPath: UriComponents;
}
export function reviveIdentifier(identifier: undefined): undefined;
export function reviveIdentifier(identifier: ISerializedWorkspaceIdentifier): IWorkspaceIdentifier;
export function reviveIdentifier(identifier: ISerializedSingleFolderWorkspaceIdentifier): ISingleFolderWorkspaceIdentifier;
export function reviveIdentifier(identifier: IEmptyWorkspaceIdentifier): IEmptyWorkspaceIdentifier;
export function reviveIdentifier(identifier: ISerializedWorkspaceIdentifier | ISerializedSingleFolderWorkspaceIdentifier | IEmptyWorkspaceIdentifier | undefined): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IEmptyWorkspaceIdentifier | undefined;
export function reviveIdentifier(identifier: ISerializedWorkspaceIdentifier | ISerializedSingleFolderWorkspaceIdentifier | IEmptyWorkspaceIdentifier | undefined): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IEmptyWorkspaceIdentifier | undefined {
// Single Folder
const singleFolderIdentifierCandidate = identifier as ISerializedSingleFolderWorkspaceIdentifier | undefined;
if (singleFolderIdentifierCandidate?.uri) {
return { id: singleFolderIdentifierCandidate.id, uri: URI.revive(singleFolderIdentifierCandidate.uri) };
}
// Multi folder
const workspaceIdentifierCandidate = identifier as ISerializedWorkspaceIdentifier | undefined;
if (workspaceIdentifierCandidate?.configPath) {
return { id: workspaceIdentifierCandidate.id, configPath: URI.revive(workspaceIdentifierCandidate.configPath) };
}
// Empty
if (identifier?.id) {
return { id: identifier.id };
}
return undefined;
}
export const enum WorkbenchState {
EMPTY = 1,
FOLDER,
WORKSPACE
}
export interface IWorkspaceFoldersWillChangeEvent {
readonly changes: IWorkspaceFoldersChangeEvent;
readonly fromCache: boolean;
join(promise: Promise<void>): void;
}
export interface IWorkspaceFoldersChangeEvent {
added: IWorkspaceFolder[];
removed: IWorkspaceFolder[];
changed: IWorkspaceFolder[];
}
export interface IWorkspace {
/**
* the unique identifier of the workspace.
*/
readonly id: string;
/**
* Folders in the workspace.
*/
readonly folders: IWorkspaceFolder[];
/**
* Transient workspaces are meant to go away after being used
* once, e.g. a window reload of a transient workspace will
* open an empty window.
*/
readonly transient?: boolean;
/**
* the location of the workspace configuration
*/
readonly configuration?: URI | null;
}
export function isWorkspace(thing: unknown): thing is IWorkspace {
const candidate = thing as IWorkspace | undefined;
return !!(candidate && typeof candidate === 'object'
&& typeof candidate.id === 'string'
&& Array.isArray(candidate.folders));
}
export interface IWorkspaceFolderData {
/**
* The associated URI for this workspace folder.
*/
readonly uri: URI;
/**
* The name of this workspace folder. Defaults to
* the basename of its [uri-path](#Uri.path)
*/
readonly name: string;
/**
* The ordinal number of this workspace folder.
*/
readonly index: number;
}
export interface IWorkspaceFolder extends IWorkspaceFolderData {
/**
* Given workspace folder relative path, returns the resource with the absolute path.
*/
toResource: (relativePath: string) => URI;
}
export function isWorkspaceFolder(thing: unknown): thing is IWorkspaceFolder {
const candidate = thing as IWorkspaceFolder;
return !!(candidate && typeof candidate === 'object'
&& URI.isUri(candidate.uri)
&& typeof candidate.name === 'string'
&& typeof candidate.toResource === 'function');
}
export class Workspace implements IWorkspace {
private _foldersMap: TernarySearchTree<URI, WorkspaceFolder> = TernarySearchTree.forUris<WorkspaceFolder>(this._ignorePathCasing, () => true);
private _folders!: WorkspaceFolder[];
constructor(
private _id: string,
folders: WorkspaceFolder[],
private _transient: boolean,
private _configuration: URI | null,
private _ignorePathCasing: (key: URI) => boolean,
) {
this.folders = folders;
}
update(workspace: Workspace) {
this._id = workspace.id;
this._configuration = workspace.configuration;
this._transient = workspace.transient;
this._ignorePathCasing = workspace._ignorePathCasing;
this.folders = workspace.folders;
}
get folders(): WorkspaceFolder[] {
return this._folders;
}
set folders(folders: WorkspaceFolder[]) {
this._folders = folders;
this.updateFoldersMap();
}
get id(): string {
return this._id;
}
get transient(): boolean {
return this._transient;
}
get configuration(): URI | null {
return this._configuration;
}
set configuration(configuration: URI | null) {
this._configuration = configuration;
}
getFolder(resource: URI): IWorkspaceFolder | null {
if (!resource) {
return null;
}
return this._foldersMap.findSubstr(resource) || null;
}
private updateFoldersMap(): void {
this._foldersMap = TernarySearchTree.forUris<WorkspaceFolder>(this._ignorePathCasing, () => true);
for (const folder of this.folders) {
this._foldersMap.set(folder.uri, folder);
}
}
toJSON(): IWorkspace {
return { id: this.id, folders: this.folders, transient: this.transient, configuration: this.configuration };
}
}
export interface IRawFileWorkspaceFolder {
readonly path: string;
name?: string;
}
export interface IRawUriWorkspaceFolder {
readonly uri: string;
name?: string;
}
export class WorkspaceFolder implements IWorkspaceFolder {
readonly uri: URI;
readonly name: string;
readonly index: number;
constructor(
data: IWorkspaceFolderData,
/**
* Provides access to the original metadata for this workspace
* folder. This can be different from the metadata provided in
* this class:
* - raw paths can be relative
* - raw paths are not normalized
*/
readonly raw?: IRawFileWorkspaceFolder | IRawUriWorkspaceFolder
) {
this.uri = data.uri;
this.index = data.index;
this.name = data.name;
}
toResource(relativePath: string): URI {
return joinPath(this.uri, relativePath);
}
toJSON(): IWorkspaceFolderData {
return { uri: this.uri, name: this.name, index: this.index };
}
}
export function toWorkspaceFolder(resource: URI): WorkspaceFolder {
return new WorkspaceFolder({ uri: resource, index: 0, name: basenameOrAuthority(resource) }, { uri: resource.toString() });
}
export const WORKSPACE_EXTENSION = 'code-workspace';
const WORKSPACE_SUFFIX = `.${WORKSPACE_EXTENSION}`;
export const WORKSPACE_FILTER = [{ name: localize('codeWorkspace', "Code Workspace"), extensions: [WORKSPACE_EXTENSION] }];
export const UNTITLED_WORKSPACE_NAME = 'workspace.json';
export function isUntitledWorkspace(path: URI, environmentService: IEnvironmentService): boolean {
return extUriBiasedIgnorePathCase.isEqualOrParent(path, environmentService.untitledWorkspacesHome);
}
export function isTemporaryWorkspace(workspace: IWorkspace): boolean;
export function isTemporaryWorkspace(path: URI): boolean;
export function isTemporaryWorkspace(arg1: IWorkspace | URI): boolean {
let path: URI | null | undefined;
if (URI.isUri(arg1)) {
path = arg1;
} else {
path = arg1.configuration;
}
return path?.scheme === Schemas.tmp;
}
export function isSavedWorkspace(path: URI, environmentService: IEnvironmentService): boolean {
return !isUntitledWorkspace(path, environmentService) && !isTemporaryWorkspace(path);
}
export function hasWorkspaceFileExtension(path: string | URI) {
const ext = (typeof path === 'string') ? extname(path) : resourceExtname(path);
return ext === WORKSPACE_SUFFIX;
}
|