@theia/workspace 1.34.1 → 1.34.3

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.
Files changed (105) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +30 -30
  3. package/lib/browser/diff-service.d.ts +10 -10
  4. package/lib/browser/diff-service.js +85 -85
  5. package/lib/browser/index.d.ts +6 -6
  6. package/lib/browser/index.js +33 -33
  7. package/lib/browser/quick-open-workspace.d.ts +25 -25
  8. package/lib/browser/quick-open-workspace.js +137 -137
  9. package/lib/browser/untitled-workspace-exit-dialog.d.ts +19 -19
  10. package/lib/browser/untitled-workspace-exit-dialog.js +74 -74
  11. package/lib/browser/workspace-breadcrumbs-contribution.d.ts +10 -10
  12. package/lib/browser/workspace-breadcrumbs-contribution.js +66 -66
  13. package/lib/browser/workspace-commands.d.ts +117 -117
  14. package/lib/browser/workspace-commands.js +593 -593
  15. package/lib/browser/workspace-commands.spec.d.ts +1 -1
  16. package/lib/browser/workspace-commands.spec.js +127 -127
  17. package/lib/browser/workspace-compare-handler.d.ts +26 -26
  18. package/lib/browser/workspace-compare-handler.js +66 -66
  19. package/lib/browser/workspace-delete-handler.d.ts +75 -75
  20. package/lib/browser/workspace-delete-handler.js +214 -214
  21. package/lib/browser/workspace-duplicate-handler.d.ts +30 -30
  22. package/lib/browser/workspace-duplicate-handler.js +86 -86
  23. package/lib/browser/workspace-frontend-contribution.d.ts +135 -135
  24. package/lib/browser/workspace-frontend-contribution.js +568 -568
  25. package/lib/browser/workspace-frontend-contribution.spec.d.ts +1 -1
  26. package/lib/browser/workspace-frontend-contribution.spec.js +53 -53
  27. package/lib/browser/workspace-frontend-module.d.ts +3 -3
  28. package/lib/browser/workspace-frontend-module.js +88 -88
  29. package/lib/browser/workspace-input-dialog.d.ts +19 -19
  30. package/lib/browser/workspace-input-dialog.js +73 -73
  31. package/lib/browser/workspace-preferences.d.ts +11 -11
  32. package/lib/browser/workspace-preferences.js +46 -46
  33. package/lib/browser/workspace-schema-updater.d.ts +32 -32
  34. package/lib/browser/workspace-schema-updater.js +148 -148
  35. package/lib/browser/workspace-service.d.ts +182 -182
  36. package/lib/browser/workspace-service.js +729 -729
  37. package/lib/browser/workspace-storage-service.d.ts +15 -15
  38. package/lib/browser/workspace-storage-service.js +80 -80
  39. package/lib/browser/workspace-trust-preferences.d.ts +21 -21
  40. package/lib/browser/workspace-trust-preferences.js +65 -65
  41. package/lib/browser/workspace-trust-service.d.ts +26 -26
  42. package/lib/browser/workspace-trust-service.js +149 -149
  43. package/lib/browser/workspace-uri-contribution.d.ts +20 -20
  44. package/lib/browser/workspace-uri-contribution.js +113 -113
  45. package/lib/browser/workspace-uri-contribution.spec.d.ts +1 -1
  46. package/lib/browser/workspace-uri-contribution.spec.js +169 -169
  47. package/lib/browser/workspace-user-working-directory-provider.d.ts +12 -12
  48. package/lib/browser/workspace-user-working-directory-provider.js +62 -62
  49. package/lib/browser/workspace-utils.d.ts +15 -15
  50. package/lib/browser/workspace-utils.js +54 -54
  51. package/lib/browser/workspace-variable-contribution.d.ts +23 -23
  52. package/lib/browser/workspace-variable-contribution.js +237 -237
  53. package/lib/browser/workspace-window-title-updater.d.ts +7 -7
  54. package/lib/browser/workspace-window-title-updater.js +57 -57
  55. package/lib/common/index.d.ts +2 -2
  56. package/lib/common/index.js +29 -29
  57. package/lib/common/test/mock-workspace-server.d.ts +7 -7
  58. package/lib/common/test/mock-workspace-server.js +35 -35
  59. package/lib/common/utils.d.ts +21 -21
  60. package/lib/common/utils.js +73 -73
  61. package/lib/common/workspace-protocol.d.ts +26 -26
  62. package/lib/common/workspace-protocol.js +23 -23
  63. package/lib/node/default-workspace-server.d.ts +64 -64
  64. package/lib/node/default-workspace-server.js +248 -248
  65. package/lib/node/default-workspace-server.spec.d.ts +1 -1
  66. package/lib/node/default-workspace-server.spec.js +84 -84
  67. package/lib/node/index.d.ts +2 -2
  68. package/lib/node/index.js +29 -29
  69. package/lib/node/workspace-backend-module.d.ts +3 -3
  70. package/lib/node/workspace-backend-module.js +32 -32
  71. package/package.json +6 -6
  72. package/src/browser/diff-service.ts +62 -62
  73. package/src/browser/index.ts +22 -22
  74. package/src/browser/quick-open-workspace.ts +112 -112
  75. package/src/browser/untitled-workspace-exit-dialog.ts +70 -70
  76. package/src/browser/workspace-breadcrumbs-contribution.ts +56 -56
  77. package/src/browser/workspace-commands.spec.ts +153 -153
  78. package/src/browser/workspace-commands.ts +586 -586
  79. package/src/browser/workspace-compare-handler.ts +56 -56
  80. package/src/browser/workspace-delete-handler.ts +212 -212
  81. package/src/browser/workspace-duplicate-handler.ts +75 -75
  82. package/src/browser/workspace-frontend-contribution.spec.ts +63 -63
  83. package/src/browser/workspace-frontend-contribution.ts +558 -558
  84. package/src/browser/workspace-frontend-module.ts +115 -115
  85. package/src/browser/workspace-input-dialog.ts +60 -60
  86. package/src/browser/workspace-preferences.ts +58 -58
  87. package/src/browser/workspace-schema-updater.ts +148 -148
  88. package/src/browser/workspace-service.ts +772 -772
  89. package/src/browser/workspace-storage-service.ts +67 -67
  90. package/src/browser/workspace-trust-preferences.ts +76 -76
  91. package/src/browser/workspace-trust-service.ts +143 -143
  92. package/src/browser/workspace-uri-contribution.spec.ts +191 -191
  93. package/src/browser/workspace-uri-contribution.ts +97 -97
  94. package/src/browser/workspace-user-working-directory-provider.ts +48 -48
  95. package/src/browser/workspace-utils.ts +45 -45
  96. package/src/browser/workspace-variable-contribution.ts +222 -222
  97. package/src/browser/workspace-window-title-updater.ts +45 -45
  98. package/src/common/index.ts +18 -18
  99. package/src/common/test/mock-workspace-server.ts +29 -29
  100. package/src/common/utils.ts +69 -69
  101. package/src/common/workspace-protocol.ts +47 -47
  102. package/src/node/default-workspace-server.spec.ts +99 -99
  103. package/src/node/default-workspace-server.ts +242 -242
  104. package/src/node/index.ts +18 -18
  105. package/src/node/workspace-backend-module.ts +37 -37
@@ -1,594 +1,594 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2017 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
- return c > 3 && r && Object.defineProperty(target, key, r), r;
22
- };
23
- var __metadata = (this && this.__metadata) || function (k, v) {
24
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.WorkspaceRootUriAwareCommandHandler = exports.WorkspaceCommandContribution = exports.EditMenuContribution = exports.FileMenuContribution = exports.WorkspaceCommands = void 0;
28
- const inversify_1 = require("@theia/core/shared/inversify");
29
- const selection_service_1 = require("@theia/core/lib/common/selection-service");
30
- const command_1 = require("@theia/core/lib/common/command");
31
- const common_frontend_contribution_1 = require("@theia/core/lib/browser/common-frontend-contribution");
32
- const browser_1 = require("@theia/filesystem/lib/browser");
33
- const dialogs_1 = require("@theia/core/lib/browser/dialogs");
34
- const browser_2 = require("@theia/core/lib/browser");
35
- const uri_command_handler_1 = require("@theia/core/lib/common/uri-command-handler");
36
- const workspace_service_1 = require("./workspace-service");
37
- const message_service_1 = require("@theia/core/lib/common/message-service");
38
- const workspace_preferences_1 = require("./workspace-preferences");
39
- const workspace_delete_handler_1 = require("./workspace-delete-handler");
40
- const workspace_duplicate_handler_1 = require("./workspace-duplicate-handler");
41
- const common_1 = require("@theia/filesystem/lib/common");
42
- const workspace_compare_handler_1 = require("./workspace-compare-handler");
43
- const file_download_command_contribution_1 = require("@theia/filesystem/lib/browser/download/file-download-command-contribution");
44
- const filesystem_frontend_contribution_1 = require("@theia/filesystem/lib/browser/filesystem-frontend-contribution");
45
- const workspace_input_dialog_1 = require("./workspace-input-dialog");
46
- const common_2 = require("@theia/core/lib/common");
47
- const file_service_1 = require("@theia/filesystem/lib/browser/file-service");
48
- const nls_1 = require("@theia/core/lib/common/nls");
49
- const clipboard_service_1 = require("@theia/core/lib/browser/clipboard-service");
50
- const validFilename = require('valid-filename');
51
- var WorkspaceCommands;
52
- (function (WorkspaceCommands) {
53
- const WORKSPACE_CATEGORY = 'Workspaces';
54
- const FILE_CATEGORY = browser_2.CommonCommands.FILE_CATEGORY;
55
- // On Linux and Windows, both files and folders cannot be opened at the same time in electron.
56
- // `OPEN_FILE` and `OPEN_FOLDER` must be available only on Linux and Windows in electron.
57
- // `OPEN` must *not* be available on Windows and Linux in electron.
58
- // VS Code does the same. See: https://github.com/eclipse-theia/theia/pull/3202#issuecomment-430585357
59
- WorkspaceCommands.OPEN = Object.assign(Object.assign({}, command_1.Command.toDefaultLocalizedCommand({
60
- id: 'workspace:open',
61
- category: browser_2.CommonCommands.FILE_CATEGORY,
62
- label: 'Open...'
63
- })), { dialogLabel: nls_1.nls.localizeByDefault('Open') });
64
- // No `label`. Otherwise, it shows up in the `Command Palette`.
65
- WorkspaceCommands.OPEN_FILE = {
66
- id: 'workspace:openFile',
67
- originalCategory: FILE_CATEGORY,
68
- category: nls_1.nls.localizeByDefault(browser_2.CommonCommands.FILE_CATEGORY),
69
- dialogLabel: nls_1.nls.localizeByDefault('Open File')
70
- };
71
- WorkspaceCommands.OPEN_FOLDER = {
72
- id: 'workspace:openFolder',
73
- dialogLabel: nls_1.nls.localizeByDefault('Open Folder') // No `label`. Otherwise, it shows up in the `Command Palette`.
74
- };
75
- WorkspaceCommands.OPEN_WORKSPACE = Object.assign(Object.assign({}, command_1.Command.toDefaultLocalizedCommand({
76
- id: 'workspace:openWorkspace',
77
- category: browser_2.CommonCommands.FILE_CATEGORY,
78
- label: 'Open Workspace...',
79
- })), { dialogLabel: nls_1.nls.localizeByDefault('Open Workspace') });
80
- WorkspaceCommands.OPEN_RECENT_WORKSPACE = command_1.Command.toLocalizedCommand({
81
- id: 'workspace:openRecent',
82
- category: FILE_CATEGORY,
83
- label: 'Open Recent Workspace...'
84
- }, 'theia/workspace/openRecentWorkspace', browser_2.CommonCommands.FILE_CATEGORY_KEY);
85
- WorkspaceCommands.CLOSE = command_1.Command.toDefaultLocalizedCommand({
86
- id: 'workspace:close',
87
- category: WORKSPACE_CATEGORY,
88
- label: 'Close Workspace'
89
- });
90
- WorkspaceCommands.NEW_FILE = command_1.Command.toDefaultLocalizedCommand({
91
- id: 'file.newFile',
92
- category: FILE_CATEGORY,
93
- label: 'New File'
94
- });
95
- WorkspaceCommands.NEW_FOLDER = command_1.Command.toDefaultLocalizedCommand({
96
- id: 'file.newFolder',
97
- category: FILE_CATEGORY,
98
- label: 'New Folder'
99
- });
100
- WorkspaceCommands.FILE_OPEN_WITH = (opener) => ({
101
- id: `file.openWith.${opener.id}`
102
- });
103
- WorkspaceCommands.FILE_RENAME = command_1.Command.toDefaultLocalizedCommand({
104
- id: 'file.rename',
105
- category: FILE_CATEGORY,
106
- label: 'Rename'
107
- });
108
- WorkspaceCommands.FILE_DELETE = command_1.Command.toDefaultLocalizedCommand({
109
- id: 'file.delete',
110
- category: FILE_CATEGORY,
111
- label: 'Delete'
112
- });
113
- WorkspaceCommands.FILE_DUPLICATE = command_1.Command.toLocalizedCommand({
114
- id: 'file.duplicate',
115
- category: FILE_CATEGORY,
116
- label: 'Duplicate'
117
- }, 'theia/workspace/duplicate', browser_2.CommonCommands.FILE_CATEGORY_KEY);
118
- WorkspaceCommands.FILE_COMPARE = command_1.Command.toLocalizedCommand({
119
- id: 'file.compare',
120
- category: FILE_CATEGORY,
121
- label: 'Compare with Each Other'
122
- }, 'theia/workspace/compareWithEachOther', browser_2.CommonCommands.FILE_CATEGORY_KEY);
123
- WorkspaceCommands.ADD_FOLDER = command_1.Command.toDefaultLocalizedCommand({
124
- id: 'workspace:addFolder',
125
- category: WORKSPACE_CATEGORY,
126
- label: 'Add Folder to Workspace...'
127
- });
128
- WorkspaceCommands.REMOVE_FOLDER = command_1.Command.toDefaultLocalizedCommand({
129
- id: 'workspace:removeFolder',
130
- category: WORKSPACE_CATEGORY,
131
- label: 'Remove Folder from Workspace'
132
- });
133
- WorkspaceCommands.SAVE_WORKSPACE_AS = command_1.Command.toDefaultLocalizedCommand({
134
- id: 'workspace:saveAs',
135
- category: WORKSPACE_CATEGORY,
136
- label: 'Save Workspace As...'
137
- });
138
- WorkspaceCommands.OPEN_WORKSPACE_FILE = command_1.Command.toDefaultLocalizedCommand({
139
- id: 'workspace:openConfigFile',
140
- category: WORKSPACE_CATEGORY,
141
- label: 'Open Workspace Configuration File'
142
- });
143
- /** @deprecated @since 1.24.0 Use `CommonCommands.SAVE_AS` instead */
144
- WorkspaceCommands.SAVE_AS = browser_2.CommonCommands.SAVE_AS;
145
- WorkspaceCommands.COPY_RELATIVE_FILE_PATH = command_1.Command.toDefaultLocalizedCommand({
146
- id: 'navigator.copyRelativeFilePath',
147
- label: 'Copy Relative Path'
148
- });
149
- })(WorkspaceCommands = exports.WorkspaceCommands || (exports.WorkspaceCommands = {}));
150
- let FileMenuContribution = class FileMenuContribution {
151
- registerMenus(registry) {
152
- registry.registerMenuAction(common_frontend_contribution_1.CommonMenus.FILE_NEW, {
153
- commandId: WorkspaceCommands.NEW_FOLDER.id,
154
- order: 'b'
155
- });
156
- const downloadUploadMenu = [...common_frontend_contribution_1.CommonMenus.FILE, '4_downloadupload'];
157
- registry.registerMenuAction(downloadUploadMenu, {
158
- commandId: filesystem_frontend_contribution_1.FileSystemCommands.UPLOAD.id,
159
- order: 'a'
160
- });
161
- registry.registerMenuAction(downloadUploadMenu, {
162
- commandId: file_download_command_contribution_1.FileDownloadCommands.DOWNLOAD.id,
163
- order: 'b'
164
- });
165
- }
166
- };
167
- FileMenuContribution = __decorate([
168
- (0, inversify_1.injectable)()
169
- ], FileMenuContribution);
170
- exports.FileMenuContribution = FileMenuContribution;
171
- let EditMenuContribution = class EditMenuContribution {
172
- registerMenus(registry) {
173
- registry.registerMenuAction(common_frontend_contribution_1.CommonMenus.EDIT_CLIPBOARD, {
174
- commandId: file_download_command_contribution_1.FileDownloadCommands.COPY_DOWNLOAD_LINK.id,
175
- order: '9999'
176
- });
177
- }
178
- };
179
- EditMenuContribution = __decorate([
180
- (0, inversify_1.injectable)()
181
- ], EditMenuContribution);
182
- exports.EditMenuContribution = EditMenuContribution;
183
- let WorkspaceCommandContribution = class WorkspaceCommandContribution {
184
- constructor() {
185
- this.onDidCreateNewFileEmitter = new common_2.Emitter();
186
- this.onDidCreateNewFolderEmitter = new common_2.Emitter();
187
- }
188
- get onDidCreateNewFile() {
189
- return this.onDidCreateNewFileEmitter.event;
190
- }
191
- get onDidCreateNewFolder() {
192
- return this.onDidCreateNewFolderEmitter.event;
193
- }
194
- fireCreateNewFile(uri) {
195
- this.onDidCreateNewFileEmitter.fire(uri);
196
- }
197
- fireCreateNewFolder(uri) {
198
- this.onDidCreateNewFolderEmitter.fire(uri);
199
- }
200
- registerCommands(registry) {
201
- this.registerOpenWith(registry);
202
- registry.registerCommand(WorkspaceCommands.NEW_FILE, this.newWorkspaceRootUriAwareCommandHandler({
203
- execute: uri => this.getDirectory(uri).then(parent => {
204
- if (parent) {
205
- const parentUri = parent.resource;
206
- const { fileName, fileExtension } = this.getDefaultFileConfig();
207
- const targetUri = parentUri.resolve(fileName + fileExtension);
208
- const vacantChildUri = common_1.FileSystemUtils.generateUniqueResourceURI(parent, targetUri, false);
209
- const dialog = new workspace_input_dialog_1.WorkspaceInputDialog({
210
- title: nls_1.nls.localizeByDefault('New File'),
211
- parentUri: parentUri,
212
- initialValue: vacantChildUri.path.base,
213
- validate: name => this.validateFileName(name, parent, true)
214
- }, this.labelProvider);
215
- dialog.open().then(async (name) => {
216
- if (name) {
217
- const fileUri = parentUri.resolve(name);
218
- await this.fileService.create(fileUri);
219
- this.fireCreateNewFile({ parent: parentUri, uri: fileUri });
220
- (0, browser_2.open)(this.openerService, fileUri);
221
- }
222
- });
223
- }
224
- })
225
- }));
226
- registry.registerCommand(WorkspaceCommands.NEW_FOLDER, this.newWorkspaceRootUriAwareCommandHandler({
227
- execute: uri => this.getDirectory(uri).then(parent => {
228
- if (parent) {
229
- const parentUri = parent.resource;
230
- const targetUri = parentUri.resolve('Untitled');
231
- const vacantChildUri = common_1.FileSystemUtils.generateUniqueResourceURI(parent, targetUri, true);
232
- const dialog = new workspace_input_dialog_1.WorkspaceInputDialog({
233
- title: nls_1.nls.localizeByDefault('New Folder'),
234
- parentUri: parentUri,
235
- initialValue: vacantChildUri.path.base,
236
- validate: name => this.validateFileName(name, parent, true)
237
- }, this.labelProvider);
238
- dialog.open().then(async (name) => {
239
- if (name) {
240
- const folderUri = parentUri.resolve(name);
241
- await this.fileService.createFolder(folderUri);
242
- this.fireCreateNewFile({ parent: parentUri, uri: folderUri });
243
- }
244
- });
245
- }
246
- })
247
- }));
248
- registry.registerCommand(WorkspaceCommands.FILE_RENAME, this.newMultiUriAwareCommandHandler({
249
- isEnabled: uris => uris.some(uri => !this.isWorkspaceRoot(uri)) && uris.length === 1,
250
- isVisible: uris => uris.some(uri => !this.isWorkspaceRoot(uri)) && uris.length === 1,
251
- execute: (uris) => {
252
- uris.forEach(async (uri) => {
253
- const parent = await this.getParent(uri);
254
- if (parent) {
255
- const oldName = uri.path.base;
256
- const dialog = new dialogs_1.SingleTextInputDialog({
257
- title: nls_1.nls.localizeByDefault('Rename'),
258
- initialValue: oldName,
259
- initialSelectionRange: {
260
- start: 0,
261
- end: uri.path.name.length
262
- },
263
- validate: async (newName, mode) => {
264
- if (oldName === newName && mode === 'preview') {
265
- return false;
266
- }
267
- return this.validateFileRename(oldName, newName, parent);
268
- }
269
- });
270
- const fileName = await dialog.open();
271
- if (fileName) {
272
- const oldUri = uri;
273
- const newUri = uri.parent.resolve(fileName);
274
- this.fileService.move(oldUri, newUri);
275
- }
276
- }
277
- });
278
- }
279
- }));
280
- registry.registerCommand(WorkspaceCommands.FILE_DUPLICATE, this.newMultiUriAwareCommandHandler(this.duplicateHandler));
281
- registry.registerCommand(WorkspaceCommands.FILE_DELETE, this.newMultiUriAwareCommandHandler(this.deleteHandler));
282
- registry.registerCommand(WorkspaceCommands.FILE_COMPARE, this.newMultiUriAwareCommandHandler(this.compareHandler));
283
- registry.registerCommand(WorkspaceCommands.COPY_RELATIVE_FILE_PATH, uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, {
284
- isEnabled: uris => !!uris.length,
285
- isVisible: uris => !!uris.length,
286
- execute: async (uris) => {
287
- const lineDelimiter = common_2.isWindows ? '\r\n' : '\n';
288
- const text = uris.map((uri) => {
289
- var _a;
290
- const workspaceRoot = this.workspaceService.getWorkspaceRootUri(uri);
291
- if (workspaceRoot) {
292
- return (_a = workspaceRoot.relative(uri)) === null || _a === void 0 ? void 0 : _a.fsPath();
293
- }
294
- else {
295
- return uri.path.fsPath();
296
- }
297
- }).join(lineDelimiter);
298
- await this.clipboardService.writeText(text);
299
- }
300
- }));
301
- this.preferences.ready.then(() => {
302
- registry.registerCommand(WorkspaceCommands.ADD_FOLDER, {
303
- isEnabled: () => this.workspaceService.opened,
304
- isVisible: () => this.workspaceService.opened,
305
- execute: async () => {
306
- const selection = await this.fileDialogService.showOpenDialog({
307
- title: WorkspaceCommands.ADD_FOLDER.label,
308
- canSelectFiles: false,
309
- canSelectFolders: true,
310
- canSelectMany: true,
311
- });
312
- if (!selection) {
313
- return;
314
- }
315
- const uris = Array.isArray(selection) ? selection : [selection];
316
- const workspaceSavedBeforeAdding = this.workspaceService.saved;
317
- await this.addFolderToWorkspace(...uris);
318
- if (!workspaceSavedBeforeAdding) {
319
- this.saveWorkspaceWithPrompt(registry);
320
- }
321
- }
322
- });
323
- registry.registerCommand(WorkspaceCommands.REMOVE_FOLDER, this.newMultiUriAwareCommandHandler({
324
- execute: uris => this.removeFolderFromWorkspace(uris),
325
- isEnabled: () => this.workspaceService.isMultiRootWorkspaceOpened,
326
- isVisible: uris => this.areWorkspaceRoots(uris) && this.workspaceService.saved
327
- }));
328
- });
329
- }
330
- async registerOpenWith(registry) {
331
- if (this.openerService.onDidChangeOpeners) {
332
- this.openerService.onDidChangeOpeners(async (e) => {
333
- this.openers = await this.openerService.getOpeners();
334
- });
335
- }
336
- this.openers = await this.openerService.getOpeners();
337
- for (const opener of this.openers) {
338
- const openWithCommand = WorkspaceCommands.FILE_OPEN_WITH(opener);
339
- registry.registerCommand(openWithCommand, this.newUriAwareCommandHandler({
340
- execute: uri => opener.open(uri),
341
- isEnabled: uri => opener.canHandle(uri) > 0,
342
- isVisible: uri => opener.canHandle(uri) > 0 && this.areMultipleOpenHandlersPresent(this.openers, uri)
343
- }));
344
- }
345
- }
346
- newUriAwareCommandHandler(handler) {
347
- return uri_command_handler_1.UriAwareCommandHandler.MonoSelect(this.selectionService, handler);
348
- }
349
- newMultiUriAwareCommandHandler(handler) {
350
- return uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, handler);
351
- }
352
- newWorkspaceRootUriAwareCommandHandler(handler) {
353
- return new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, handler);
354
- }
355
- async validateFileRename(oldName, newName, parent) {
356
- if (common_2.OS.backend.isWindows && parent.resource.resolve(newName).isEqual(parent.resource.resolve(oldName), false)) {
357
- return '';
358
- }
359
- return this.validateFileName(newName, parent, false);
360
- }
361
- /**
362
- * Returns an error message if the file name is invalid. Otherwise, an empty string.
363
- *
364
- * @param name the simple file name of the file to validate.
365
- * @param parent the parent directory's file stat.
366
- * @param allowNested allow file or folder creation using recursive path
367
- */
368
- async validateFileName(name, parent, allowNested = false) {
369
- if (!name) {
370
- return '';
371
- }
372
- // do not allow recursive rename
373
- if (!allowNested && !validFilename(name)) {
374
- return nls_1.nls.localizeByDefault('The name **{0}** is not valid as a file or folder name. Please choose a different name.');
375
- }
376
- if (name.startsWith('/')) {
377
- return nls_1.nls.localizeByDefault('A file or folder name cannot start with a slash.');
378
- }
379
- else if (name.startsWith(' ') || name.endsWith(' ')) {
380
- return nls_1.nls.localizeByDefault('Leading or trailing whitespace detected in file or folder name.');
381
- }
382
- // check and validate each sub-paths
383
- if (name.split(/[\\/]/).some(file => !file || !validFilename(file) || /^\s+$/.test(file))) {
384
- return nls_1.nls.localizeByDefault('\'{0}\' is not a valid file name', this.trimFileName(name));
385
- }
386
- const childUri = parent.resource.resolve(name);
387
- const exists = await this.fileService.exists(childUri);
388
- if (exists) {
389
- return nls_1.nls.localizeByDefault('A file or folder **{0}** already exists at this location. Please choose a different name.', this.trimFileName(name));
390
- }
391
- return '';
392
- }
393
- trimFileName(name) {
394
- if (name && name.length > 30) {
395
- return `${name.substring(0, 30)}...`;
396
- }
397
- return name;
398
- }
399
- async getDirectory(candidate) {
400
- let stat;
401
- try {
402
- stat = await this.fileService.resolve(candidate);
403
- }
404
- catch (_a) { }
405
- if (stat && stat.isDirectory) {
406
- return stat;
407
- }
408
- return this.getParent(candidate);
409
- }
410
- async getParent(candidate) {
411
- try {
412
- return await this.fileService.resolve(candidate.parent);
413
- }
414
- catch (_a) {
415
- return undefined;
416
- }
417
- }
418
- async addFolderToWorkspace(...uris) {
419
- if (uris.length) {
420
- const foldersToAdd = [];
421
- try {
422
- for (const uri of uris) {
423
- const stat = await this.fileService.resolve(uri);
424
- if (stat.isDirectory) {
425
- foldersToAdd.push(uri);
426
- }
427
- }
428
- await this.workspaceService.addRoot(foldersToAdd);
429
- }
430
- catch (_a) { }
431
- }
432
- }
433
- areWorkspaceRoots(uris) {
434
- return this.workspaceService.areWorkspaceRoots(uris);
435
- }
436
- isWorkspaceRoot(uri) {
437
- const rootUris = new Set(this.workspaceService.tryGetRoots().map(root => root.resource.toString()));
438
- return rootUris.has(uri.toString());
439
- }
440
- getDefaultFileConfig() {
441
- return {
442
- fileName: 'Untitled',
443
- fileExtension: '.txt'
444
- };
445
- }
446
- /**
447
- * Removes the list of folders from the workspace upon confirmation from the user.
448
- * @param uris the list of folder uris to remove.
449
- */
450
- async removeFolderFromWorkspace(uris) {
451
- const roots = new Set(this.workspaceService.tryGetRoots().map(root => root.resource.toString()));
452
- const toRemove = uris.filter(uri => roots.has(uri.toString()));
453
- if (toRemove.length > 0) {
454
- const messageContainer = document.createElement('div');
455
- if (toRemove.length > 1) {
456
- messageContainer.textContent = nls_1.nls.localize('theia/workspace/removeFolders', 'Are you sure you want to remove the following folders from the workspace?');
457
- }
458
- else {
459
- messageContainer.textContent = nls_1.nls.localize('theia/workspace/removeFolder', 'Are you sure you want to remove the following folder from the workspace?');
460
- }
461
- messageContainer.title = nls_1.nls.localize('theia/workspace/noErasure', 'Note: Nothing will be erased from disk');
462
- const list = document.createElement('div');
463
- list.classList.add('theia-dialog-node');
464
- toRemove.forEach(uri => {
465
- const listItem = document.createElement('div');
466
- listItem.classList.add('theia-dialog-node-content');
467
- const folderIcon = document.createElement('span');
468
- folderIcon.classList.add('codicon', 'codicon-root-folder', 'theia-dialog-icon');
469
- listItem.appendChild(folderIcon);
470
- listItem.title = this.labelProvider.getLongName(uri);
471
- const listContent = document.createElement('span');
472
- listContent.classList.add('theia-dialog-node-segment');
473
- listContent.appendChild(document.createTextNode(this.labelProvider.getName(uri)));
474
- listItem.appendChild(listContent);
475
- list.appendChild(listItem);
476
- });
477
- messageContainer.appendChild(list);
478
- const dialog = new dialogs_1.ConfirmDialog({
479
- title: nls_1.nls.localizeByDefault('Remove Folder from Workspace'),
480
- msg: messageContainer
481
- });
482
- if (await dialog.open()) {
483
- await this.workspaceService.removeRoots(toRemove);
484
- }
485
- }
486
- }
487
- async saveWorkspaceWithPrompt(registry) {
488
- const saveCommand = registry.getCommand(WorkspaceCommands.SAVE_WORKSPACE_AS.id);
489
- if (saveCommand && await new dialogs_1.ConfirmDialog({
490
- title: nls_1.nls.localize('theia/workspace/workspaceFolderAddedTitle', 'Folder added to Workspace'),
491
- msg: nls_1.nls.localize('theia/workspace/workspaceFolderAdded', 'A workspace with multiple roots was created. Do you want to save your workspace configuration as a file?'),
492
- ok: dialogs_1.Dialog.YES,
493
- cancel: dialogs_1.Dialog.NO
494
- }).open()) {
495
- return registry.executeCommand(saveCommand.id);
496
- }
497
- }
498
- areMultipleOpenHandlersPresent(openers, uri) {
499
- let count = 0;
500
- for (const opener of openers) {
501
- if (opener.canHandle(uri) > 0) {
502
- count++;
503
- }
504
- if (count > 1) {
505
- return true;
506
- }
507
- }
508
- return false;
509
- }
510
- };
511
- __decorate([
512
- (0, inversify_1.inject)(browser_2.LabelProvider),
513
- __metadata("design:type", browser_2.LabelProvider)
514
- ], WorkspaceCommandContribution.prototype, "labelProvider", void 0);
515
- __decorate([
516
- (0, inversify_1.inject)(file_service_1.FileService),
517
- __metadata("design:type", file_service_1.FileService)
518
- ], WorkspaceCommandContribution.prototype, "fileService", void 0);
519
- __decorate([
520
- (0, inversify_1.inject)(workspace_service_1.WorkspaceService),
521
- __metadata("design:type", workspace_service_1.WorkspaceService)
522
- ], WorkspaceCommandContribution.prototype, "workspaceService", void 0);
523
- __decorate([
524
- (0, inversify_1.inject)(selection_service_1.SelectionService),
525
- __metadata("design:type", selection_service_1.SelectionService)
526
- ], WorkspaceCommandContribution.prototype, "selectionService", void 0);
527
- __decorate([
528
- (0, inversify_1.inject)(browser_2.OpenerService),
529
- __metadata("design:type", Object)
530
- ], WorkspaceCommandContribution.prototype, "openerService", void 0);
531
- __decorate([
532
- (0, inversify_1.inject)(browser_2.FrontendApplication),
533
- __metadata("design:type", browser_2.FrontendApplication)
534
- ], WorkspaceCommandContribution.prototype, "app", void 0);
535
- __decorate([
536
- (0, inversify_1.inject)(message_service_1.MessageService),
537
- __metadata("design:type", message_service_1.MessageService)
538
- ], WorkspaceCommandContribution.prototype, "messageService", void 0);
539
- __decorate([
540
- (0, inversify_1.inject)(workspace_preferences_1.WorkspacePreferences),
541
- __metadata("design:type", Object)
542
- ], WorkspaceCommandContribution.prototype, "preferences", void 0);
543
- __decorate([
544
- (0, inversify_1.inject)(browser_1.FileDialogService),
545
- __metadata("design:type", Object)
546
- ], WorkspaceCommandContribution.prototype, "fileDialogService", void 0);
547
- __decorate([
548
- (0, inversify_1.inject)(workspace_delete_handler_1.WorkspaceDeleteHandler),
549
- __metadata("design:type", workspace_delete_handler_1.WorkspaceDeleteHandler)
550
- ], WorkspaceCommandContribution.prototype, "deleteHandler", void 0);
551
- __decorate([
552
- (0, inversify_1.inject)(workspace_duplicate_handler_1.WorkspaceDuplicateHandler),
553
- __metadata("design:type", workspace_duplicate_handler_1.WorkspaceDuplicateHandler)
554
- ], WorkspaceCommandContribution.prototype, "duplicateHandler", void 0);
555
- __decorate([
556
- (0, inversify_1.inject)(workspace_compare_handler_1.WorkspaceCompareHandler),
557
- __metadata("design:type", workspace_compare_handler_1.WorkspaceCompareHandler)
558
- ], WorkspaceCommandContribution.prototype, "compareHandler", void 0);
559
- __decorate([
560
- (0, inversify_1.inject)(clipboard_service_1.ClipboardService),
561
- __metadata("design:type", Object)
562
- ], WorkspaceCommandContribution.prototype, "clipboardService", void 0);
563
- WorkspaceCommandContribution = __decorate([
564
- (0, inversify_1.injectable)()
565
- ], WorkspaceCommandContribution);
566
- exports.WorkspaceCommandContribution = WorkspaceCommandContribution;
567
- class WorkspaceRootUriAwareCommandHandler extends uri_command_handler_1.UriAwareCommandHandler {
568
- constructor(workspaceService, selectionService, handler) {
569
- super(selectionService, handler);
570
- this.workspaceService = workspaceService;
571
- }
572
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
573
- isEnabled(...args) {
574
- return super.isEnabled(...args) && !!this.workspaceService.tryGetRoots().length;
575
- }
576
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
577
- isVisible(...args) {
578
- return super.isVisible(...args) && !!this.workspaceService.tryGetRoots().length;
579
- }
580
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
581
- getUri(...args) {
582
- const uri = super.getUri(...args);
583
- // Return the `uri` immediately if the resource exists in any of the workspace roots and is of `file` scheme.
584
- if (uri && uri.scheme === 'file' && this.workspaceService.getWorkspaceRootUri(uri)) {
585
- return uri;
586
- }
587
- // Return the first root if available.
588
- if (!!this.workspaceService.tryGetRoots().length) {
589
- return this.workspaceService.tryGetRoots()[0].resource;
590
- }
591
- }
592
- }
593
- exports.WorkspaceRootUriAwareCommandHandler = WorkspaceRootUriAwareCommandHandler;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2017 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.WorkspaceRootUriAwareCommandHandler = exports.WorkspaceCommandContribution = exports.EditMenuContribution = exports.FileMenuContribution = exports.WorkspaceCommands = void 0;
28
+ const inversify_1 = require("@theia/core/shared/inversify");
29
+ const selection_service_1 = require("@theia/core/lib/common/selection-service");
30
+ const command_1 = require("@theia/core/lib/common/command");
31
+ const common_frontend_contribution_1 = require("@theia/core/lib/browser/common-frontend-contribution");
32
+ const browser_1 = require("@theia/filesystem/lib/browser");
33
+ const dialogs_1 = require("@theia/core/lib/browser/dialogs");
34
+ const browser_2 = require("@theia/core/lib/browser");
35
+ const uri_command_handler_1 = require("@theia/core/lib/common/uri-command-handler");
36
+ const workspace_service_1 = require("./workspace-service");
37
+ const message_service_1 = require("@theia/core/lib/common/message-service");
38
+ const workspace_preferences_1 = require("./workspace-preferences");
39
+ const workspace_delete_handler_1 = require("./workspace-delete-handler");
40
+ const workspace_duplicate_handler_1 = require("./workspace-duplicate-handler");
41
+ const common_1 = require("@theia/filesystem/lib/common");
42
+ const workspace_compare_handler_1 = require("./workspace-compare-handler");
43
+ const file_download_command_contribution_1 = require("@theia/filesystem/lib/browser/download/file-download-command-contribution");
44
+ const filesystem_frontend_contribution_1 = require("@theia/filesystem/lib/browser/filesystem-frontend-contribution");
45
+ const workspace_input_dialog_1 = require("./workspace-input-dialog");
46
+ const common_2 = require("@theia/core/lib/common");
47
+ const file_service_1 = require("@theia/filesystem/lib/browser/file-service");
48
+ const nls_1 = require("@theia/core/lib/common/nls");
49
+ const clipboard_service_1 = require("@theia/core/lib/browser/clipboard-service");
50
+ const validFilename = require('valid-filename');
51
+ var WorkspaceCommands;
52
+ (function (WorkspaceCommands) {
53
+ const WORKSPACE_CATEGORY = 'Workspaces';
54
+ const FILE_CATEGORY = browser_2.CommonCommands.FILE_CATEGORY;
55
+ // On Linux and Windows, both files and folders cannot be opened at the same time in electron.
56
+ // `OPEN_FILE` and `OPEN_FOLDER` must be available only on Linux and Windows in electron.
57
+ // `OPEN` must *not* be available on Windows and Linux in electron.
58
+ // VS Code does the same. See: https://github.com/eclipse-theia/theia/pull/3202#issuecomment-430585357
59
+ WorkspaceCommands.OPEN = Object.assign(Object.assign({}, command_1.Command.toDefaultLocalizedCommand({
60
+ id: 'workspace:open',
61
+ category: browser_2.CommonCommands.FILE_CATEGORY,
62
+ label: 'Open...'
63
+ })), { dialogLabel: nls_1.nls.localizeByDefault('Open') });
64
+ // No `label`. Otherwise, it shows up in the `Command Palette`.
65
+ WorkspaceCommands.OPEN_FILE = {
66
+ id: 'workspace:openFile',
67
+ originalCategory: FILE_CATEGORY,
68
+ category: nls_1.nls.localizeByDefault(browser_2.CommonCommands.FILE_CATEGORY),
69
+ dialogLabel: nls_1.nls.localizeByDefault('Open File')
70
+ };
71
+ WorkspaceCommands.OPEN_FOLDER = {
72
+ id: 'workspace:openFolder',
73
+ dialogLabel: nls_1.nls.localizeByDefault('Open Folder') // No `label`. Otherwise, it shows up in the `Command Palette`.
74
+ };
75
+ WorkspaceCommands.OPEN_WORKSPACE = Object.assign(Object.assign({}, command_1.Command.toDefaultLocalizedCommand({
76
+ id: 'workspace:openWorkspace',
77
+ category: browser_2.CommonCommands.FILE_CATEGORY,
78
+ label: 'Open Workspace...',
79
+ })), { dialogLabel: nls_1.nls.localizeByDefault('Open Workspace') });
80
+ WorkspaceCommands.OPEN_RECENT_WORKSPACE = command_1.Command.toLocalizedCommand({
81
+ id: 'workspace:openRecent',
82
+ category: FILE_CATEGORY,
83
+ label: 'Open Recent Workspace...'
84
+ }, 'theia/workspace/openRecentWorkspace', browser_2.CommonCommands.FILE_CATEGORY_KEY);
85
+ WorkspaceCommands.CLOSE = command_1.Command.toDefaultLocalizedCommand({
86
+ id: 'workspace:close',
87
+ category: WORKSPACE_CATEGORY,
88
+ label: 'Close Workspace'
89
+ });
90
+ WorkspaceCommands.NEW_FILE = command_1.Command.toDefaultLocalizedCommand({
91
+ id: 'file.newFile',
92
+ category: FILE_CATEGORY,
93
+ label: 'New File'
94
+ });
95
+ WorkspaceCommands.NEW_FOLDER = command_1.Command.toDefaultLocalizedCommand({
96
+ id: 'file.newFolder',
97
+ category: FILE_CATEGORY,
98
+ label: 'New Folder'
99
+ });
100
+ WorkspaceCommands.FILE_OPEN_WITH = (opener) => ({
101
+ id: `file.openWith.${opener.id}`
102
+ });
103
+ WorkspaceCommands.FILE_RENAME = command_1.Command.toDefaultLocalizedCommand({
104
+ id: 'file.rename',
105
+ category: FILE_CATEGORY,
106
+ label: 'Rename'
107
+ });
108
+ WorkspaceCommands.FILE_DELETE = command_1.Command.toDefaultLocalizedCommand({
109
+ id: 'file.delete',
110
+ category: FILE_CATEGORY,
111
+ label: 'Delete'
112
+ });
113
+ WorkspaceCommands.FILE_DUPLICATE = command_1.Command.toLocalizedCommand({
114
+ id: 'file.duplicate',
115
+ category: FILE_CATEGORY,
116
+ label: 'Duplicate'
117
+ }, 'theia/workspace/duplicate', browser_2.CommonCommands.FILE_CATEGORY_KEY);
118
+ WorkspaceCommands.FILE_COMPARE = command_1.Command.toLocalizedCommand({
119
+ id: 'file.compare',
120
+ category: FILE_CATEGORY,
121
+ label: 'Compare with Each Other'
122
+ }, 'theia/workspace/compareWithEachOther', browser_2.CommonCommands.FILE_CATEGORY_KEY);
123
+ WorkspaceCommands.ADD_FOLDER = command_1.Command.toDefaultLocalizedCommand({
124
+ id: 'workspace:addFolder',
125
+ category: WORKSPACE_CATEGORY,
126
+ label: 'Add Folder to Workspace...'
127
+ });
128
+ WorkspaceCommands.REMOVE_FOLDER = command_1.Command.toDefaultLocalizedCommand({
129
+ id: 'workspace:removeFolder',
130
+ category: WORKSPACE_CATEGORY,
131
+ label: 'Remove Folder from Workspace'
132
+ });
133
+ WorkspaceCommands.SAVE_WORKSPACE_AS = command_1.Command.toDefaultLocalizedCommand({
134
+ id: 'workspace:saveAs',
135
+ category: WORKSPACE_CATEGORY,
136
+ label: 'Save Workspace As...'
137
+ });
138
+ WorkspaceCommands.OPEN_WORKSPACE_FILE = command_1.Command.toDefaultLocalizedCommand({
139
+ id: 'workspace:openConfigFile',
140
+ category: WORKSPACE_CATEGORY,
141
+ label: 'Open Workspace Configuration File'
142
+ });
143
+ /** @deprecated @since 1.24.0 Use `CommonCommands.SAVE_AS` instead */
144
+ WorkspaceCommands.SAVE_AS = browser_2.CommonCommands.SAVE_AS;
145
+ WorkspaceCommands.COPY_RELATIVE_FILE_PATH = command_1.Command.toDefaultLocalizedCommand({
146
+ id: 'navigator.copyRelativeFilePath',
147
+ label: 'Copy Relative Path'
148
+ });
149
+ })(WorkspaceCommands = exports.WorkspaceCommands || (exports.WorkspaceCommands = {}));
150
+ let FileMenuContribution = class FileMenuContribution {
151
+ registerMenus(registry) {
152
+ registry.registerMenuAction(common_frontend_contribution_1.CommonMenus.FILE_NEW, {
153
+ commandId: WorkspaceCommands.NEW_FOLDER.id,
154
+ order: 'b'
155
+ });
156
+ const downloadUploadMenu = [...common_frontend_contribution_1.CommonMenus.FILE, '4_downloadupload'];
157
+ registry.registerMenuAction(downloadUploadMenu, {
158
+ commandId: filesystem_frontend_contribution_1.FileSystemCommands.UPLOAD.id,
159
+ order: 'a'
160
+ });
161
+ registry.registerMenuAction(downloadUploadMenu, {
162
+ commandId: file_download_command_contribution_1.FileDownloadCommands.DOWNLOAD.id,
163
+ order: 'b'
164
+ });
165
+ }
166
+ };
167
+ FileMenuContribution = __decorate([
168
+ (0, inversify_1.injectable)()
169
+ ], FileMenuContribution);
170
+ exports.FileMenuContribution = FileMenuContribution;
171
+ let EditMenuContribution = class EditMenuContribution {
172
+ registerMenus(registry) {
173
+ registry.registerMenuAction(common_frontend_contribution_1.CommonMenus.EDIT_CLIPBOARD, {
174
+ commandId: file_download_command_contribution_1.FileDownloadCommands.COPY_DOWNLOAD_LINK.id,
175
+ order: '9999'
176
+ });
177
+ }
178
+ };
179
+ EditMenuContribution = __decorate([
180
+ (0, inversify_1.injectable)()
181
+ ], EditMenuContribution);
182
+ exports.EditMenuContribution = EditMenuContribution;
183
+ let WorkspaceCommandContribution = class WorkspaceCommandContribution {
184
+ constructor() {
185
+ this.onDidCreateNewFileEmitter = new common_2.Emitter();
186
+ this.onDidCreateNewFolderEmitter = new common_2.Emitter();
187
+ }
188
+ get onDidCreateNewFile() {
189
+ return this.onDidCreateNewFileEmitter.event;
190
+ }
191
+ get onDidCreateNewFolder() {
192
+ return this.onDidCreateNewFolderEmitter.event;
193
+ }
194
+ fireCreateNewFile(uri) {
195
+ this.onDidCreateNewFileEmitter.fire(uri);
196
+ }
197
+ fireCreateNewFolder(uri) {
198
+ this.onDidCreateNewFolderEmitter.fire(uri);
199
+ }
200
+ registerCommands(registry) {
201
+ this.registerOpenWith(registry);
202
+ registry.registerCommand(WorkspaceCommands.NEW_FILE, this.newWorkspaceRootUriAwareCommandHandler({
203
+ execute: uri => this.getDirectory(uri).then(parent => {
204
+ if (parent) {
205
+ const parentUri = parent.resource;
206
+ const { fileName, fileExtension } = this.getDefaultFileConfig();
207
+ const targetUri = parentUri.resolve(fileName + fileExtension);
208
+ const vacantChildUri = common_1.FileSystemUtils.generateUniqueResourceURI(parent, targetUri, false);
209
+ const dialog = new workspace_input_dialog_1.WorkspaceInputDialog({
210
+ title: nls_1.nls.localizeByDefault('New File'),
211
+ parentUri: parentUri,
212
+ initialValue: vacantChildUri.path.base,
213
+ validate: name => this.validateFileName(name, parent, true)
214
+ }, this.labelProvider);
215
+ dialog.open().then(async (name) => {
216
+ if (name) {
217
+ const fileUri = parentUri.resolve(name);
218
+ await this.fileService.create(fileUri);
219
+ this.fireCreateNewFile({ parent: parentUri, uri: fileUri });
220
+ (0, browser_2.open)(this.openerService, fileUri);
221
+ }
222
+ });
223
+ }
224
+ })
225
+ }));
226
+ registry.registerCommand(WorkspaceCommands.NEW_FOLDER, this.newWorkspaceRootUriAwareCommandHandler({
227
+ execute: uri => this.getDirectory(uri).then(parent => {
228
+ if (parent) {
229
+ const parentUri = parent.resource;
230
+ const targetUri = parentUri.resolve('Untitled');
231
+ const vacantChildUri = common_1.FileSystemUtils.generateUniqueResourceURI(parent, targetUri, true);
232
+ const dialog = new workspace_input_dialog_1.WorkspaceInputDialog({
233
+ title: nls_1.nls.localizeByDefault('New Folder'),
234
+ parentUri: parentUri,
235
+ initialValue: vacantChildUri.path.base,
236
+ validate: name => this.validateFileName(name, parent, true)
237
+ }, this.labelProvider);
238
+ dialog.open().then(async (name) => {
239
+ if (name) {
240
+ const folderUri = parentUri.resolve(name);
241
+ await this.fileService.createFolder(folderUri);
242
+ this.fireCreateNewFile({ parent: parentUri, uri: folderUri });
243
+ }
244
+ });
245
+ }
246
+ })
247
+ }));
248
+ registry.registerCommand(WorkspaceCommands.FILE_RENAME, this.newMultiUriAwareCommandHandler({
249
+ isEnabled: uris => uris.some(uri => !this.isWorkspaceRoot(uri)) && uris.length === 1,
250
+ isVisible: uris => uris.some(uri => !this.isWorkspaceRoot(uri)) && uris.length === 1,
251
+ execute: (uris) => {
252
+ uris.forEach(async (uri) => {
253
+ const parent = await this.getParent(uri);
254
+ if (parent) {
255
+ const oldName = uri.path.base;
256
+ const dialog = new dialogs_1.SingleTextInputDialog({
257
+ title: nls_1.nls.localizeByDefault('Rename'),
258
+ initialValue: oldName,
259
+ initialSelectionRange: {
260
+ start: 0,
261
+ end: uri.path.name.length
262
+ },
263
+ validate: async (newName, mode) => {
264
+ if (oldName === newName && mode === 'preview') {
265
+ return false;
266
+ }
267
+ return this.validateFileRename(oldName, newName, parent);
268
+ }
269
+ });
270
+ const fileName = await dialog.open();
271
+ if (fileName) {
272
+ const oldUri = uri;
273
+ const newUri = uri.parent.resolve(fileName);
274
+ this.fileService.move(oldUri, newUri);
275
+ }
276
+ }
277
+ });
278
+ }
279
+ }));
280
+ registry.registerCommand(WorkspaceCommands.FILE_DUPLICATE, this.newMultiUriAwareCommandHandler(this.duplicateHandler));
281
+ registry.registerCommand(WorkspaceCommands.FILE_DELETE, this.newMultiUriAwareCommandHandler(this.deleteHandler));
282
+ registry.registerCommand(WorkspaceCommands.FILE_COMPARE, this.newMultiUriAwareCommandHandler(this.compareHandler));
283
+ registry.registerCommand(WorkspaceCommands.COPY_RELATIVE_FILE_PATH, uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, {
284
+ isEnabled: uris => !!uris.length,
285
+ isVisible: uris => !!uris.length,
286
+ execute: async (uris) => {
287
+ const lineDelimiter = common_2.isWindows ? '\r\n' : '\n';
288
+ const text = uris.map((uri) => {
289
+ var _a;
290
+ const workspaceRoot = this.workspaceService.getWorkspaceRootUri(uri);
291
+ if (workspaceRoot) {
292
+ return (_a = workspaceRoot.relative(uri)) === null || _a === void 0 ? void 0 : _a.fsPath();
293
+ }
294
+ else {
295
+ return uri.path.fsPath();
296
+ }
297
+ }).join(lineDelimiter);
298
+ await this.clipboardService.writeText(text);
299
+ }
300
+ }));
301
+ this.preferences.ready.then(() => {
302
+ registry.registerCommand(WorkspaceCommands.ADD_FOLDER, {
303
+ isEnabled: () => this.workspaceService.opened,
304
+ isVisible: () => this.workspaceService.opened,
305
+ execute: async () => {
306
+ const selection = await this.fileDialogService.showOpenDialog({
307
+ title: WorkspaceCommands.ADD_FOLDER.label,
308
+ canSelectFiles: false,
309
+ canSelectFolders: true,
310
+ canSelectMany: true,
311
+ });
312
+ if (!selection) {
313
+ return;
314
+ }
315
+ const uris = Array.isArray(selection) ? selection : [selection];
316
+ const workspaceSavedBeforeAdding = this.workspaceService.saved;
317
+ await this.addFolderToWorkspace(...uris);
318
+ if (!workspaceSavedBeforeAdding) {
319
+ this.saveWorkspaceWithPrompt(registry);
320
+ }
321
+ }
322
+ });
323
+ registry.registerCommand(WorkspaceCommands.REMOVE_FOLDER, this.newMultiUriAwareCommandHandler({
324
+ execute: uris => this.removeFolderFromWorkspace(uris),
325
+ isEnabled: () => this.workspaceService.isMultiRootWorkspaceOpened,
326
+ isVisible: uris => this.areWorkspaceRoots(uris) && this.workspaceService.saved
327
+ }));
328
+ });
329
+ }
330
+ async registerOpenWith(registry) {
331
+ if (this.openerService.onDidChangeOpeners) {
332
+ this.openerService.onDidChangeOpeners(async (e) => {
333
+ this.openers = await this.openerService.getOpeners();
334
+ });
335
+ }
336
+ this.openers = await this.openerService.getOpeners();
337
+ for (const opener of this.openers) {
338
+ const openWithCommand = WorkspaceCommands.FILE_OPEN_WITH(opener);
339
+ registry.registerCommand(openWithCommand, this.newUriAwareCommandHandler({
340
+ execute: uri => opener.open(uri),
341
+ isEnabled: uri => opener.canHandle(uri) > 0,
342
+ isVisible: uri => opener.canHandle(uri) > 0 && this.areMultipleOpenHandlersPresent(this.openers, uri)
343
+ }));
344
+ }
345
+ }
346
+ newUriAwareCommandHandler(handler) {
347
+ return uri_command_handler_1.UriAwareCommandHandler.MonoSelect(this.selectionService, handler);
348
+ }
349
+ newMultiUriAwareCommandHandler(handler) {
350
+ return uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, handler);
351
+ }
352
+ newWorkspaceRootUriAwareCommandHandler(handler) {
353
+ return new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, handler);
354
+ }
355
+ async validateFileRename(oldName, newName, parent) {
356
+ if (common_2.OS.backend.isWindows && parent.resource.resolve(newName).isEqual(parent.resource.resolve(oldName), false)) {
357
+ return '';
358
+ }
359
+ return this.validateFileName(newName, parent, false);
360
+ }
361
+ /**
362
+ * Returns an error message if the file name is invalid. Otherwise, an empty string.
363
+ *
364
+ * @param name the simple file name of the file to validate.
365
+ * @param parent the parent directory's file stat.
366
+ * @param allowNested allow file or folder creation using recursive path
367
+ */
368
+ async validateFileName(name, parent, allowNested = false) {
369
+ if (!name) {
370
+ return '';
371
+ }
372
+ // do not allow recursive rename
373
+ if (!allowNested && !validFilename(name)) {
374
+ return nls_1.nls.localizeByDefault('The name **{0}** is not valid as a file or folder name. Please choose a different name.');
375
+ }
376
+ if (name.startsWith('/')) {
377
+ return nls_1.nls.localizeByDefault('A file or folder name cannot start with a slash.');
378
+ }
379
+ else if (name.startsWith(' ') || name.endsWith(' ')) {
380
+ return nls_1.nls.localizeByDefault('Leading or trailing whitespace detected in file or folder name.');
381
+ }
382
+ // check and validate each sub-paths
383
+ if (name.split(/[\\/]/).some(file => !file || !validFilename(file) || /^\s+$/.test(file))) {
384
+ return nls_1.nls.localizeByDefault('\'{0}\' is not a valid file name', this.trimFileName(name));
385
+ }
386
+ const childUri = parent.resource.resolve(name);
387
+ const exists = await this.fileService.exists(childUri);
388
+ if (exists) {
389
+ return nls_1.nls.localizeByDefault('A file or folder **{0}** already exists at this location. Please choose a different name.', this.trimFileName(name));
390
+ }
391
+ return '';
392
+ }
393
+ trimFileName(name) {
394
+ if (name && name.length > 30) {
395
+ return `${name.substring(0, 30)}...`;
396
+ }
397
+ return name;
398
+ }
399
+ async getDirectory(candidate) {
400
+ let stat;
401
+ try {
402
+ stat = await this.fileService.resolve(candidate);
403
+ }
404
+ catch (_a) { }
405
+ if (stat && stat.isDirectory) {
406
+ return stat;
407
+ }
408
+ return this.getParent(candidate);
409
+ }
410
+ async getParent(candidate) {
411
+ try {
412
+ return await this.fileService.resolve(candidate.parent);
413
+ }
414
+ catch (_a) {
415
+ return undefined;
416
+ }
417
+ }
418
+ async addFolderToWorkspace(...uris) {
419
+ if (uris.length) {
420
+ const foldersToAdd = [];
421
+ try {
422
+ for (const uri of uris) {
423
+ const stat = await this.fileService.resolve(uri);
424
+ if (stat.isDirectory) {
425
+ foldersToAdd.push(uri);
426
+ }
427
+ }
428
+ await this.workspaceService.addRoot(foldersToAdd);
429
+ }
430
+ catch (_a) { }
431
+ }
432
+ }
433
+ areWorkspaceRoots(uris) {
434
+ return this.workspaceService.areWorkspaceRoots(uris);
435
+ }
436
+ isWorkspaceRoot(uri) {
437
+ const rootUris = new Set(this.workspaceService.tryGetRoots().map(root => root.resource.toString()));
438
+ return rootUris.has(uri.toString());
439
+ }
440
+ getDefaultFileConfig() {
441
+ return {
442
+ fileName: 'Untitled',
443
+ fileExtension: '.txt'
444
+ };
445
+ }
446
+ /**
447
+ * Removes the list of folders from the workspace upon confirmation from the user.
448
+ * @param uris the list of folder uris to remove.
449
+ */
450
+ async removeFolderFromWorkspace(uris) {
451
+ const roots = new Set(this.workspaceService.tryGetRoots().map(root => root.resource.toString()));
452
+ const toRemove = uris.filter(uri => roots.has(uri.toString()));
453
+ if (toRemove.length > 0) {
454
+ const messageContainer = document.createElement('div');
455
+ if (toRemove.length > 1) {
456
+ messageContainer.textContent = nls_1.nls.localize('theia/workspace/removeFolders', 'Are you sure you want to remove the following folders from the workspace?');
457
+ }
458
+ else {
459
+ messageContainer.textContent = nls_1.nls.localize('theia/workspace/removeFolder', 'Are you sure you want to remove the following folder from the workspace?');
460
+ }
461
+ messageContainer.title = nls_1.nls.localize('theia/workspace/noErasure', 'Note: Nothing will be erased from disk');
462
+ const list = document.createElement('div');
463
+ list.classList.add('theia-dialog-node');
464
+ toRemove.forEach(uri => {
465
+ const listItem = document.createElement('div');
466
+ listItem.classList.add('theia-dialog-node-content');
467
+ const folderIcon = document.createElement('span');
468
+ folderIcon.classList.add('codicon', 'codicon-root-folder', 'theia-dialog-icon');
469
+ listItem.appendChild(folderIcon);
470
+ listItem.title = this.labelProvider.getLongName(uri);
471
+ const listContent = document.createElement('span');
472
+ listContent.classList.add('theia-dialog-node-segment');
473
+ listContent.appendChild(document.createTextNode(this.labelProvider.getName(uri)));
474
+ listItem.appendChild(listContent);
475
+ list.appendChild(listItem);
476
+ });
477
+ messageContainer.appendChild(list);
478
+ const dialog = new dialogs_1.ConfirmDialog({
479
+ title: nls_1.nls.localizeByDefault('Remove Folder from Workspace'),
480
+ msg: messageContainer
481
+ });
482
+ if (await dialog.open()) {
483
+ await this.workspaceService.removeRoots(toRemove);
484
+ }
485
+ }
486
+ }
487
+ async saveWorkspaceWithPrompt(registry) {
488
+ const saveCommand = registry.getCommand(WorkspaceCommands.SAVE_WORKSPACE_AS.id);
489
+ if (saveCommand && await new dialogs_1.ConfirmDialog({
490
+ title: nls_1.nls.localize('theia/workspace/workspaceFolderAddedTitle', 'Folder added to Workspace'),
491
+ msg: nls_1.nls.localize('theia/workspace/workspaceFolderAdded', 'A workspace with multiple roots was created. Do you want to save your workspace configuration as a file?'),
492
+ ok: dialogs_1.Dialog.YES,
493
+ cancel: dialogs_1.Dialog.NO
494
+ }).open()) {
495
+ return registry.executeCommand(saveCommand.id);
496
+ }
497
+ }
498
+ areMultipleOpenHandlersPresent(openers, uri) {
499
+ let count = 0;
500
+ for (const opener of openers) {
501
+ if (opener.canHandle(uri) > 0) {
502
+ count++;
503
+ }
504
+ if (count > 1) {
505
+ return true;
506
+ }
507
+ }
508
+ return false;
509
+ }
510
+ };
511
+ __decorate([
512
+ (0, inversify_1.inject)(browser_2.LabelProvider),
513
+ __metadata("design:type", browser_2.LabelProvider)
514
+ ], WorkspaceCommandContribution.prototype, "labelProvider", void 0);
515
+ __decorate([
516
+ (0, inversify_1.inject)(file_service_1.FileService),
517
+ __metadata("design:type", file_service_1.FileService)
518
+ ], WorkspaceCommandContribution.prototype, "fileService", void 0);
519
+ __decorate([
520
+ (0, inversify_1.inject)(workspace_service_1.WorkspaceService),
521
+ __metadata("design:type", workspace_service_1.WorkspaceService)
522
+ ], WorkspaceCommandContribution.prototype, "workspaceService", void 0);
523
+ __decorate([
524
+ (0, inversify_1.inject)(selection_service_1.SelectionService),
525
+ __metadata("design:type", selection_service_1.SelectionService)
526
+ ], WorkspaceCommandContribution.prototype, "selectionService", void 0);
527
+ __decorate([
528
+ (0, inversify_1.inject)(browser_2.OpenerService),
529
+ __metadata("design:type", Object)
530
+ ], WorkspaceCommandContribution.prototype, "openerService", void 0);
531
+ __decorate([
532
+ (0, inversify_1.inject)(browser_2.FrontendApplication),
533
+ __metadata("design:type", browser_2.FrontendApplication)
534
+ ], WorkspaceCommandContribution.prototype, "app", void 0);
535
+ __decorate([
536
+ (0, inversify_1.inject)(message_service_1.MessageService),
537
+ __metadata("design:type", message_service_1.MessageService)
538
+ ], WorkspaceCommandContribution.prototype, "messageService", void 0);
539
+ __decorate([
540
+ (0, inversify_1.inject)(workspace_preferences_1.WorkspacePreferences),
541
+ __metadata("design:type", Object)
542
+ ], WorkspaceCommandContribution.prototype, "preferences", void 0);
543
+ __decorate([
544
+ (0, inversify_1.inject)(browser_1.FileDialogService),
545
+ __metadata("design:type", Object)
546
+ ], WorkspaceCommandContribution.prototype, "fileDialogService", void 0);
547
+ __decorate([
548
+ (0, inversify_1.inject)(workspace_delete_handler_1.WorkspaceDeleteHandler),
549
+ __metadata("design:type", workspace_delete_handler_1.WorkspaceDeleteHandler)
550
+ ], WorkspaceCommandContribution.prototype, "deleteHandler", void 0);
551
+ __decorate([
552
+ (0, inversify_1.inject)(workspace_duplicate_handler_1.WorkspaceDuplicateHandler),
553
+ __metadata("design:type", workspace_duplicate_handler_1.WorkspaceDuplicateHandler)
554
+ ], WorkspaceCommandContribution.prototype, "duplicateHandler", void 0);
555
+ __decorate([
556
+ (0, inversify_1.inject)(workspace_compare_handler_1.WorkspaceCompareHandler),
557
+ __metadata("design:type", workspace_compare_handler_1.WorkspaceCompareHandler)
558
+ ], WorkspaceCommandContribution.prototype, "compareHandler", void 0);
559
+ __decorate([
560
+ (0, inversify_1.inject)(clipboard_service_1.ClipboardService),
561
+ __metadata("design:type", Object)
562
+ ], WorkspaceCommandContribution.prototype, "clipboardService", void 0);
563
+ WorkspaceCommandContribution = __decorate([
564
+ (0, inversify_1.injectable)()
565
+ ], WorkspaceCommandContribution);
566
+ exports.WorkspaceCommandContribution = WorkspaceCommandContribution;
567
+ class WorkspaceRootUriAwareCommandHandler extends uri_command_handler_1.UriAwareCommandHandler {
568
+ constructor(workspaceService, selectionService, handler) {
569
+ super(selectionService, handler);
570
+ this.workspaceService = workspaceService;
571
+ }
572
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
573
+ isEnabled(...args) {
574
+ return super.isEnabled(...args) && !!this.workspaceService.tryGetRoots().length;
575
+ }
576
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
577
+ isVisible(...args) {
578
+ return super.isVisible(...args) && !!this.workspaceService.tryGetRoots().length;
579
+ }
580
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
581
+ getUri(...args) {
582
+ const uri = super.getUri(...args);
583
+ // Return the `uri` immediately if the resource exists in any of the workspace roots and is of `file` scheme.
584
+ if (uri && uri.scheme === 'file' && this.workspaceService.getWorkspaceRootUri(uri)) {
585
+ return uri;
586
+ }
587
+ // Return the first root if available.
588
+ if (!!this.workspaceService.tryGetRoots().length) {
589
+ return this.workspaceService.tryGetRoots()[0].resource;
590
+ }
591
+ }
592
+ }
593
+ exports.WorkspaceRootUriAwareCommandHandler = WorkspaceRootUriAwareCommandHandler;
594
594
  //# sourceMappingURL=workspace-commands.js.map