@theia/workspace 1.45.1 → 1.46.0-next.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -30
- package/lib/browser/canonical-uri-service.d.ts +11 -11
- package/lib/browser/canonical-uri-service.js +59 -59
- package/lib/browser/diff-service.d.ts +10 -10
- package/lib/browser/diff-service.js +85 -85
- package/lib/browser/index.d.ts +7 -7
- package/lib/browser/index.js +34 -34
- package/lib/browser/quick-open-workspace.d.ts +25 -25
- package/lib/browser/quick-open-workspace.js +137 -137
- package/lib/browser/untitled-workspace-exit-dialog.d.ts +19 -19
- package/lib/browser/untitled-workspace-exit-dialog.js +74 -74
- package/lib/browser/workspace-breadcrumbs-contribution.d.ts +10 -10
- package/lib/browser/workspace-breadcrumbs-contribution.js +66 -66
- package/lib/browser/workspace-commands.d.ts +117 -117
- package/lib/browser/workspace-commands.js +601 -601
- package/lib/browser/workspace-commands.spec.d.ts +1 -1
- package/lib/browser/workspace-commands.spec.js +127 -127
- package/lib/browser/workspace-compare-handler.d.ts +26 -26
- package/lib/browser/workspace-compare-handler.js +66 -66
- package/lib/browser/workspace-delete-handler.d.ts +75 -75
- package/lib/browser/workspace-delete-handler.js +214 -214
- package/lib/browser/workspace-duplicate-handler.d.ts +30 -30
- package/lib/browser/workspace-duplicate-handler.js +86 -86
- package/lib/browser/workspace-frontend-contribution.d.ts +110 -110
- package/lib/browser/workspace-frontend-contribution.js +546 -546
- package/lib/browser/workspace-frontend-contribution.js.map +1 -1
- package/lib/browser/workspace-frontend-module.d.ts +3 -3
- package/lib/browser/workspace-frontend-module.js +91 -91
- package/lib/browser/workspace-input-dialog.d.ts +19 -19
- package/lib/browser/workspace-input-dialog.js +74 -74
- package/lib/browser/workspace-preferences.d.ts +11 -11
- package/lib/browser/workspace-preferences.js +46 -46
- package/lib/browser/workspace-schema-updater.d.ts +34 -34
- package/lib/browser/workspace-schema-updater.js +153 -153
- package/lib/browser/workspace-service.d.ts +184 -184
- package/lib/browser/workspace-service.js +739 -739
- package/lib/browser/workspace-storage-service.d.ts +15 -15
- package/lib/browser/workspace-storage-service.js +80 -80
- package/lib/browser/workspace-trust-preferences.d.ts +21 -21
- package/lib/browser/workspace-trust-preferences.js +65 -65
- package/lib/browser/workspace-trust-service.d.ts +27 -27
- package/lib/browser/workspace-trust-service.js +152 -152
- package/lib/browser/workspace-uri-contribution.d.ts +20 -20
- package/lib/browser/workspace-uri-contribution.js +113 -113
- package/lib/browser/workspace-uri-contribution.spec.d.ts +1 -1
- package/lib/browser/workspace-uri-contribution.spec.js +169 -169
- package/lib/browser/workspace-user-working-directory-provider.d.ts +12 -12
- package/lib/browser/workspace-user-working-directory-provider.js +62 -62
- package/lib/browser/workspace-utils.d.ts +15 -15
- package/lib/browser/workspace-utils.js +54 -54
- package/lib/browser/workspace-variable-contribution.d.ts +23 -23
- package/lib/browser/workspace-variable-contribution.js +237 -237
- package/lib/browser/workspace-window-title-updater.d.ts +7 -7
- package/lib/browser/workspace-window-title-updater.js +57 -57
- package/lib/browser-only/browser-only-workspace-server.d.ts +13 -0
- package/lib/browser-only/browser-only-workspace-server.d.ts.map +1 -0
- package/lib/browser-only/browser-only-workspace-server.js +81 -0
- package/lib/browser-only/browser-only-workspace-server.js.map +1 -0
- package/lib/browser-only/workspace-frontend-only-module.d.ts +4 -0
- package/lib/browser-only/workspace-frontend-only-module.d.ts.map +1 -0
- package/lib/browser-only/workspace-frontend-only-module.js +30 -0
- package/lib/browser-only/workspace-frontend-only-module.js.map +1 -0
- package/lib/common/index.d.ts +3 -3
- package/lib/common/index.js +30 -30
- package/lib/common/test/mock-workspace-server.d.ts +7 -7
- package/lib/common/test/mock-workspace-server.js +35 -35
- package/lib/common/untitled-workspace-service.d.ts +8 -8
- package/lib/common/untitled-workspace-service.js +60 -60
- package/lib/common/workspace-file-service.d.ts +26 -26
- package/lib/common/workspace-file-service.js +71 -71
- package/lib/common/workspace-protocol.d.ts +26 -26
- package/lib/common/workspace-protocol.js +23 -23
- package/lib/node/default-workspace-server.d.ts +65 -65
- package/lib/node/default-workspace-server.js +248 -248
- package/lib/node/default-workspace-server.spec.d.ts +1 -1
- package/lib/node/default-workspace-server.spec.js +85 -85
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +29 -29
- package/lib/node/workspace-backend-module.d.ts +3 -3
- package/lib/node/workspace-backend-module.js +33 -33
- package/package.json +9 -6
- package/src/browser/canonical-uri-service.ts +57 -57
- package/src/browser/diff-service.ts +62 -62
- package/src/browser/index.ts +23 -23
- package/src/browser/quick-open-workspace.ts +112 -112
- package/src/browser/untitled-workspace-exit-dialog.ts +70 -70
- package/src/browser/workspace-breadcrumbs-contribution.ts +56 -56
- package/src/browser/workspace-commands.spec.ts +153 -153
- package/src/browser/workspace-commands.ts +588 -588
- package/src/browser/workspace-compare-handler.ts +56 -56
- package/src/browser/workspace-delete-handler.ts +212 -212
- package/src/browser/workspace-duplicate-handler.ts +75 -75
- package/src/browser/workspace-frontend-contribution.ts +537 -537
- package/src/browser/workspace-frontend-module.ts +118 -118
- package/src/browser/workspace-input-dialog.ts +61 -61
- package/src/browser/workspace-preferences.ts +58 -58
- package/src/browser/workspace-schema-updater.ts +150 -150
- package/src/browser/workspace-service.ts +780 -780
- package/src/browser/workspace-storage-service.ts +67 -67
- package/src/browser/workspace-trust-preferences.ts +76 -76
- package/src/browser/workspace-trust-service.ts +147 -147
- package/src/browser/workspace-uri-contribution.spec.ts +191 -191
- package/src/browser/workspace-uri-contribution.ts +97 -97
- package/src/browser/workspace-user-working-directory-provider.ts +49 -49
- package/src/browser/workspace-utils.ts +45 -45
- package/src/browser/workspace-variable-contribution.ts +222 -222
- package/src/browser/workspace-window-title-updater.ts +45 -45
- package/src/browser-only/browser-only-workspace-server.ts +69 -0
- package/src/browser-only/workspace-frontend-only-module.ts +28 -0
- package/src/common/index.ts +19 -19
- package/src/common/test/mock-workspace-server.ts +29 -29
- package/src/common/untitled-workspace-service.ts +50 -50
- package/src/common/workspace-file-service.ts +72 -72
- package/src/common/workspace-protocol.ts +47 -47
- package/src/node/default-workspace-server.spec.ts +100 -100
- package/src/node/default-workspace-server.ts +244 -244
- package/src/node/index.ts +18 -18
- package/src/node/workspace-backend-module.ts +38 -38
|
@@ -1,780 +1,780 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2017 TypeFox and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
|
|
18
|
-
import URI from '@theia/core/lib/common/uri';
|
|
19
|
-
import { WorkspaceServer, UntitledWorkspaceService, WorkspaceFileService } from '../common';
|
|
20
|
-
import { WindowService } from '@theia/core/lib/browser/window/window-service';
|
|
21
|
-
import { DEFAULT_WINDOW_HASH } from '@theia/core/lib/common/window';
|
|
22
|
-
import {
|
|
23
|
-
FrontendApplicationContribution, PreferenceServiceImpl, PreferenceScope, PreferenceSchemaProvider, LabelProvider
|
|
24
|
-
} from '@theia/core/lib/browser';
|
|
25
|
-
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
26
|
-
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
|
|
27
|
-
import { ILogger, Disposable, DisposableCollection, Emitter, Event, MaybePromise, MessageService, nls } from '@theia/core';
|
|
28
|
-
import { WorkspacePreferences } from './workspace-preferences';
|
|
29
|
-
import * as jsoncparser from 'jsonc-parser';
|
|
30
|
-
import * as Ajv from '@theia/core/shared/ajv';
|
|
31
|
-
import { FileStat, BaseStat } from '@theia/filesystem/lib/common/files';
|
|
32
|
-
import { FileService } from '@theia/filesystem/lib/browser/file-service';
|
|
33
|
-
import { WindowTitleService } from '@theia/core/lib/browser/window/window-title-service';
|
|
34
|
-
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
|
|
35
|
-
import { workspaceSchema, WorkspaceSchemaUpdater } from './workspace-schema-updater';
|
|
36
|
-
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
|
|
37
|
-
import { StopReason } from '@theia/core/lib/common/frontend-application-state';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The workspace service.
|
|
41
|
-
*/
|
|
42
|
-
@injectable()
|
|
43
|
-
export class WorkspaceService implements FrontendApplicationContribution {
|
|
44
|
-
|
|
45
|
-
protected _workspace: FileStat | undefined;
|
|
46
|
-
|
|
47
|
-
protected _roots: FileStat[] = [];
|
|
48
|
-
protected deferredRoots = new Deferred<FileStat[]>();
|
|
49
|
-
|
|
50
|
-
@inject(FileService)
|
|
51
|
-
protected readonly fileService: FileService;
|
|
52
|
-
|
|
53
|
-
@inject(WorkspaceServer)
|
|
54
|
-
protected readonly server: WorkspaceServer;
|
|
55
|
-
|
|
56
|
-
@inject(WindowService)
|
|
57
|
-
protected readonly windowService: WindowService;
|
|
58
|
-
|
|
59
|
-
@inject(ILogger)
|
|
60
|
-
protected logger: ILogger;
|
|
61
|
-
|
|
62
|
-
@inject(WorkspacePreferences)
|
|
63
|
-
protected preferences: WorkspacePreferences;
|
|
64
|
-
|
|
65
|
-
@inject(PreferenceServiceImpl)
|
|
66
|
-
protected readonly preferenceImpl: PreferenceServiceImpl;
|
|
67
|
-
|
|
68
|
-
@inject(PreferenceSchemaProvider)
|
|
69
|
-
protected readonly schemaProvider: PreferenceSchemaProvider;
|
|
70
|
-
|
|
71
|
-
@inject(EnvVariablesServer)
|
|
72
|
-
protected readonly envVariableServer: EnvVariablesServer;
|
|
73
|
-
|
|
74
|
-
@inject(MessageService)
|
|
75
|
-
protected readonly messageService: MessageService;
|
|
76
|
-
|
|
77
|
-
@inject(LabelProvider)
|
|
78
|
-
protected readonly labelProvider: LabelProvider;
|
|
79
|
-
|
|
80
|
-
@inject(FileSystemPreferences)
|
|
81
|
-
protected readonly fsPreferences: FileSystemPreferences;
|
|
82
|
-
|
|
83
|
-
@inject(WorkspaceSchemaUpdater)
|
|
84
|
-
protected readonly schemaUpdater: WorkspaceSchemaUpdater;
|
|
85
|
-
|
|
86
|
-
@inject(UntitledWorkspaceService)
|
|
87
|
-
protected readonly untitledWorkspaceService: UntitledWorkspaceService;
|
|
88
|
-
|
|
89
|
-
@inject(WorkspaceFileService)
|
|
90
|
-
protected readonly workspaceFileService: WorkspaceFileService;
|
|
91
|
-
|
|
92
|
-
@inject(WindowTitleService)
|
|
93
|
-
protected readonly windowTitleService: WindowTitleService;
|
|
94
|
-
|
|
95
|
-
protected _ready = new Deferred<void>();
|
|
96
|
-
get ready(): Promise<void> {
|
|
97
|
-
return this._ready.promise;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
@postConstruct()
|
|
101
|
-
protected init(): void {
|
|
102
|
-
this.doInit();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
protected async doInit(): Promise<void> {
|
|
106
|
-
const wsUriString = await this.getDefaultWorkspaceUri();
|
|
107
|
-
const wsStat = await this.toFileStat(wsUriString);
|
|
108
|
-
await this.setWorkspace(wsStat);
|
|
109
|
-
|
|
110
|
-
this.fileService.onDidFilesChange(event => {
|
|
111
|
-
if (this._workspace && this._workspace.isFile && event.contains(this._workspace.resource)) {
|
|
112
|
-
this.updateWorkspace();
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
this.fsPreferences.onPreferenceChanged(event => {
|
|
116
|
-
if (event.preferenceName === 'files.watcherExclude') {
|
|
117
|
-
this.refreshRootWatchers();
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
this._ready.resolve();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Resolves to the default workspace URI as string.
|
|
125
|
-
*
|
|
126
|
-
* The default implementation tries to extract the default workspace location
|
|
127
|
-
* from the `window.location.hash`, then falls-back to the most recently
|
|
128
|
-
* used workspace root from the server.
|
|
129
|
-
*
|
|
130
|
-
* It is not ensured that the resolved workspace URI is valid, it can point
|
|
131
|
-
* to a non-existing location.
|
|
132
|
-
*/
|
|
133
|
-
protected getDefaultWorkspaceUri(): MaybePromise<string | undefined> {
|
|
134
|
-
return this.doGetDefaultWorkspaceUri();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
protected async doGetDefaultWorkspaceUri(): Promise<string | undefined> {
|
|
138
|
-
|
|
139
|
-
// If an empty window is explicitly requested do not restore a previous workspace.
|
|
140
|
-
// Note: `window.location.hash` includes leading "#" if non-empty.
|
|
141
|
-
if (window.location.hash === `#${DEFAULT_WINDOW_HASH}`) {
|
|
142
|
-
window.location.hash = '';
|
|
143
|
-
return undefined;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Prefer the workspace path specified as the URL fragment, if present.
|
|
147
|
-
if (window.location.hash.length > 1) {
|
|
148
|
-
// Remove the leading # and decode the URI.
|
|
149
|
-
const wpPath = decodeURI(window.location.hash.substring(1));
|
|
150
|
-
const workspaceUri = new URI().withPath(wpPath).withScheme('file');
|
|
151
|
-
let workspaceStat: FileStat | undefined;
|
|
152
|
-
try {
|
|
153
|
-
workspaceStat = await this.fileService.resolve(workspaceUri);
|
|
154
|
-
} catch { }
|
|
155
|
-
if (workspaceStat && !workspaceStat.isDirectory && !this.isWorkspaceFile(workspaceStat)) {
|
|
156
|
-
this.messageService.error(`Not a valid workspace file: ${workspaceUri}`);
|
|
157
|
-
return undefined;
|
|
158
|
-
}
|
|
159
|
-
return workspaceUri.toString();
|
|
160
|
-
} else {
|
|
161
|
-
// Else, ask the server for its suggested workspace (usually the one
|
|
162
|
-
// specified on the CLI, or the most recent).
|
|
163
|
-
return this.server.getMostRecentlyUsedWorkspace();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Set the URL fragment to the given workspace path.
|
|
169
|
-
*/
|
|
170
|
-
protected setURLFragment(workspacePath: string): void {
|
|
171
|
-
window.location.hash = encodeURI(workspacePath);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
get roots(): Promise<FileStat[]> {
|
|
175
|
-
return this.deferredRoots.promise;
|
|
176
|
-
}
|
|
177
|
-
tryGetRoots(): FileStat[] {
|
|
178
|
-
return this._roots;
|
|
179
|
-
}
|
|
180
|
-
get workspace(): FileStat | undefined {
|
|
181
|
-
return this._workspace;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
protected readonly onWorkspaceChangeEmitter = new Emitter<FileStat[]>();
|
|
185
|
-
get onWorkspaceChanged(): Event<FileStat[]> {
|
|
186
|
-
return this.onWorkspaceChangeEmitter.event;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
protected readonly onWorkspaceLocationChangedEmitter = new Emitter<FileStat | undefined>();
|
|
190
|
-
get onWorkspaceLocationChanged(): Event<FileStat | undefined> {
|
|
191
|
-
return this.onWorkspaceLocationChangedEmitter.event;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
protected readonly toDisposeOnWorkspace = new DisposableCollection();
|
|
195
|
-
protected async setWorkspace(workspaceStat: FileStat | undefined): Promise<void> {
|
|
196
|
-
if (this._workspace && workspaceStat &&
|
|
197
|
-
this._workspace.resource === workspaceStat.resource &&
|
|
198
|
-
this._workspace.mtime === workspaceStat.mtime &&
|
|
199
|
-
this._workspace.etag === workspaceStat.etag &&
|
|
200
|
-
this._workspace.size === workspaceStat.size) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
this.toDisposeOnWorkspace.dispose();
|
|
204
|
-
this._workspace = workspaceStat;
|
|
205
|
-
if (this._workspace) {
|
|
206
|
-
const uri = this._workspace.resource;
|
|
207
|
-
if (this._workspace.isFile) {
|
|
208
|
-
this.toDisposeOnWorkspace.push(this.fileService.watch(uri));
|
|
209
|
-
this.onWorkspaceLocationChangedEmitter.fire(this._workspace);
|
|
210
|
-
}
|
|
211
|
-
this.setURLFragment(uri.path.toString());
|
|
212
|
-
} else {
|
|
213
|
-
this.setURLFragment('');
|
|
214
|
-
}
|
|
215
|
-
this.updateTitle();
|
|
216
|
-
await this.server.setMostRecentlyUsedWorkspace(this._workspace ? this._workspace.resource.toString() : '');
|
|
217
|
-
await this.updateWorkspace();
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
protected async updateWorkspace(): Promise<void> {
|
|
221
|
-
await this.updateRoots();
|
|
222
|
-
this.watchRoots();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
protected async updateRoots(): Promise<void> {
|
|
226
|
-
const newRoots = await this.computeRoots();
|
|
227
|
-
let rootsChanged = false;
|
|
228
|
-
if (newRoots.length !== this._roots.length || newRoots.length === 0) {
|
|
229
|
-
rootsChanged = true;
|
|
230
|
-
} else {
|
|
231
|
-
for (const newRoot of newRoots) {
|
|
232
|
-
if (!this._roots.some(r => r.resource.toString() === newRoot.resource.toString())) {
|
|
233
|
-
rootsChanged = true;
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (rootsChanged) {
|
|
239
|
-
this._roots = newRoots;
|
|
240
|
-
this.deferredRoots.resolve(this._roots); // in order to resolve first
|
|
241
|
-
this.deferredRoots = new Deferred<FileStat[]>();
|
|
242
|
-
this.deferredRoots.resolve(this._roots);
|
|
243
|
-
this.onWorkspaceChangeEmitter.fire(this._roots);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
protected async computeRoots(): Promise<FileStat[]> {
|
|
248
|
-
const roots: FileStat[] = [];
|
|
249
|
-
if (this._workspace) {
|
|
250
|
-
if (this._workspace.isDirectory) {
|
|
251
|
-
return [this._workspace];
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const workspaceData = await this.getWorkspaceDataFromFile();
|
|
255
|
-
if (workspaceData) {
|
|
256
|
-
for (const { path } of workspaceData.folders) {
|
|
257
|
-
const valid = await this.toValidRoot(path);
|
|
258
|
-
if (valid) {
|
|
259
|
-
roots.push(valid);
|
|
260
|
-
} else {
|
|
261
|
-
roots.push(FileStat.dir(path));
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return roots;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
protected async getWorkspaceDataFromFile(): Promise<WorkspaceData | undefined> {
|
|
270
|
-
if (this._workspace && await this.fileService.exists(this._workspace.resource)) {
|
|
271
|
-
if (this._workspace.isDirectory) {
|
|
272
|
-
return {
|
|
273
|
-
folders: [{ path: this._workspace.resource.toString() }]
|
|
274
|
-
};
|
|
275
|
-
} else if (this.isWorkspaceFile(this._workspace)) {
|
|
276
|
-
const stat = await this.fileService.read(this._workspace.resource);
|
|
277
|
-
const strippedContent = jsoncparser.stripComments(stat.value);
|
|
278
|
-
const data = jsoncparser.parse(strippedContent);
|
|
279
|
-
if (data && WorkspaceData.is(data)) {
|
|
280
|
-
return WorkspaceData.transformToAbsolute(data, stat);
|
|
281
|
-
}
|
|
282
|
-
this.logger.error(`Unable to retrieve workspace data from the file: '${this.labelProvider.getLongName(this._workspace)}'. Please check if the file is corrupted.`);
|
|
283
|
-
} else {
|
|
284
|
-
this.logger.warn(`Not a valid workspace file: ${this.labelProvider.getLongName(this._workspace)}`);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
protected updateTitle(): void {
|
|
290
|
-
let rootName: string | undefined;
|
|
291
|
-
let rootPath: string | undefined;
|
|
292
|
-
if (this._workspace) {
|
|
293
|
-
const displayName = this._workspace.name;
|
|
294
|
-
const fullName = this._workspace.resource.path.toString();
|
|
295
|
-
if (this.isWorkspaceFile(this._workspace)) {
|
|
296
|
-
if (this.isUntitledWorkspace(this._workspace.resource)) {
|
|
297
|
-
const untitled = nls.localizeByDefault('Untitled (Workspace)');
|
|
298
|
-
rootName = untitled;
|
|
299
|
-
rootPath = untitled;
|
|
300
|
-
} else {
|
|
301
|
-
rootName = displayName.slice(0, displayName.lastIndexOf('.'));
|
|
302
|
-
rootPath = fullName.slice(0, fullName.lastIndexOf('.'));
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
rootName = displayName;
|
|
306
|
-
rootPath = fullName;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
this.windowTitleService.update({
|
|
310
|
-
rootName,
|
|
311
|
-
rootPath
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* on unload, we set our workspace root as the last recently used on the backend.
|
|
317
|
-
*/
|
|
318
|
-
onStop(): void {
|
|
319
|
-
this.server.setMostRecentlyUsedWorkspace(this._workspace ? this._workspace.resource.toString() : '');
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async recentWorkspaces(): Promise<string[]> {
|
|
323
|
-
return this.server.getRecentWorkspaces();
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async removeRecentWorkspace(uri: string): Promise<void> {
|
|
327
|
-
return this.server.removeRecentWorkspace(uri);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Returns `true` if theia has an opened workspace or folder
|
|
332
|
-
* @returns {boolean}
|
|
333
|
-
*/
|
|
334
|
-
get opened(): boolean {
|
|
335
|
-
return !!this._workspace;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Returns `true` if a multiple-root workspace is currently open.
|
|
340
|
-
* @returns {boolean}
|
|
341
|
-
*/
|
|
342
|
-
get isMultiRootWorkspaceOpened(): boolean {
|
|
343
|
-
return !!this.workspace && !this.workspace.isDirectory;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Opens directory, or recreates a workspace from the file that `uri` points to.
|
|
348
|
-
*/
|
|
349
|
-
open(uri: URI, options?: WorkspaceInput): void {
|
|
350
|
-
this.doOpen(uri, options);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
protected async doOpen(uri: URI, options?: WorkspaceInput): Promise<URI | undefined> {
|
|
354
|
-
const stat = await this.toFileStat(uri);
|
|
355
|
-
if (stat) {
|
|
356
|
-
if (!stat.isDirectory && !this.isWorkspaceFile(stat)) {
|
|
357
|
-
const message = `Not a valid workspace: ${uri.path.toString()}`;
|
|
358
|
-
this.messageService.error(message);
|
|
359
|
-
throw new Error(message);
|
|
360
|
-
}
|
|
361
|
-
// The same window has to be preserved too (instead of opening a new one), if the workspace root is not yet available and we are setting it for the first time.
|
|
362
|
-
// Option passed as parameter has the highest priority (for api developers), then the preference, then the default.
|
|
363
|
-
await this.roots;
|
|
364
|
-
const { preserveWindow } = {
|
|
365
|
-
preserveWindow: this.preferences['workspace.preserveWindow'] || !this.opened,
|
|
366
|
-
...options
|
|
367
|
-
};
|
|
368
|
-
await this.server.setMostRecentlyUsedWorkspace(uri.toString());
|
|
369
|
-
if (preserveWindow) {
|
|
370
|
-
this._workspace = stat;
|
|
371
|
-
}
|
|
372
|
-
this.openWindow(stat, Object.assign(options ?? {}, { preserveWindow }));
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
throw new Error('Invalid workspace root URI. Expected an existing directory or workspace file.');
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Adds root folder(s) to the workspace
|
|
380
|
-
* @param uris URI or URIs of the root folder(s) to add
|
|
381
|
-
*/
|
|
382
|
-
async addRoot(uris: URI[] | URI): Promise<void> {
|
|
383
|
-
const toAdd = Array.isArray(uris) ? uris : [uris];
|
|
384
|
-
await this.spliceRoots(this._roots.length, 0, ...toAdd);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Removes root folder(s) from workspace.
|
|
389
|
-
*/
|
|
390
|
-
async removeRoots(uris: URI[]): Promise<void> {
|
|
391
|
-
if (!this.opened) {
|
|
392
|
-
throw new Error('Folder cannot be removed as there is no active folder in the current workspace.');
|
|
393
|
-
}
|
|
394
|
-
if (this._workspace) {
|
|
395
|
-
const workspaceData = await this.getWorkspaceDataFromFile();
|
|
396
|
-
this._workspace = await this.writeWorkspaceFile(this._workspace,
|
|
397
|
-
WorkspaceData.buildWorkspaceData(
|
|
398
|
-
this._roots.filter(root => uris.findIndex(u => u.toString() === root.resource.toString()) < 0),
|
|
399
|
-
workspaceData
|
|
400
|
-
)
|
|
401
|
-
);
|
|
402
|
-
await this.updateWorkspace();
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
async spliceRoots(start: number, deleteCount?: number, ...rootsToAdd: URI[]): Promise<URI[]> {
|
|
407
|
-
if (!this._workspace || this._workspace.isDirectory) {
|
|
408
|
-
const untitledWorkspace = await this.getUntitledWorkspace();
|
|
409
|
-
await this.save(untitledWorkspace);
|
|
410
|
-
if (!this._workspace) {
|
|
411
|
-
throw new Error('Could not create new untitled workspace');
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
const dedup = new Set<string>();
|
|
415
|
-
const roots = this._roots.map(root => (dedup.add(root.resource.toString()), root.resource.toString()));
|
|
416
|
-
const toAdd: string[] = [];
|
|
417
|
-
for (const root of rootsToAdd) {
|
|
418
|
-
const uri = root.toString();
|
|
419
|
-
if (!dedup.has(uri)) {
|
|
420
|
-
dedup.add(uri);
|
|
421
|
-
toAdd.push(uri);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
const toRemove = roots.splice(start, deleteCount || 0, ...toAdd);
|
|
425
|
-
if (!toRemove.length && !toAdd.length) {
|
|
426
|
-
return [];
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
const currentData = await this.getWorkspaceDataFromFile();
|
|
430
|
-
const newData = WorkspaceData.buildWorkspaceData(roots, currentData);
|
|
431
|
-
await this.writeWorkspaceFile(this._workspace, newData);
|
|
432
|
-
await this.updateWorkspace();
|
|
433
|
-
return toRemove.map(root => new URI(root));
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
async getUntitledWorkspace(): Promise<URI> {
|
|
437
|
-
const configDirURI = new URI(await this.envVariableServer.getConfigDirUri());
|
|
438
|
-
return this.untitledWorkspaceService.getUntitledWorkspaceUri(
|
|
439
|
-
configDirURI,
|
|
440
|
-
uri => this.fileService.exists(uri).then(exists => !exists),
|
|
441
|
-
() => this.messageService.warn(nls.localize(
|
|
442
|
-
'theia/workspace/untitled-cleanup',
|
|
443
|
-
'There appear to be many untitled workspace files. Please check {0} and remove any unused files.',
|
|
444
|
-
configDirURI.resolve('workspaces').path.fsPath())
|
|
445
|
-
),
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
protected async writeWorkspaceFile(workspaceFile: FileStat | undefined, workspaceData: WorkspaceData): Promise<FileStat | undefined> {
|
|
450
|
-
if (workspaceFile) {
|
|
451
|
-
const data = JSON.stringify(WorkspaceData.transformToRelative(workspaceData, workspaceFile));
|
|
452
|
-
const edits = jsoncparser.format(data, undefined, { tabSize: 2, insertSpaces: true, eol: '' });
|
|
453
|
-
const result = jsoncparser.applyEdits(data, edits);
|
|
454
|
-
await this.fileService.write(workspaceFile.resource, result);
|
|
455
|
-
return this.fileService.resolve(workspaceFile.resource);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
* Clears current workspace root.
|
|
461
|
-
*/
|
|
462
|
-
async close(): Promise<void> {
|
|
463
|
-
if (await this.windowService.isSafeToShutDown(StopReason.Reload)) {
|
|
464
|
-
this.windowService.setSafeToShutDown();
|
|
465
|
-
this._workspace = undefined;
|
|
466
|
-
this._roots.length = 0;
|
|
467
|
-
|
|
468
|
-
await this.server.setMostRecentlyUsedWorkspace('');
|
|
469
|
-
this.reloadWindow();
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* returns a FileStat if the argument URI points to an existing directory. Otherwise, `undefined`.
|
|
475
|
-
*/
|
|
476
|
-
protected async toValidRoot(uri: URI | string | undefined): Promise<FileStat | undefined> {
|
|
477
|
-
const fileStat = await this.toFileStat(uri);
|
|
478
|
-
if (fileStat && fileStat.isDirectory) {
|
|
479
|
-
return fileStat;
|
|
480
|
-
}
|
|
481
|
-
return undefined;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* returns a FileStat if the argument URI points to a file or directory. Otherwise, `undefined`.
|
|
486
|
-
*/
|
|
487
|
-
protected async toFileStat(uri: URI | string | undefined): Promise<FileStat | undefined> {
|
|
488
|
-
if (!uri) {
|
|
489
|
-
return undefined;
|
|
490
|
-
}
|
|
491
|
-
let uriStr = uri.toString();
|
|
492
|
-
try {
|
|
493
|
-
if (uriStr.endsWith('/')) {
|
|
494
|
-
uriStr = uriStr.slice(0, -1);
|
|
495
|
-
}
|
|
496
|
-
const normalizedUri = new URI(uriStr).normalizePath();
|
|
497
|
-
return await this.fileService.resolve(normalizedUri);
|
|
498
|
-
} catch (error) {
|
|
499
|
-
return undefined;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
protected openWindow(uri: FileStat, options?: WorkspaceInput): void {
|
|
504
|
-
const workspacePath = uri.resource.path.toString();
|
|
505
|
-
|
|
506
|
-
if (this.shouldPreserveWindow(options)) {
|
|
507
|
-
this.reloadWindow(options);
|
|
508
|
-
} else {
|
|
509
|
-
try {
|
|
510
|
-
this.openNewWindow(workspacePath, options);
|
|
511
|
-
} catch (error) {
|
|
512
|
-
// Fall back to reloading the current window in case the browser has blocked the new window
|
|
513
|
-
this._workspace = uri;
|
|
514
|
-
this.logger.error(error.toString()).then(() => this.reloadWindow());
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
protected reloadWindow(options?: WorkspaceInput): void {
|
|
520
|
-
// Set the new workspace path as the URL fragment.
|
|
521
|
-
if (this._workspace !== undefined) {
|
|
522
|
-
this.setURLFragment(this._workspace.resource.path.toString());
|
|
523
|
-
} else {
|
|
524
|
-
this.setURLFragment('');
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
this.windowService.reload();
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
protected openNewWindow(workspacePath: string, options?: WorkspaceInput): void {
|
|
531
|
-
const url = new URL(window.location.href);
|
|
532
|
-
url.hash = encodeURI(workspacePath);
|
|
533
|
-
this.windowService.openNewWindow(url.toString());
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
protected shouldPreserveWindow(options?: WorkspaceInput): boolean {
|
|
537
|
-
return options !== undefined && !!options.preserveWindow;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
/**
|
|
541
|
-
* Return true if one of the paths in paths array is present in the workspace
|
|
542
|
-
* NOTE: You should always explicitly use `/` as the separator between the path segments.
|
|
543
|
-
*/
|
|
544
|
-
async containsSome(paths: string[]): Promise<boolean> {
|
|
545
|
-
await this.roots;
|
|
546
|
-
if (this.opened) {
|
|
547
|
-
for (const root of this._roots) {
|
|
548
|
-
const uri = root.resource;
|
|
549
|
-
for (const path of paths) {
|
|
550
|
-
const fileUri = uri.resolve(path);
|
|
551
|
-
const exists = await this.fileService.exists(fileUri);
|
|
552
|
-
if (exists) {
|
|
553
|
-
return exists;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
return false;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* `true` if the current workspace is configured using a configuration file.
|
|
563
|
-
*
|
|
564
|
-
* `false` if there is no workspace or the workspace is simply a folder.
|
|
565
|
-
*/
|
|
566
|
-
get saved(): boolean {
|
|
567
|
-
return !!this._workspace && !this._workspace.isDirectory;
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
* Save workspace data into a file
|
|
572
|
-
* @param uri URI or FileStat of the workspace file
|
|
573
|
-
*/
|
|
574
|
-
async save(uri: URI | FileStat): Promise<void> {
|
|
575
|
-
const resource = uri instanceof URI ? uri : uri.resource;
|
|
576
|
-
if (!await this.fileService.exists(resource)) {
|
|
577
|
-
await this.fileService.create(resource);
|
|
578
|
-
}
|
|
579
|
-
const workspaceData: WorkspaceData = { folders: [], settings: {} };
|
|
580
|
-
if (!this.saved) {
|
|
581
|
-
for (const p of Object.keys(this.schemaProvider.getCombinedSchema().properties)) {
|
|
582
|
-
if (this.schemaProvider.isValidInScope(p, PreferenceScope.Folder)) {
|
|
583
|
-
continue;
|
|
584
|
-
}
|
|
585
|
-
const preferences = this.preferenceImpl.inspect(p);
|
|
586
|
-
if (preferences && preferences.workspaceValue) {
|
|
587
|
-
workspaceData.settings![p] = preferences.workspaceValue;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
let stat = await this.toFileStat(resource);
|
|
592
|
-
Object.assign(workspaceData, await this.getWorkspaceDataFromFile());
|
|
593
|
-
stat = await this.writeWorkspaceFile(stat, WorkspaceData.buildWorkspaceData(this._roots, workspaceData));
|
|
594
|
-
await this.server.setMostRecentlyUsedWorkspace(resource.toString());
|
|
595
|
-
// If saving a workspace based on an untitled workspace, delete the old file.
|
|
596
|
-
const toDelete = this.isUntitledWorkspace(this.workspace?.resource) && this.workspace!.resource;
|
|
597
|
-
await this.setWorkspace(stat);
|
|
598
|
-
if (toDelete && stat && !toDelete.isEqual(stat.resource)) {
|
|
599
|
-
await this.fileService.delete(toDelete).catch(() => { });
|
|
600
|
-
}
|
|
601
|
-
this.onWorkspaceLocationChangedEmitter.fire(stat);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
protected readonly rootWatchers = new Map<string, Disposable>();
|
|
605
|
-
|
|
606
|
-
protected async watchRoots(): Promise<void> {
|
|
607
|
-
const rootUris = new Set(this._roots.map(r => r.resource.toString()));
|
|
608
|
-
for (const [uri, watcher] of this.rootWatchers.entries()) {
|
|
609
|
-
if (!rootUris.has(uri)) {
|
|
610
|
-
watcher.dispose();
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
for (const root of this._roots) {
|
|
614
|
-
this.watchRoot(root);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
protected async refreshRootWatchers(): Promise<void> {
|
|
619
|
-
for (const watcher of this.rootWatchers.values()) {
|
|
620
|
-
watcher.dispose();
|
|
621
|
-
}
|
|
622
|
-
await this.watchRoots();
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
protected async watchRoot(root: FileStat): Promise<void> {
|
|
626
|
-
const uriStr = root.resource.toString();
|
|
627
|
-
if (this.rootWatchers.has(uriStr)) {
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
const excludes = this.getExcludes(uriStr);
|
|
631
|
-
const watcher = this.fileService.watch(new URI(uriStr), {
|
|
632
|
-
recursive: true,
|
|
633
|
-
excludes
|
|
634
|
-
});
|
|
635
|
-
this.rootWatchers.set(uriStr, new DisposableCollection(
|
|
636
|
-
watcher,
|
|
637
|
-
Disposable.create(() => this.rootWatchers.delete(uriStr))
|
|
638
|
-
));
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
protected getExcludes(uri: string): string[] {
|
|
642
|
-
const patterns = this.fsPreferences.get('files.watcherExclude', undefined, uri);
|
|
643
|
-
return Object.keys(patterns).filter(pattern => patterns[pattern]);
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
/**
|
|
647
|
-
* Returns the workspace root uri that the given file belongs to.
|
|
648
|
-
* In case that the file is found in more than one workspace roots, returns the root that is closest to the file.
|
|
649
|
-
* If the file is not from the current workspace, returns `undefined`.
|
|
650
|
-
* @param uri URI of the file
|
|
651
|
-
*/
|
|
652
|
-
getWorkspaceRootUri(uri: URI | undefined): URI | undefined {
|
|
653
|
-
if (!uri) {
|
|
654
|
-
const root = this.tryGetRoots()[0];
|
|
655
|
-
if (root) {
|
|
656
|
-
return root.resource;
|
|
657
|
-
}
|
|
658
|
-
return undefined;
|
|
659
|
-
}
|
|
660
|
-
const rootUris: URI[] = [];
|
|
661
|
-
for (const root of this.tryGetRoots()) {
|
|
662
|
-
const rootUri = root.resource;
|
|
663
|
-
if (rootUri && rootUri.isEqualOrParent(uri)) {
|
|
664
|
-
rootUris.push(rootUri);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
return rootUris.sort((r1, r2) => r2.toString().length - r1.toString().length)[0];
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
areWorkspaceRoots(uris: URI[]): boolean {
|
|
671
|
-
if (!uris.length) {
|
|
672
|
-
return false;
|
|
673
|
-
}
|
|
674
|
-
const rootUris = new Set(this.tryGetRoots().map(root => root.resource.toString()));
|
|
675
|
-
return uris.every(uri => rootUris.has(uri.toString()));
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
* Check if the file should be considered as a workspace file.
|
|
680
|
-
*
|
|
681
|
-
* Example: We should not try to read the contents of an .exe file.
|
|
682
|
-
*/
|
|
683
|
-
protected isWorkspaceFile(candidate: FileStat | URI): boolean {
|
|
684
|
-
return this.workspaceFileService.isWorkspaceFile(candidate);
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
isUntitledWorkspace(candidate?: URI): boolean {
|
|
688
|
-
return this.untitledWorkspaceService.isUntitledWorkspace(candidate);
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
async isSafeToReload(withURI?: URI): Promise<boolean> {
|
|
692
|
-
return !withURI || !this.untitledWorkspaceService.isUntitledWorkspace(withURI) || new URI(await this.getDefaultWorkspaceUri()).isEqual(withURI);
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
/**
|
|
696
|
-
*
|
|
697
|
-
* @param key the property key under which to store the schema (e.g. tasks, launch)
|
|
698
|
-
* @param schema the schema for the property. If none is supplied, the update is treated as a deletion.
|
|
699
|
-
*/
|
|
700
|
-
async updateSchema(key: string, schema?: IJSONSchema): Promise<boolean> {
|
|
701
|
-
return this.schemaUpdater.updateSchema({ key, schema });
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
export interface WorkspaceInput {
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Tests whether the same window should be used or a new one has to be opened after setting the workspace root. By default it is `false`.
|
|
709
|
-
*/
|
|
710
|
-
preserveWindow?: boolean;
|
|
711
|
-
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
export interface WorkspaceData {
|
|
715
|
-
folders: Array<{ path: string, name?: string }>;
|
|
716
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
717
|
-
[key: string]: { [id: string]: any };
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
export namespace WorkspaceData {
|
|
721
|
-
const validateSchema = new Ajv().compile(workspaceSchema);
|
|
722
|
-
|
|
723
|
-
export function is(data: unknown): data is WorkspaceData {
|
|
724
|
-
return !!validateSchema(data);
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
export function buildWorkspaceData(folders: string[] | FileStat[], additionalFields?: Partial<WorkspaceData>): WorkspaceData {
|
|
728
|
-
const roots = new Set<string>();
|
|
729
|
-
if (folders.length > 0) {
|
|
730
|
-
if (typeof folders[0] !== 'string') {
|
|
731
|
-
(<FileStat[]>folders).forEach(folder => roots.add(folder.resource.toString()));
|
|
732
|
-
} else {
|
|
733
|
-
(<string[]>folders).forEach(folder => roots.add(folder));
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
const data: WorkspaceData = {
|
|
737
|
-
folders: Array.from(roots, folder => ({ path: folder }))
|
|
738
|
-
};
|
|
739
|
-
if (additionalFields) {
|
|
740
|
-
delete additionalFields.folders;
|
|
741
|
-
Object.assign(data, additionalFields);
|
|
742
|
-
}
|
|
743
|
-
return data;
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
export function transformToRelative(data: WorkspaceData, workspaceFile?: FileStat): WorkspaceData {
|
|
747
|
-
const folderUris: string[] = [];
|
|
748
|
-
const workspaceFileUri = new URI(workspaceFile ? workspaceFile.resource.toString() : '').withScheme('file');
|
|
749
|
-
for (const { path } of data.folders) {
|
|
750
|
-
const folderUri = new URI(path).withScheme('file');
|
|
751
|
-
const rel = workspaceFileUri.parent.relative(folderUri);
|
|
752
|
-
if (rel) {
|
|
753
|
-
folderUris.push(rel.toString());
|
|
754
|
-
} else {
|
|
755
|
-
folderUris.push(folderUri.toString());
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
return buildWorkspaceData(folderUris, data);
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
export function transformToAbsolute(data: WorkspaceData, workspaceFile?: BaseStat): WorkspaceData {
|
|
762
|
-
if (workspaceFile) {
|
|
763
|
-
const folders: string[] = [];
|
|
764
|
-
for (const folder of data.folders) {
|
|
765
|
-
const path = folder.path;
|
|
766
|
-
if (path.startsWith('file:///')) {
|
|
767
|
-
folders.push(path);
|
|
768
|
-
} else {
|
|
769
|
-
const absolutePath = workspaceFile.resource.withScheme('file').parent.resolveToAbsolute(path)?.toString();
|
|
770
|
-
if (absolutePath) {
|
|
771
|
-
folders.push(absolutePath.toString());
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
}
|
|
776
|
-
return Object.assign(data, buildWorkspaceData(folders, data));
|
|
777
|
-
}
|
|
778
|
-
return data;
|
|
779
|
-
}
|
|
780
|
-
}
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2017 TypeFox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
|
|
18
|
+
import URI from '@theia/core/lib/common/uri';
|
|
19
|
+
import { WorkspaceServer, UntitledWorkspaceService, WorkspaceFileService } from '../common';
|
|
20
|
+
import { WindowService } from '@theia/core/lib/browser/window/window-service';
|
|
21
|
+
import { DEFAULT_WINDOW_HASH } from '@theia/core/lib/common/window';
|
|
22
|
+
import {
|
|
23
|
+
FrontendApplicationContribution, PreferenceServiceImpl, PreferenceScope, PreferenceSchemaProvider, LabelProvider
|
|
24
|
+
} from '@theia/core/lib/browser';
|
|
25
|
+
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
26
|
+
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
|
|
27
|
+
import { ILogger, Disposable, DisposableCollection, Emitter, Event, MaybePromise, MessageService, nls } from '@theia/core';
|
|
28
|
+
import { WorkspacePreferences } from './workspace-preferences';
|
|
29
|
+
import * as jsoncparser from 'jsonc-parser';
|
|
30
|
+
import * as Ajv from '@theia/core/shared/ajv';
|
|
31
|
+
import { FileStat, BaseStat } from '@theia/filesystem/lib/common/files';
|
|
32
|
+
import { FileService } from '@theia/filesystem/lib/browser/file-service';
|
|
33
|
+
import { WindowTitleService } from '@theia/core/lib/browser/window/window-title-service';
|
|
34
|
+
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
|
|
35
|
+
import { workspaceSchema, WorkspaceSchemaUpdater } from './workspace-schema-updater';
|
|
36
|
+
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
|
|
37
|
+
import { StopReason } from '@theia/core/lib/common/frontend-application-state';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The workspace service.
|
|
41
|
+
*/
|
|
42
|
+
@injectable()
|
|
43
|
+
export class WorkspaceService implements FrontendApplicationContribution {
|
|
44
|
+
|
|
45
|
+
protected _workspace: FileStat | undefined;
|
|
46
|
+
|
|
47
|
+
protected _roots: FileStat[] = [];
|
|
48
|
+
protected deferredRoots = new Deferred<FileStat[]>();
|
|
49
|
+
|
|
50
|
+
@inject(FileService)
|
|
51
|
+
protected readonly fileService: FileService;
|
|
52
|
+
|
|
53
|
+
@inject(WorkspaceServer)
|
|
54
|
+
protected readonly server: WorkspaceServer;
|
|
55
|
+
|
|
56
|
+
@inject(WindowService)
|
|
57
|
+
protected readonly windowService: WindowService;
|
|
58
|
+
|
|
59
|
+
@inject(ILogger)
|
|
60
|
+
protected logger: ILogger;
|
|
61
|
+
|
|
62
|
+
@inject(WorkspacePreferences)
|
|
63
|
+
protected preferences: WorkspacePreferences;
|
|
64
|
+
|
|
65
|
+
@inject(PreferenceServiceImpl)
|
|
66
|
+
protected readonly preferenceImpl: PreferenceServiceImpl;
|
|
67
|
+
|
|
68
|
+
@inject(PreferenceSchemaProvider)
|
|
69
|
+
protected readonly schemaProvider: PreferenceSchemaProvider;
|
|
70
|
+
|
|
71
|
+
@inject(EnvVariablesServer)
|
|
72
|
+
protected readonly envVariableServer: EnvVariablesServer;
|
|
73
|
+
|
|
74
|
+
@inject(MessageService)
|
|
75
|
+
protected readonly messageService: MessageService;
|
|
76
|
+
|
|
77
|
+
@inject(LabelProvider)
|
|
78
|
+
protected readonly labelProvider: LabelProvider;
|
|
79
|
+
|
|
80
|
+
@inject(FileSystemPreferences)
|
|
81
|
+
protected readonly fsPreferences: FileSystemPreferences;
|
|
82
|
+
|
|
83
|
+
@inject(WorkspaceSchemaUpdater)
|
|
84
|
+
protected readonly schemaUpdater: WorkspaceSchemaUpdater;
|
|
85
|
+
|
|
86
|
+
@inject(UntitledWorkspaceService)
|
|
87
|
+
protected readonly untitledWorkspaceService: UntitledWorkspaceService;
|
|
88
|
+
|
|
89
|
+
@inject(WorkspaceFileService)
|
|
90
|
+
protected readonly workspaceFileService: WorkspaceFileService;
|
|
91
|
+
|
|
92
|
+
@inject(WindowTitleService)
|
|
93
|
+
protected readonly windowTitleService: WindowTitleService;
|
|
94
|
+
|
|
95
|
+
protected _ready = new Deferred<void>();
|
|
96
|
+
get ready(): Promise<void> {
|
|
97
|
+
return this._ready.promise;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@postConstruct()
|
|
101
|
+
protected init(): void {
|
|
102
|
+
this.doInit();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
protected async doInit(): Promise<void> {
|
|
106
|
+
const wsUriString = await this.getDefaultWorkspaceUri();
|
|
107
|
+
const wsStat = await this.toFileStat(wsUriString);
|
|
108
|
+
await this.setWorkspace(wsStat);
|
|
109
|
+
|
|
110
|
+
this.fileService.onDidFilesChange(event => {
|
|
111
|
+
if (this._workspace && this._workspace.isFile && event.contains(this._workspace.resource)) {
|
|
112
|
+
this.updateWorkspace();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
this.fsPreferences.onPreferenceChanged(event => {
|
|
116
|
+
if (event.preferenceName === 'files.watcherExclude') {
|
|
117
|
+
this.refreshRootWatchers();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
this._ready.resolve();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Resolves to the default workspace URI as string.
|
|
125
|
+
*
|
|
126
|
+
* The default implementation tries to extract the default workspace location
|
|
127
|
+
* from the `window.location.hash`, then falls-back to the most recently
|
|
128
|
+
* used workspace root from the server.
|
|
129
|
+
*
|
|
130
|
+
* It is not ensured that the resolved workspace URI is valid, it can point
|
|
131
|
+
* to a non-existing location.
|
|
132
|
+
*/
|
|
133
|
+
protected getDefaultWorkspaceUri(): MaybePromise<string | undefined> {
|
|
134
|
+
return this.doGetDefaultWorkspaceUri();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
protected async doGetDefaultWorkspaceUri(): Promise<string | undefined> {
|
|
138
|
+
|
|
139
|
+
// If an empty window is explicitly requested do not restore a previous workspace.
|
|
140
|
+
// Note: `window.location.hash` includes leading "#" if non-empty.
|
|
141
|
+
if (window.location.hash === `#${DEFAULT_WINDOW_HASH}`) {
|
|
142
|
+
window.location.hash = '';
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Prefer the workspace path specified as the URL fragment, if present.
|
|
147
|
+
if (window.location.hash.length > 1) {
|
|
148
|
+
// Remove the leading # and decode the URI.
|
|
149
|
+
const wpPath = decodeURI(window.location.hash.substring(1));
|
|
150
|
+
const workspaceUri = new URI().withPath(wpPath).withScheme('file');
|
|
151
|
+
let workspaceStat: FileStat | undefined;
|
|
152
|
+
try {
|
|
153
|
+
workspaceStat = await this.fileService.resolve(workspaceUri);
|
|
154
|
+
} catch { }
|
|
155
|
+
if (workspaceStat && !workspaceStat.isDirectory && !this.isWorkspaceFile(workspaceStat)) {
|
|
156
|
+
this.messageService.error(`Not a valid workspace file: ${workspaceUri}`);
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
return workspaceUri.toString();
|
|
160
|
+
} else {
|
|
161
|
+
// Else, ask the server for its suggested workspace (usually the one
|
|
162
|
+
// specified on the CLI, or the most recent).
|
|
163
|
+
return this.server.getMostRecentlyUsedWorkspace();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Set the URL fragment to the given workspace path.
|
|
169
|
+
*/
|
|
170
|
+
protected setURLFragment(workspacePath: string): void {
|
|
171
|
+
window.location.hash = encodeURI(workspacePath);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
get roots(): Promise<FileStat[]> {
|
|
175
|
+
return this.deferredRoots.promise;
|
|
176
|
+
}
|
|
177
|
+
tryGetRoots(): FileStat[] {
|
|
178
|
+
return this._roots;
|
|
179
|
+
}
|
|
180
|
+
get workspace(): FileStat | undefined {
|
|
181
|
+
return this._workspace;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
protected readonly onWorkspaceChangeEmitter = new Emitter<FileStat[]>();
|
|
185
|
+
get onWorkspaceChanged(): Event<FileStat[]> {
|
|
186
|
+
return this.onWorkspaceChangeEmitter.event;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
protected readonly onWorkspaceLocationChangedEmitter = new Emitter<FileStat | undefined>();
|
|
190
|
+
get onWorkspaceLocationChanged(): Event<FileStat | undefined> {
|
|
191
|
+
return this.onWorkspaceLocationChangedEmitter.event;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
protected readonly toDisposeOnWorkspace = new DisposableCollection();
|
|
195
|
+
protected async setWorkspace(workspaceStat: FileStat | undefined): Promise<void> {
|
|
196
|
+
if (this._workspace && workspaceStat &&
|
|
197
|
+
this._workspace.resource === workspaceStat.resource &&
|
|
198
|
+
this._workspace.mtime === workspaceStat.mtime &&
|
|
199
|
+
this._workspace.etag === workspaceStat.etag &&
|
|
200
|
+
this._workspace.size === workspaceStat.size) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.toDisposeOnWorkspace.dispose();
|
|
204
|
+
this._workspace = workspaceStat;
|
|
205
|
+
if (this._workspace) {
|
|
206
|
+
const uri = this._workspace.resource;
|
|
207
|
+
if (this._workspace.isFile) {
|
|
208
|
+
this.toDisposeOnWorkspace.push(this.fileService.watch(uri));
|
|
209
|
+
this.onWorkspaceLocationChangedEmitter.fire(this._workspace);
|
|
210
|
+
}
|
|
211
|
+
this.setURLFragment(uri.path.toString());
|
|
212
|
+
} else {
|
|
213
|
+
this.setURLFragment('');
|
|
214
|
+
}
|
|
215
|
+
this.updateTitle();
|
|
216
|
+
await this.server.setMostRecentlyUsedWorkspace(this._workspace ? this._workspace.resource.toString() : '');
|
|
217
|
+
await this.updateWorkspace();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
protected async updateWorkspace(): Promise<void> {
|
|
221
|
+
await this.updateRoots();
|
|
222
|
+
this.watchRoots();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
protected async updateRoots(): Promise<void> {
|
|
226
|
+
const newRoots = await this.computeRoots();
|
|
227
|
+
let rootsChanged = false;
|
|
228
|
+
if (newRoots.length !== this._roots.length || newRoots.length === 0) {
|
|
229
|
+
rootsChanged = true;
|
|
230
|
+
} else {
|
|
231
|
+
for (const newRoot of newRoots) {
|
|
232
|
+
if (!this._roots.some(r => r.resource.toString() === newRoot.resource.toString())) {
|
|
233
|
+
rootsChanged = true;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (rootsChanged) {
|
|
239
|
+
this._roots = newRoots;
|
|
240
|
+
this.deferredRoots.resolve(this._roots); // in order to resolve first
|
|
241
|
+
this.deferredRoots = new Deferred<FileStat[]>();
|
|
242
|
+
this.deferredRoots.resolve(this._roots);
|
|
243
|
+
this.onWorkspaceChangeEmitter.fire(this._roots);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
protected async computeRoots(): Promise<FileStat[]> {
|
|
248
|
+
const roots: FileStat[] = [];
|
|
249
|
+
if (this._workspace) {
|
|
250
|
+
if (this._workspace.isDirectory) {
|
|
251
|
+
return [this._workspace];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const workspaceData = await this.getWorkspaceDataFromFile();
|
|
255
|
+
if (workspaceData) {
|
|
256
|
+
for (const { path } of workspaceData.folders) {
|
|
257
|
+
const valid = await this.toValidRoot(path);
|
|
258
|
+
if (valid) {
|
|
259
|
+
roots.push(valid);
|
|
260
|
+
} else {
|
|
261
|
+
roots.push(FileStat.dir(path));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return roots;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
protected async getWorkspaceDataFromFile(): Promise<WorkspaceData | undefined> {
|
|
270
|
+
if (this._workspace && await this.fileService.exists(this._workspace.resource)) {
|
|
271
|
+
if (this._workspace.isDirectory) {
|
|
272
|
+
return {
|
|
273
|
+
folders: [{ path: this._workspace.resource.toString() }]
|
|
274
|
+
};
|
|
275
|
+
} else if (this.isWorkspaceFile(this._workspace)) {
|
|
276
|
+
const stat = await this.fileService.read(this._workspace.resource);
|
|
277
|
+
const strippedContent = jsoncparser.stripComments(stat.value);
|
|
278
|
+
const data = jsoncparser.parse(strippedContent);
|
|
279
|
+
if (data && WorkspaceData.is(data)) {
|
|
280
|
+
return WorkspaceData.transformToAbsolute(data, stat);
|
|
281
|
+
}
|
|
282
|
+
this.logger.error(`Unable to retrieve workspace data from the file: '${this.labelProvider.getLongName(this._workspace)}'. Please check if the file is corrupted.`);
|
|
283
|
+
} else {
|
|
284
|
+
this.logger.warn(`Not a valid workspace file: ${this.labelProvider.getLongName(this._workspace)}`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
protected updateTitle(): void {
|
|
290
|
+
let rootName: string | undefined;
|
|
291
|
+
let rootPath: string | undefined;
|
|
292
|
+
if (this._workspace) {
|
|
293
|
+
const displayName = this._workspace.name;
|
|
294
|
+
const fullName = this._workspace.resource.path.toString();
|
|
295
|
+
if (this.isWorkspaceFile(this._workspace)) {
|
|
296
|
+
if (this.isUntitledWorkspace(this._workspace.resource)) {
|
|
297
|
+
const untitled = nls.localizeByDefault('Untitled (Workspace)');
|
|
298
|
+
rootName = untitled;
|
|
299
|
+
rootPath = untitled;
|
|
300
|
+
} else {
|
|
301
|
+
rootName = displayName.slice(0, displayName.lastIndexOf('.'));
|
|
302
|
+
rootPath = fullName.slice(0, fullName.lastIndexOf('.'));
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
rootName = displayName;
|
|
306
|
+
rootPath = fullName;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
this.windowTitleService.update({
|
|
310
|
+
rootName,
|
|
311
|
+
rootPath
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* on unload, we set our workspace root as the last recently used on the backend.
|
|
317
|
+
*/
|
|
318
|
+
onStop(): void {
|
|
319
|
+
this.server.setMostRecentlyUsedWorkspace(this._workspace ? this._workspace.resource.toString() : '');
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async recentWorkspaces(): Promise<string[]> {
|
|
323
|
+
return this.server.getRecentWorkspaces();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async removeRecentWorkspace(uri: string): Promise<void> {
|
|
327
|
+
return this.server.removeRecentWorkspace(uri);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Returns `true` if theia has an opened workspace or folder
|
|
332
|
+
* @returns {boolean}
|
|
333
|
+
*/
|
|
334
|
+
get opened(): boolean {
|
|
335
|
+
return !!this._workspace;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Returns `true` if a multiple-root workspace is currently open.
|
|
340
|
+
* @returns {boolean}
|
|
341
|
+
*/
|
|
342
|
+
get isMultiRootWorkspaceOpened(): boolean {
|
|
343
|
+
return !!this.workspace && !this.workspace.isDirectory;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Opens directory, or recreates a workspace from the file that `uri` points to.
|
|
348
|
+
*/
|
|
349
|
+
open(uri: URI, options?: WorkspaceInput): void {
|
|
350
|
+
this.doOpen(uri, options);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
protected async doOpen(uri: URI, options?: WorkspaceInput): Promise<URI | undefined> {
|
|
354
|
+
const stat = await this.toFileStat(uri);
|
|
355
|
+
if (stat) {
|
|
356
|
+
if (!stat.isDirectory && !this.isWorkspaceFile(stat)) {
|
|
357
|
+
const message = `Not a valid workspace: ${uri.path.toString()}`;
|
|
358
|
+
this.messageService.error(message);
|
|
359
|
+
throw new Error(message);
|
|
360
|
+
}
|
|
361
|
+
// The same window has to be preserved too (instead of opening a new one), if the workspace root is not yet available and we are setting it for the first time.
|
|
362
|
+
// Option passed as parameter has the highest priority (for api developers), then the preference, then the default.
|
|
363
|
+
await this.roots;
|
|
364
|
+
const { preserveWindow } = {
|
|
365
|
+
preserveWindow: this.preferences['workspace.preserveWindow'] || !this.opened,
|
|
366
|
+
...options
|
|
367
|
+
};
|
|
368
|
+
await this.server.setMostRecentlyUsedWorkspace(uri.toString());
|
|
369
|
+
if (preserveWindow) {
|
|
370
|
+
this._workspace = stat;
|
|
371
|
+
}
|
|
372
|
+
this.openWindow(stat, Object.assign(options ?? {}, { preserveWindow }));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
throw new Error('Invalid workspace root URI. Expected an existing directory or workspace file.');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Adds root folder(s) to the workspace
|
|
380
|
+
* @param uris URI or URIs of the root folder(s) to add
|
|
381
|
+
*/
|
|
382
|
+
async addRoot(uris: URI[] | URI): Promise<void> {
|
|
383
|
+
const toAdd = Array.isArray(uris) ? uris : [uris];
|
|
384
|
+
await this.spliceRoots(this._roots.length, 0, ...toAdd);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Removes root folder(s) from workspace.
|
|
389
|
+
*/
|
|
390
|
+
async removeRoots(uris: URI[]): Promise<void> {
|
|
391
|
+
if (!this.opened) {
|
|
392
|
+
throw new Error('Folder cannot be removed as there is no active folder in the current workspace.');
|
|
393
|
+
}
|
|
394
|
+
if (this._workspace) {
|
|
395
|
+
const workspaceData = await this.getWorkspaceDataFromFile();
|
|
396
|
+
this._workspace = await this.writeWorkspaceFile(this._workspace,
|
|
397
|
+
WorkspaceData.buildWorkspaceData(
|
|
398
|
+
this._roots.filter(root => uris.findIndex(u => u.toString() === root.resource.toString()) < 0),
|
|
399
|
+
workspaceData
|
|
400
|
+
)
|
|
401
|
+
);
|
|
402
|
+
await this.updateWorkspace();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async spliceRoots(start: number, deleteCount?: number, ...rootsToAdd: URI[]): Promise<URI[]> {
|
|
407
|
+
if (!this._workspace || this._workspace.isDirectory) {
|
|
408
|
+
const untitledWorkspace = await this.getUntitledWorkspace();
|
|
409
|
+
await this.save(untitledWorkspace);
|
|
410
|
+
if (!this._workspace) {
|
|
411
|
+
throw new Error('Could not create new untitled workspace');
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const dedup = new Set<string>();
|
|
415
|
+
const roots = this._roots.map(root => (dedup.add(root.resource.toString()), root.resource.toString()));
|
|
416
|
+
const toAdd: string[] = [];
|
|
417
|
+
for (const root of rootsToAdd) {
|
|
418
|
+
const uri = root.toString();
|
|
419
|
+
if (!dedup.has(uri)) {
|
|
420
|
+
dedup.add(uri);
|
|
421
|
+
toAdd.push(uri);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const toRemove = roots.splice(start, deleteCount || 0, ...toAdd);
|
|
425
|
+
if (!toRemove.length && !toAdd.length) {
|
|
426
|
+
return [];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const currentData = await this.getWorkspaceDataFromFile();
|
|
430
|
+
const newData = WorkspaceData.buildWorkspaceData(roots, currentData);
|
|
431
|
+
await this.writeWorkspaceFile(this._workspace, newData);
|
|
432
|
+
await this.updateWorkspace();
|
|
433
|
+
return toRemove.map(root => new URI(root));
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async getUntitledWorkspace(): Promise<URI> {
|
|
437
|
+
const configDirURI = new URI(await this.envVariableServer.getConfigDirUri());
|
|
438
|
+
return this.untitledWorkspaceService.getUntitledWorkspaceUri(
|
|
439
|
+
configDirURI,
|
|
440
|
+
uri => this.fileService.exists(uri).then(exists => !exists),
|
|
441
|
+
() => this.messageService.warn(nls.localize(
|
|
442
|
+
'theia/workspace/untitled-cleanup',
|
|
443
|
+
'There appear to be many untitled workspace files. Please check {0} and remove any unused files.',
|
|
444
|
+
configDirURI.resolve('workspaces').path.fsPath())
|
|
445
|
+
),
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
protected async writeWorkspaceFile(workspaceFile: FileStat | undefined, workspaceData: WorkspaceData): Promise<FileStat | undefined> {
|
|
450
|
+
if (workspaceFile) {
|
|
451
|
+
const data = JSON.stringify(WorkspaceData.transformToRelative(workspaceData, workspaceFile));
|
|
452
|
+
const edits = jsoncparser.format(data, undefined, { tabSize: 2, insertSpaces: true, eol: '' });
|
|
453
|
+
const result = jsoncparser.applyEdits(data, edits);
|
|
454
|
+
await this.fileService.write(workspaceFile.resource, result);
|
|
455
|
+
return this.fileService.resolve(workspaceFile.resource);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Clears current workspace root.
|
|
461
|
+
*/
|
|
462
|
+
async close(): Promise<void> {
|
|
463
|
+
if (await this.windowService.isSafeToShutDown(StopReason.Reload)) {
|
|
464
|
+
this.windowService.setSafeToShutDown();
|
|
465
|
+
this._workspace = undefined;
|
|
466
|
+
this._roots.length = 0;
|
|
467
|
+
|
|
468
|
+
await this.server.setMostRecentlyUsedWorkspace('');
|
|
469
|
+
this.reloadWindow();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* returns a FileStat if the argument URI points to an existing directory. Otherwise, `undefined`.
|
|
475
|
+
*/
|
|
476
|
+
protected async toValidRoot(uri: URI | string | undefined): Promise<FileStat | undefined> {
|
|
477
|
+
const fileStat = await this.toFileStat(uri);
|
|
478
|
+
if (fileStat && fileStat.isDirectory) {
|
|
479
|
+
return fileStat;
|
|
480
|
+
}
|
|
481
|
+
return undefined;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* returns a FileStat if the argument URI points to a file or directory. Otherwise, `undefined`.
|
|
486
|
+
*/
|
|
487
|
+
protected async toFileStat(uri: URI | string | undefined): Promise<FileStat | undefined> {
|
|
488
|
+
if (!uri) {
|
|
489
|
+
return undefined;
|
|
490
|
+
}
|
|
491
|
+
let uriStr = uri.toString();
|
|
492
|
+
try {
|
|
493
|
+
if (uriStr.endsWith('/')) {
|
|
494
|
+
uriStr = uriStr.slice(0, -1);
|
|
495
|
+
}
|
|
496
|
+
const normalizedUri = new URI(uriStr).normalizePath();
|
|
497
|
+
return await this.fileService.resolve(normalizedUri);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
protected openWindow(uri: FileStat, options?: WorkspaceInput): void {
|
|
504
|
+
const workspacePath = uri.resource.path.toString();
|
|
505
|
+
|
|
506
|
+
if (this.shouldPreserveWindow(options)) {
|
|
507
|
+
this.reloadWindow(options);
|
|
508
|
+
} else {
|
|
509
|
+
try {
|
|
510
|
+
this.openNewWindow(workspacePath, options);
|
|
511
|
+
} catch (error) {
|
|
512
|
+
// Fall back to reloading the current window in case the browser has blocked the new window
|
|
513
|
+
this._workspace = uri;
|
|
514
|
+
this.logger.error(error.toString()).then(() => this.reloadWindow());
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
protected reloadWindow(options?: WorkspaceInput): void {
|
|
520
|
+
// Set the new workspace path as the URL fragment.
|
|
521
|
+
if (this._workspace !== undefined) {
|
|
522
|
+
this.setURLFragment(this._workspace.resource.path.toString());
|
|
523
|
+
} else {
|
|
524
|
+
this.setURLFragment('');
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
this.windowService.reload();
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
protected openNewWindow(workspacePath: string, options?: WorkspaceInput): void {
|
|
531
|
+
const url = new URL(window.location.href);
|
|
532
|
+
url.hash = encodeURI(workspacePath);
|
|
533
|
+
this.windowService.openNewWindow(url.toString());
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
protected shouldPreserveWindow(options?: WorkspaceInput): boolean {
|
|
537
|
+
return options !== undefined && !!options.preserveWindow;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Return true if one of the paths in paths array is present in the workspace
|
|
542
|
+
* NOTE: You should always explicitly use `/` as the separator between the path segments.
|
|
543
|
+
*/
|
|
544
|
+
async containsSome(paths: string[]): Promise<boolean> {
|
|
545
|
+
await this.roots;
|
|
546
|
+
if (this.opened) {
|
|
547
|
+
for (const root of this._roots) {
|
|
548
|
+
const uri = root.resource;
|
|
549
|
+
for (const path of paths) {
|
|
550
|
+
const fileUri = uri.resolve(path);
|
|
551
|
+
const exists = await this.fileService.exists(fileUri);
|
|
552
|
+
if (exists) {
|
|
553
|
+
return exists;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return false;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* `true` if the current workspace is configured using a configuration file.
|
|
563
|
+
*
|
|
564
|
+
* `false` if there is no workspace or the workspace is simply a folder.
|
|
565
|
+
*/
|
|
566
|
+
get saved(): boolean {
|
|
567
|
+
return !!this._workspace && !this._workspace.isDirectory;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Save workspace data into a file
|
|
572
|
+
* @param uri URI or FileStat of the workspace file
|
|
573
|
+
*/
|
|
574
|
+
async save(uri: URI | FileStat): Promise<void> {
|
|
575
|
+
const resource = uri instanceof URI ? uri : uri.resource;
|
|
576
|
+
if (!await this.fileService.exists(resource)) {
|
|
577
|
+
await this.fileService.create(resource);
|
|
578
|
+
}
|
|
579
|
+
const workspaceData: WorkspaceData = { folders: [], settings: {} };
|
|
580
|
+
if (!this.saved) {
|
|
581
|
+
for (const p of Object.keys(this.schemaProvider.getCombinedSchema().properties)) {
|
|
582
|
+
if (this.schemaProvider.isValidInScope(p, PreferenceScope.Folder)) {
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const preferences = this.preferenceImpl.inspect(p);
|
|
586
|
+
if (preferences && preferences.workspaceValue) {
|
|
587
|
+
workspaceData.settings![p] = preferences.workspaceValue;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
let stat = await this.toFileStat(resource);
|
|
592
|
+
Object.assign(workspaceData, await this.getWorkspaceDataFromFile());
|
|
593
|
+
stat = await this.writeWorkspaceFile(stat, WorkspaceData.buildWorkspaceData(this._roots, workspaceData));
|
|
594
|
+
await this.server.setMostRecentlyUsedWorkspace(resource.toString());
|
|
595
|
+
// If saving a workspace based on an untitled workspace, delete the old file.
|
|
596
|
+
const toDelete = this.isUntitledWorkspace(this.workspace?.resource) && this.workspace!.resource;
|
|
597
|
+
await this.setWorkspace(stat);
|
|
598
|
+
if (toDelete && stat && !toDelete.isEqual(stat.resource)) {
|
|
599
|
+
await this.fileService.delete(toDelete).catch(() => { });
|
|
600
|
+
}
|
|
601
|
+
this.onWorkspaceLocationChangedEmitter.fire(stat);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
protected readonly rootWatchers = new Map<string, Disposable>();
|
|
605
|
+
|
|
606
|
+
protected async watchRoots(): Promise<void> {
|
|
607
|
+
const rootUris = new Set(this._roots.map(r => r.resource.toString()));
|
|
608
|
+
for (const [uri, watcher] of this.rootWatchers.entries()) {
|
|
609
|
+
if (!rootUris.has(uri)) {
|
|
610
|
+
watcher.dispose();
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
for (const root of this._roots) {
|
|
614
|
+
this.watchRoot(root);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
protected async refreshRootWatchers(): Promise<void> {
|
|
619
|
+
for (const watcher of this.rootWatchers.values()) {
|
|
620
|
+
watcher.dispose();
|
|
621
|
+
}
|
|
622
|
+
await this.watchRoots();
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
protected async watchRoot(root: FileStat): Promise<void> {
|
|
626
|
+
const uriStr = root.resource.toString();
|
|
627
|
+
if (this.rootWatchers.has(uriStr)) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
const excludes = this.getExcludes(uriStr);
|
|
631
|
+
const watcher = this.fileService.watch(new URI(uriStr), {
|
|
632
|
+
recursive: true,
|
|
633
|
+
excludes
|
|
634
|
+
});
|
|
635
|
+
this.rootWatchers.set(uriStr, new DisposableCollection(
|
|
636
|
+
watcher,
|
|
637
|
+
Disposable.create(() => this.rootWatchers.delete(uriStr))
|
|
638
|
+
));
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
protected getExcludes(uri: string): string[] {
|
|
642
|
+
const patterns = this.fsPreferences.get('files.watcherExclude', undefined, uri);
|
|
643
|
+
return Object.keys(patterns).filter(pattern => patterns[pattern]);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Returns the workspace root uri that the given file belongs to.
|
|
648
|
+
* In case that the file is found in more than one workspace roots, returns the root that is closest to the file.
|
|
649
|
+
* If the file is not from the current workspace, returns `undefined`.
|
|
650
|
+
* @param uri URI of the file
|
|
651
|
+
*/
|
|
652
|
+
getWorkspaceRootUri(uri: URI | undefined): URI | undefined {
|
|
653
|
+
if (!uri) {
|
|
654
|
+
const root = this.tryGetRoots()[0];
|
|
655
|
+
if (root) {
|
|
656
|
+
return root.resource;
|
|
657
|
+
}
|
|
658
|
+
return undefined;
|
|
659
|
+
}
|
|
660
|
+
const rootUris: URI[] = [];
|
|
661
|
+
for (const root of this.tryGetRoots()) {
|
|
662
|
+
const rootUri = root.resource;
|
|
663
|
+
if (rootUri && rootUri.isEqualOrParent(uri)) {
|
|
664
|
+
rootUris.push(rootUri);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return rootUris.sort((r1, r2) => r2.toString().length - r1.toString().length)[0];
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
areWorkspaceRoots(uris: URI[]): boolean {
|
|
671
|
+
if (!uris.length) {
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
const rootUris = new Set(this.tryGetRoots().map(root => root.resource.toString()));
|
|
675
|
+
return uris.every(uri => rootUris.has(uri.toString()));
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Check if the file should be considered as a workspace file.
|
|
680
|
+
*
|
|
681
|
+
* Example: We should not try to read the contents of an .exe file.
|
|
682
|
+
*/
|
|
683
|
+
protected isWorkspaceFile(candidate: FileStat | URI): boolean {
|
|
684
|
+
return this.workspaceFileService.isWorkspaceFile(candidate);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
isUntitledWorkspace(candidate?: URI): boolean {
|
|
688
|
+
return this.untitledWorkspaceService.isUntitledWorkspace(candidate);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
async isSafeToReload(withURI?: URI): Promise<boolean> {
|
|
692
|
+
return !withURI || !this.untitledWorkspaceService.isUntitledWorkspace(withURI) || new URI(await this.getDefaultWorkspaceUri()).isEqual(withURI);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
*
|
|
697
|
+
* @param key the property key under which to store the schema (e.g. tasks, launch)
|
|
698
|
+
* @param schema the schema for the property. If none is supplied, the update is treated as a deletion.
|
|
699
|
+
*/
|
|
700
|
+
async updateSchema(key: string, schema?: IJSONSchema): Promise<boolean> {
|
|
701
|
+
return this.schemaUpdater.updateSchema({ key, schema });
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export interface WorkspaceInput {
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Tests whether the same window should be used or a new one has to be opened after setting the workspace root. By default it is `false`.
|
|
709
|
+
*/
|
|
710
|
+
preserveWindow?: boolean;
|
|
711
|
+
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
export interface WorkspaceData {
|
|
715
|
+
folders: Array<{ path: string, name?: string }>;
|
|
716
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
717
|
+
[key: string]: { [id: string]: any };
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
export namespace WorkspaceData {
|
|
721
|
+
const validateSchema = new Ajv().compile(workspaceSchema);
|
|
722
|
+
|
|
723
|
+
export function is(data: unknown): data is WorkspaceData {
|
|
724
|
+
return !!validateSchema(data);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export function buildWorkspaceData(folders: string[] | FileStat[], additionalFields?: Partial<WorkspaceData>): WorkspaceData {
|
|
728
|
+
const roots = new Set<string>();
|
|
729
|
+
if (folders.length > 0) {
|
|
730
|
+
if (typeof folders[0] !== 'string') {
|
|
731
|
+
(<FileStat[]>folders).forEach(folder => roots.add(folder.resource.toString()));
|
|
732
|
+
} else {
|
|
733
|
+
(<string[]>folders).forEach(folder => roots.add(folder));
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
const data: WorkspaceData = {
|
|
737
|
+
folders: Array.from(roots, folder => ({ path: folder }))
|
|
738
|
+
};
|
|
739
|
+
if (additionalFields) {
|
|
740
|
+
delete additionalFields.folders;
|
|
741
|
+
Object.assign(data, additionalFields);
|
|
742
|
+
}
|
|
743
|
+
return data;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
export function transformToRelative(data: WorkspaceData, workspaceFile?: FileStat): WorkspaceData {
|
|
747
|
+
const folderUris: string[] = [];
|
|
748
|
+
const workspaceFileUri = new URI(workspaceFile ? workspaceFile.resource.toString() : '').withScheme('file');
|
|
749
|
+
for (const { path } of data.folders) {
|
|
750
|
+
const folderUri = new URI(path).withScheme('file');
|
|
751
|
+
const rel = workspaceFileUri.parent.relative(folderUri);
|
|
752
|
+
if (rel) {
|
|
753
|
+
folderUris.push(rel.toString());
|
|
754
|
+
} else {
|
|
755
|
+
folderUris.push(folderUri.toString());
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return buildWorkspaceData(folderUris, data);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
export function transformToAbsolute(data: WorkspaceData, workspaceFile?: BaseStat): WorkspaceData {
|
|
762
|
+
if (workspaceFile) {
|
|
763
|
+
const folders: string[] = [];
|
|
764
|
+
for (const folder of data.folders) {
|
|
765
|
+
const path = folder.path;
|
|
766
|
+
if (path.startsWith('file:///')) {
|
|
767
|
+
folders.push(path);
|
|
768
|
+
} else {
|
|
769
|
+
const absolutePath = workspaceFile.resource.withScheme('file').parent.resolveToAbsolute(path)?.toString();
|
|
770
|
+
if (absolutePath) {
|
|
771
|
+
folders.push(absolutePath.toString());
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
}
|
|
776
|
+
return Object.assign(data, buildWorkspaceData(folders, data));
|
|
777
|
+
}
|
|
778
|
+
return data;
|
|
779
|
+
}
|
|
780
|
+
}
|