/
filesystem-frontend-contribution.ts
381 lines (338 loc) · 15.3 KB
/
filesystem-frontend-contribution.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
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
// *****************************************************************************
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { nls } from '@theia/core';
import {
ApplicationShell,
CommonCommands,
CorePreferences,
ExpandableTreeNode,
FrontendApplication,
FrontendApplicationContribution,
NavigatableWidget, NavigatableWidgetOptions,
OpenerService,
Saveable,
StatefulWidget,
WidgetManager,
open
} from '@theia/core/lib/browser';
import { MimeService } from '@theia/core/lib/browser/mime-service';
import { TreeWidgetSelection } from '@theia/core/lib/browser/tree/tree-widget-selection';
import { Emitter, MaybePromise, SelectionService, isCancelled } from '@theia/core/lib/common';
import { Command, CommandContribution, CommandRegistry } from '@theia/core/lib/common/command';
import { Deferred } from '@theia/core/lib/common/promise-util';
import URI from '@theia/core/lib/common/uri';
import { environment } from '@theia/core/shared/@theia/application-package/lib/environment';
import { inject, injectable } from '@theia/core/shared/inversify';
import { UserWorkingDirectoryProvider } from '@theia/core/lib/browser/user-working-directory-provider';
import { FileChangeType, FileChangesEvent, FileOperation } from '../common/files';
import { FileDialogService, SaveFileDialogProps } from './file-dialog';
import { FileSelection } from './file-selection';
import { FileService, UserFileOperationEvent } from './file-service';
import { FileUploadResult, FileUploadService } from './file-upload-service';
import { FileSystemPreferences } from './filesystem-preferences';
export namespace FileSystemCommands {
export const UPLOAD = Command.toLocalizedCommand({
id: 'file.upload',
category: CommonCommands.FILE_CATEGORY,
label: 'Upload Files...'
}, 'theia/filesystem/uploadFiles', CommonCommands.FILE_CATEGORY_KEY);
}
export interface NavigatableWidgetMoveSnapshot {
dirty?: object,
view?: object
}
@injectable()
export class FileSystemFrontendContribution implements FrontendApplicationContribution, CommandContribution {
@inject(ApplicationShell)
protected readonly shell: ApplicationShell;
@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;
@inject(MimeService)
protected readonly mimeService: MimeService;
@inject(FileSystemPreferences)
protected readonly preferences: FileSystemPreferences;
@inject(CorePreferences)
protected readonly corePreferences: CorePreferences;
@inject(SelectionService)
protected readonly selectionService: SelectionService;
@inject(FileUploadService)
protected readonly uploadService: FileUploadService;
@inject(FileService)
protected readonly fileService: FileService;
@inject(FileDialogService)
protected readonly fileDialogService: FileDialogService;
@inject(OpenerService)
protected readonly openerService: OpenerService;
@inject(UserWorkingDirectoryProvider)
protected readonly workingDirectory: UserWorkingDirectoryProvider;
protected onDidChangeEditorFileEmitter = new Emitter<{ editor: NavigatableWidget, type: FileChangeType }>();
readonly onDidChangeEditorFile = this.onDidChangeEditorFileEmitter.event;
protected readonly userOperations = new Map<number, Deferred<void>>();
protected queueUserOperation(event: UserFileOperationEvent): void {
const moveOperation = new Deferred<void>();
this.userOperations.set(event.correlationId, moveOperation);
this.run(() => moveOperation.promise);
}
protected resolveUserOperation(event: UserFileOperationEvent): void {
const operation = this.userOperations.get(event.correlationId);
if (operation) {
this.userOperations.delete(event.correlationId);
operation.resolve();
}
}
initialize(): void {
this.fileService.onDidFilesChange(event => this.run(() => this.updateWidgets(event)));
this.fileService.onWillRunUserOperation(event => {
this.queueUserOperation(event);
event.waitUntil(this.runEach((uri, widget) => this.pushMove(uri, widget, event)));
});
this.fileService.onDidFailUserOperation(event => event.waitUntil((async () => {
await this.runEach((uri, widget) => this.revertMove(uri, widget, event));
this.resolveUserOperation(event);
})()));
this.fileService.onDidRunUserOperation(event => event.waitUntil((async () => {
await this.runEach((uri, widget) => this.applyMove(uri, widget, event));
this.resolveUserOperation(event);
})()));
}
onStart?(app: FrontendApplication): MaybePromise<void> {
this.updateAssociations();
this.preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'files.associations') {
this.updateAssociations();
}
});
}
registerCommands(commands: CommandRegistry): void {
commands.registerCommand(FileSystemCommands.UPLOAD, {
isEnabled: (...args: unknown[]) => {
const selection = this.getSelection(...args);
return !!selection && this.canUpload(selection);
},
isVisible: () => !environment.electron.is(),
execute: (...args: unknown[]) => {
const selection = this.getSelection(...args);
if (selection) {
return this.upload(selection);
}
}
});
commands.registerCommand(CommonCommands.NEW_FILE, {
execute: (...args: unknown[]) => {
this.handleNewFileCommand(args);
}
});
}
protected canUpload({ fileStat }: FileSelection): boolean {
return !environment.electron.is() && fileStat.isDirectory;
}
protected async upload(selection: FileSelection): Promise<FileUploadResult | undefined> {
try {
const source = TreeWidgetSelection.getSource(this.selectionService.selection);
const fileUploadResult = await this.uploadService.upload(selection.fileStat.resource);
if (ExpandableTreeNode.is(selection) && source) {
await source.model.expandNode(selection);
}
return fileUploadResult;
} catch (e) {
if (!isCancelled(e)) {
console.error(e);
}
}
}
/**
* Opens a save dialog to create a new file.
*
* @param args The first argument is the name of the new file. The second argument is the parent directory URI.
*/
protected async handleNewFileCommand(args: unknown[]): Promise<void> {
const fileName = (args !== undefined && typeof args[0] === 'string') ? args[0] : undefined;
const title = nls.localizeByDefault('Create File');
const props: SaveFileDialogProps = { title, saveLabel: title, inputValue: fileName };
const dirUri = (args[1] instanceof URI) ? args[1] : await this.workingDirectory.getUserWorkingDir();
const directory = await this.fileService.resolve(dirUri);
const filePath = await this.fileDialogService.showSaveDialog(props, directory.isDirectory ? directory : undefined);
if (filePath) {
const file = await this.fileService.createFile(filePath);
open(this.openerService, file.resource);
}
}
protected getSelection(...args: unknown[]): FileSelection | undefined {
const { selection } = this.selectionService;
return this.toSelection(args[0]) ?? (Array.isArray(selection) ? selection.find(FileSelection.is) : this.toSelection(selection));
};
protected toSelection(arg: unknown): FileSelection | undefined {
return FileSelection.is(arg) ? arg : undefined;
}
protected pendingOperation = Promise.resolve();
protected run(operation: () => MaybePromise<void>): Promise<void> {
return this.pendingOperation = this.pendingOperation.then(async () => {
try {
await operation();
} catch (e) {
console.error(e);
}
});
}
protected async runEach(participant: (resourceUri: URI, widget: NavigatableWidget) => Promise<void>): Promise<void> {
const promises: Promise<void>[] = [];
for (const [resourceUri, widget] of NavigatableWidget.get(this.shell.widgets)) {
promises.push(participant(resourceUri, widget));
}
await Promise.all(promises);
}
protected readonly moveSnapshots = new Map<string, NavigatableWidgetMoveSnapshot>();
protected popMoveSnapshot(resourceUri: URI): NavigatableWidgetMoveSnapshot | undefined {
const snapshotKey = resourceUri.toString();
const snapshot = this.moveSnapshots.get(snapshotKey);
if (snapshot) {
this.moveSnapshots.delete(snapshotKey);
}
return snapshot;
}
protected applyMoveSnapshot(widget: NavigatableWidget, snapshot: NavigatableWidgetMoveSnapshot | undefined): void {
if (!snapshot) {
return undefined;
}
if (snapshot.dirty) {
const saveable = Saveable.get(widget);
if (saveable && saveable.applySnapshot) {
saveable.applySnapshot(snapshot.dirty);
}
}
if (snapshot.view && StatefulWidget.is(widget)) {
widget.restoreState(snapshot.view);
}
}
protected async pushMove(resourceUri: URI, widget: NavigatableWidget, event: UserFileOperationEvent): Promise<void> {
const newResourceUri = this.createMoveToUri(resourceUri, widget, event);
if (!newResourceUri) {
return;
}
const snapshot: NavigatableWidgetMoveSnapshot = {};
const saveable = Saveable.get(widget);
if (StatefulWidget.is(widget)) {
snapshot.view = widget.storeState();
}
if (saveable && saveable.dirty) {
if (saveable.createSnapshot) {
snapshot.dirty = saveable.createSnapshot();
}
if (saveable.revert) {
await saveable.revert({ soft: true });
}
}
this.moveSnapshots.set(newResourceUri.toString(), snapshot);
}
protected async revertMove(resourceUri: URI, widget: NavigatableWidget, event: UserFileOperationEvent): Promise<void> {
const newResourceUri = this.createMoveToUri(resourceUri, widget, event);
if (!newResourceUri) {
return;
}
const snapshot = this.popMoveSnapshot(newResourceUri);
this.applyMoveSnapshot(widget, snapshot);
}
protected async applyMove(resourceUri: URI, widget: NavigatableWidget, event: UserFileOperationEvent): Promise<void> {
const newResourceUri = this.createMoveToUri(resourceUri, widget, event);
if (!newResourceUri) {
return;
}
const snapshot = this.popMoveSnapshot(newResourceUri);
const description = this.widgetManager.getDescription(widget);
if (!description) {
return;
}
const { factoryId, options } = description;
if (!NavigatableWidgetOptions.is(options)) {
return;
}
const newWidget = await this.widgetManager.getOrCreateWidget<NavigatableWidget>(factoryId, <NavigatableWidgetOptions>{
...options,
uri: newResourceUri.toString()
});
this.applyMoveSnapshot(newWidget, snapshot);
const area = this.shell.getAreaFor(widget) || 'main';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pending: Promise<any>[] = [this.shell.addWidget(newWidget, {
area, ref: widget
})];
if (this.shell.activeWidget === widget) {
pending.push(this.shell.activateWidget(newWidget.id));
} else if (widget.isVisible) {
pending.push(this.shell.revealWidget(newWidget.id));
}
pending.push(this.shell.closeWidget(widget.id, { save: false }));
await Promise.all(pending);
}
protected createMoveToUri(resourceUri: URI, widget: NavigatableWidget, event: UserFileOperationEvent): URI | undefined {
if (event.operation !== FileOperation.MOVE) {
return undefined;
}
const path = event.source?.relative(resourceUri);
const targetUri = path && event.target.resolve(path);
return targetUri && widget.createMoveToUri(targetUri);
}
protected readonly deletedSuffix = `(${nls.localizeByDefault('Deleted')})`;
protected async updateWidgets(event: FileChangesEvent): Promise<void> {
if (!event.gotDeleted() && !event.gotAdded()) {
return;
}
const dirty = new Set<string>();
const toClose = new Map<string, NavigatableWidget[]>();
for (const [uri, widget] of NavigatableWidget.get(this.shell.widgets)) {
this.updateWidget(uri, widget, event, { dirty, toClose: toClose });
}
if (this.corePreferences['workbench.editor.closeOnFileDelete']) {
const doClose = [];
for (const [uri, widgets] of toClose.entries()) {
if (!dirty.has(uri)) {
doClose.push(...widgets);
}
}
await this.shell.closeMany(doClose);
}
}
protected updateWidget(uri: URI, widget: NavigatableWidget, event: FileChangesEvent, { dirty, toClose }: {
dirty: Set<string>;
toClose: Map<string, NavigatableWidget[]>
}): void {
const label = widget.title.label;
const deleted = label.endsWith(this.deletedSuffix);
if (event.contains(uri, FileChangeType.DELETED)) {
const uriString = uri.toString();
if (Saveable.isDirty(widget)) {
dirty.add(uriString);
}
if (!deleted) {
widget.title.label += this.deletedSuffix;
this.onDidChangeEditorFileEmitter.fire({ editor: widget, type: FileChangeType.DELETED });
}
const widgets = toClose.get(uriString) || [];
widgets.push(widget);
toClose.set(uriString, widgets);
} else if (event.contains(uri, FileChangeType.ADDED)) {
if (deleted) {
widget.title.label = widget.title.label.substring(0, label.length - this.deletedSuffix.length);
this.onDidChangeEditorFileEmitter.fire({ editor: widget, type: FileChangeType.ADDED });
}
}
}
protected updateAssociations(): void {
const fileAssociations = this.preferences['files.associations'];
const mimeAssociations = Object.keys(fileAssociations).map(filepattern => ({ id: fileAssociations[filepattern], filepattern }));
this.mimeService.setAssociations(mimeAssociations);
}
}