-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathworkbench.ts
249 lines (222 loc) · 8.2 KB
/
workbench.ts
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
import * as os from "os";
import { IProgress, INotificationHandle } from "@coder/ide";
import { logger } from "@coder/logger";
import { client } from "./client";
import "./fill/platform";
import "./fill/dom";
import "./fill/codeEditor";
import "./fill/environmentService";
import "./fill/labels";
import "./fill/menuRegistry";
import "./fill/mouseEvent";
import "./fill/storageDatabase";
import "./fill/vscodeTextmate";
import "./fill/windowsService";
import "./fill/workbenchRegistry";
import "./fill/workspacesService";
import * as paths from "./fill/paths";
import { PasteAction } from "./fill/paste";
import { ExplorerItem, ExplorerModel } from "vs/workbench/contrib/files/common/explorerModel";
import { IEditorGroup } from "vs/workbench/services/editor/common/editorGroupsService";
import { IEditorService, IResourceEditor } from "vs/workbench/services/editor/common/editorService";
import { INotificationService } from "vs/platform/notification/common/notification";
import { IProgressService2, ProgressLocation } from "vs/platform/progress/common/progress";
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from "vs/platform/workspaces/common/workspaces";
import { IWindowsService, IWindowConfiguration } from "vs/platform/windows/common/windows";
import { LogLevel } from "vs/platform/log/common/log";
import { RawContextKey, IContextKeyService } from "vs/platform/contextkey/common/contextkey";
import { ServiceCollection } from "vs/platform/instantiation/common/serviceCollection";
import { URI } from "vs/base/common/uri";
import { BackupMainService } from "vs/platform/backup/electron-main/backupMainService";
import { IInstantiationService } from "vs/platform/instantiation/common/instantiation";
/**
* Initializes VS Code and provides a way to call into general client
* functionality.
*/
export class Workbench {
public readonly retry = client.retry;
private readonly windowId = parseInt(new Date().toISOString().replace(/[-:.TZ]/g, ""), 10);
private _serviceCollection: ServiceCollection | undefined;
private _clipboardContextKey: RawContextKey<boolean> | undefined;
/**
* Handle a drop event on the file explorer.
*/
public async handleExternalDrop(target: ExplorerItem | ExplorerModel, originalEvent: DragEvent): Promise<void> {
await client.upload.uploadDropped(
originalEvent,
(target instanceof ExplorerItem ? target : target.roots[0]).resource,
);
}
/**
* Handle a drop event on the editor.
*/
public handleDrop(event: DragEvent, resolveTargetGroup: () => IEditorGroup, afterDrop: (targetGroup: IEditorGroup) => void, targetIndex?: number): void {
client.upload.uploadDropped(event, URI.file(paths.getWorkingDirectory())).then(async (paths) => {
const uris = paths.map((p) => URI.file(p));
if (uris.length) {
await (this.serviceCollection.get(IWindowsService) as IWindowsService).addRecentlyOpened(uris);
}
const editors: IResourceEditor[] = uris.map(uri => ({
resource: uri,
options: {
pinned: true,
index: targetIndex,
},
}));
const targetGroup = resolveTargetGroup();
await (this.serviceCollection.get(IEditorService) as IEditorService).openEditors(editors, targetGroup);
afterDrop(targetGroup);
}).catch((error) => {
logger.error(error.message);
});
}
/**
* Use to toggle the paste option inside editors based on the native clipboard.
*/
public get clipboardContextKey(): RawContextKey<boolean> {
if (!this._clipboardContextKey) {
throw new Error("Trying to access clipboard context key before it has been set");
}
return this._clipboardContextKey;
}
public get clipboardText(): Promise<string> {
return client.clipboard.readText();
}
/**
* Create a paste action for use in text inputs.
*/
public get pasteAction(): PasteAction {
return new PasteAction();
}
public set workspace(ws: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined) {
if (typeof ws === "undefined") {
window.localStorage.removeItem("workspace");
} else {
window.localStorage.setItem("workspace", JSON.stringify(ws));
}
location.reload();
}
public get workspace(): undefined | IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier {
const ws = window.localStorage.getItem("workspace");
try {
return JSON.parse(ws!);
} catch (ex) {
return undefined;
}
}
public get serviceCollection(): ServiceCollection {
if (!this._serviceCollection) {
throw new Error("Trying to access service collection before it has been set");
}
return this._serviceCollection;
}
public set serviceCollection(collection: ServiceCollection) {
this._serviceCollection = collection;
// TODO: If possible it might be better to start the app from vs/code/electron-main/app.
// For now, manually initialize services from there as needed.
const inst = this._serviceCollection.get(IInstantiationService) as IInstantiationService;
const backupMainService = inst.createInstance(BackupMainService) as BackupMainService;
backupMainService.initialize().catch((error) => {
logger.error(error.message);
});
client.progressService = {
start: <T>(title: string, task: (progress: IProgress) => Promise<T>, onCancel: () => void): Promise<T> => {
let lastProgress = 0;
return (this.serviceCollection.get(IProgressService2) as IProgressService2).withProgress({
location: ProgressLocation.Notification,
title,
cancellable: true,
}, (progress) => {
return task({
report: (p): void => {
progress.report({ increment: p - lastProgress });
lastProgress = p;
},
});
}, () => {
onCancel();
});
},
};
client.notificationService = {
error: (error: Error): void => (this.serviceCollection.get(INotificationService) as INotificationService).error(error),
prompt: (severity, message, buttons, onCancel): INotificationHandle => {
const handle = (this.serviceCollection.get(INotificationService) as INotificationService).prompt(
severity, message, buttons, { onCancel },
);
return {
close: (): void => handle.close(),
updateMessage: (message): void => handle.updateMessage(message),
updateButtons: (buttons): void => handle.updateActions({
primary: buttons.map((button) => ({
id: "",
label: button.label,
tooltip: "",
class: undefined,
enabled: true,
checked: false,
radio: false,
dispose: (): void => undefined,
run: (): Promise<void> => Promise.resolve(button.run()),
})),
}),
};
},
};
}
/**
* Start VS Code.
*/
public async initialize(): Promise<void> {
this._clipboardContextKey = new RawContextKey("nativeClipboard", client.clipboard.isEnabled);
const workspace = this.workspace || URI.file(paths.getWorkingDirectory());
// If we try to import this above, workbench will be undefined due to
// circular imports.
require("vs/workbench/workbench.main");
const { main } = require("vs/workbench/electron-browser/main");
const config: IWindowConfiguration = {
machineId: "1",
windowId: this.windowId,
logLevel: LogLevel.Info,
mainPid: 1,
appRoot: paths.getDefaultUserDataPath(),
execPath: os.tmpdir(),
userEnv: {},
nodeCachedDataDir: os.tmpdir(),
perfEntries: [],
_: [],
};
if ((workspace as IWorkspaceIdentifier).configPath) {
// tslint:disable-next-line:no-any
let wid: IWorkspaceIdentifier = (<any>Object).assign({}, workspace);
if (!URI.isUri(wid.configPath)) {
// Ensure that the configPath is a valid URI.
wid.configPath = URI.file(wid.configPath);
}
config.workspace = wid;
} else {
config.folderUri = workspace as URI;
}
try {
await main(config);
} catch (ex) {
if (ex.toString().indexOf("UriError") !== -1 || ex.toString().indexOf("backupPath") !== -1) {
/**
* Resolves the error of the workspace identifier being invalid.
*/
// tslint:disable-next-line:no-console
console.error(ex);
this.workspace = undefined;
location.reload();
return;
}
}
const contextKeys = this.serviceCollection.get(IContextKeyService) as IContextKeyService;
const bounded = this.clipboardContextKey.bindTo(contextKeys);
client.clipboard.onPermissionChange((enabled) => {
bounded.set(enabled);
});
client.clipboard.initialize();
}
}
export const workbench = new Workbench();