Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/files/browser/fileActions.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as nls from '../../../../nls.js';
7
import { isWindows, OperatingSystem, OS } from '../../../../base/common/platform.js';
8
import { extname, basename, isAbsolute } from '../../../../base/common/path.js';
9
import * as resources from '../../../../base/common/resources.js';
10
import { URI } from '../../../../base/common/uri.js';
11
import { toErrorMessage } from '../../../../base/common/errorMessage.js';
12
import { Action } from '../../../../base/common/actions.js';
13
import { dispose, IDisposable } from '../../../../base/common/lifecycle.js';
14
import { VIEWLET_ID, IFilesConfiguration, VIEW_ID, UndoConfirmLevel } from '../common/files.js';
15
import { IFileService } from '../../../../platform/files/common/files.js';
16
import { EditorResourceAccessor, SideBySideEditor } from '../../../common/editor.js';
17
import { IQuickInputService, ItemActivation } from '../../../../platform/quickinput/common/quickInput.js';
18
import { IInstantiationService, ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
19
import { ITextModel } from '../../../../editor/common/model.js';
20
import { IHostService } from '../../../services/host/browser/host.js';
21
import { REVEAL_IN_EXPLORER_COMMAND_ID, SAVE_ALL_IN_GROUP_COMMAND_ID, NEW_UNTITLED_FILE_COMMAND_ID } from './fileConstants.js';
22
import { ITextModelService, ITextModelContentProvider } from '../../../../editor/common/services/resolverService.js';
23
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
24
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';
25
import { ILanguageService } from '../../../../editor/common/languages/language.js';
26
import { IModelService } from '../../../../editor/common/services/model.js';
27
import { ICommandService, CommandsRegistry } from '../../../../platform/commands/common/commands.js';
28
import { RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
29
import { Schemas } from '../../../../base/common/network.js';
30
import { IDialogService, IConfirmationResult, getFileNamesMessage } from '../../../../platform/dialogs/common/dialogs.js';
31
import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';
32
import { IEditorService } from '../../../services/editor/common/editorService.js';
33
import { Constants } from '../../../../base/common/uint.js';
34
import { CLOSE_EDITORS_AND_GROUP_COMMAND_ID } from '../../../browser/parts/editor/editorCommands.js';
35
import { coalesce } from '../../../../base/common/arrays.js';
36
import { ExplorerItem, NewExplorerItem } from '../common/explorerModel.js';
37
import { getErrorMessage } from '../../../../base/common/errors.js';
38
import { triggerUpload } from '../../../../base/browser/dom.js';
39
import { IFilesConfigurationService } from '../../../services/filesConfiguration/common/filesConfigurationService.js';
40
import { IWorkingCopyService } from '../../../services/workingCopy/common/workingCopyService.js';
41
import { IWorkingCopy } from '../../../services/workingCopy/common/workingCopy.js';
42
import { timeout } from '../../../../base/common/async.js';
43
import { IWorkingCopyFileService } from '../../../services/workingCopy/common/workingCopyFileService.js';
44
import { Codicon } from '../../../../base/common/codicons.js';
45
import { ThemeIcon } from '../../../../base/common/themables.js';
46
import { ViewContainerLocation } from '../../../common/views.js';
47
import { IViewsService } from '../../../services/views/common/viewsService.js';
48
import { trim, rtrim } from '../../../../base/common/strings.js';
49
import { IUriIdentityService } from '../../../../platform/uriIdentity/common/uriIdentity.js';
50
import { ResourceFileEdit } from '../../../../editor/browser/services/bulkEditService.js';
51
import { IExplorerService } from './files.js';
52
import { BrowserFileUpload, FileDownload } from './fileImportExport.js';
53
import { IPaneCompositePartService } from '../../../services/panecomposite/browser/panecomposite.js';
54
import { IRemoteAgentService } from '../../../services/remote/common/remoteAgentService.js';
55
import { IPathService } from '../../../services/path/common/pathService.js';
56
import { Action2 } from '../../../../platform/actions/common/actions.js';
57
import { ActiveEditorCanToggleReadonlyContext, ActiveEditorContext, EmptyWorkspaceSupportContext } from '../../../common/contextkeys.js';
58
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
59
import { KeyChord, KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
60
import { Categories } from '../../../../platform/action/common/actionCommonCategories.js';
61
import { ILocalizedString } from '../../../../platform/action/common/action.js';
62
import { VSBuffer } from '../../../../base/common/buffer.js';
63
import { getPathForFile } from '../../../../platform/dnd/browser/dnd.js';
64
65
export const NEW_FILE_COMMAND_ID = 'explorer.newFile';
66
export const NEW_FILE_LABEL = nls.localize2('newFile', "New File...");
67
export const NEW_FOLDER_COMMAND_ID = 'explorer.newFolder';
68
export const NEW_FOLDER_LABEL = nls.localize2('newFolder', "New Folder...");
69
export const TRIGGER_RENAME_LABEL = nls.localize('rename', "Rename...");
70
export const MOVE_FILE_TO_TRASH_LABEL = nls.localize('delete', "Delete");
71
export const COPY_FILE_LABEL = nls.localize('copyFile', "Copy");
72
export const PASTE_FILE_LABEL = nls.localize('pasteFile', "Paste");
73
export const FileCopiedContext = new RawContextKey<boolean>('fileCopied', false);
74
export const DOWNLOAD_COMMAND_ID = 'explorer.download';
75
export const DOWNLOAD_LABEL = nls.localize('download', "Download...");
76
export const UPLOAD_COMMAND_ID = 'explorer.upload';
77
export const UPLOAD_LABEL = nls.localize('upload', "Upload...");
78
const CONFIRM_DELETE_SETTING_KEY = 'explorer.confirmDelete';
79
const MAX_UNDO_FILE_SIZE = 5000000; // 5mb
80
81
function onError(notificationService: INotificationService, error: any): void {
82
if (error.message === 'string') {
83
error = error.message;
84
}
85
86
notificationService.error(toErrorMessage(error, false));
87
}
88
89
async function refreshIfSeparator(value: string, explorerService: IExplorerService): Promise<void> {
90
if (value && ((value.indexOf('/') >= 0) || (value.indexOf('\\') >= 0))) {
91
// New input contains separator, multiple resources will get created workaround for #68204
92
await explorerService.refresh();
93
}
94
}
95
96
async function deleteFiles(explorerService: IExplorerService, workingCopyFileService: IWorkingCopyFileService, dialogService: IDialogService, configurationService: IConfigurationService, filesConfigurationService: IFilesConfigurationService, elements: ExplorerItem[], useTrash: boolean, skipConfirm = false, ignoreIfNotExists = false): Promise<void> {
97
let primaryButton: string;
98
if (useTrash) {
99
primaryButton = isWindows ? nls.localize('deleteButtonLabelRecycleBin', "&&Move to Recycle Bin") : nls.localize({ key: 'deleteButtonLabelTrash', comment: ['&& denotes a mnemonic'] }, "&&Move to Trash");
100
} else {
101
primaryButton = nls.localize({ key: 'deleteButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Delete");
102
}
103
104
// Handle dirty
105
const distinctElements = resources.distinctParents(elements, e => e.resource);
106
const dirtyWorkingCopies = new Set<IWorkingCopy>();
107
for (const distinctElement of distinctElements) {
108
for (const dirtyWorkingCopy of workingCopyFileService.getDirty(distinctElement.resource)) {
109
dirtyWorkingCopies.add(dirtyWorkingCopy);
110
}
111
}
112
113
if (dirtyWorkingCopies.size) {
114
let message: string;
115
if (distinctElements.length > 1) {
116
message = nls.localize('dirtyMessageFilesDelete', "You are deleting files with unsaved changes. Do you want to continue?");
117
} else if (distinctElements[0].isDirectory) {
118
if (dirtyWorkingCopies.size === 1) {
119
message = nls.localize('dirtyMessageFolderOneDelete', "You are deleting a folder {0} with unsaved changes in 1 file. Do you want to continue?", distinctElements[0].name);
120
} else {
121
message = nls.localize('dirtyMessageFolderDelete', "You are deleting a folder {0} with unsaved changes in {1} files. Do you want to continue?", distinctElements[0].name, dirtyWorkingCopies.size);
122
}
123
} else {
124
message = nls.localize('dirtyMessageFileDelete', "You are deleting {0} with unsaved changes. Do you want to continue?", distinctElements[0].name);
125
}
126
127
const response = await dialogService.confirm({
128
type: 'warning',
129
message,
130
detail: nls.localize('dirtyWarning', "Your changes will be lost if you don't save them."),
131
primaryButton
132
});
133
134
if (!response.confirmed) {
135
return;
136
} else {
137
skipConfirm = true;
138
}
139
}
140
141
// Handle readonly
142
if (!skipConfirm) {
143
const readonlyResources = distinctElements.filter(e => filesConfigurationService.isReadonly(e.resource));
144
if (readonlyResources.length) {
145
let message: string;
146
if (readonlyResources.length > 1) {
147
message = nls.localize('readonlyMessageFilesDelete', "You are deleting files that are configured to be read-only. Do you want to continue?");
148
} else if (readonlyResources[0].isDirectory) {
149
message = nls.localize('readonlyMessageFolderOneDelete', "You are deleting a folder {0} that is configured to be read-only. Do you want to continue?", distinctElements[0].name);
150
} else {
151
message = nls.localize('readonlyMessageFolderDelete', "You are deleting a file {0} that is configured to be read-only. Do you want to continue?", distinctElements[0].name);
152
}
153
154
const response = await dialogService.confirm({
155
type: 'warning',
156
message,
157
detail: nls.localize('continueDetail', "The read-only protection will be overridden if you continue."),
158
primaryButton: nls.localize('continueButtonLabel', "Continue")
159
});
160
161
if (!response.confirmed) {
162
return;
163
}
164
}
165
}
166
167
let confirmation: IConfirmationResult;
168
169
// We do not support undo of folders, so in that case the delete action is irreversible
170
const deleteDetail = distinctElements.some(e => e.isDirectory) ? nls.localize('irreversible', "This action is irreversible!") :
171
distinctElements.length > 1 ? nls.localize('restorePlural', "You can restore these files using the Undo command.") : nls.localize('restore', "You can restore this file using the Undo command.");
172
173
// Check if we need to ask for confirmation at all
174
if (skipConfirm || (useTrash && configurationService.getValue<boolean>(CONFIRM_DELETE_SETTING_KEY) === false)) {
175
confirmation = { confirmed: true };
176
}
177
178
// Confirm for moving to trash
179
else if (useTrash) {
180
let { message, detail } = getMoveToTrashMessage(distinctElements);
181
detail += detail ? '\n' : '';
182
if (isWindows) {
183
detail += distinctElements.length > 1 ? nls.localize('undoBinFiles', "You can restore these files from the Recycle Bin.") : nls.localize('undoBin', "You can restore this file from the Recycle Bin.");
184
} else {
185
detail += distinctElements.length > 1 ? nls.localize('undoTrashFiles', "You can restore these files from the Trash.") : nls.localize('undoTrash', "You can restore this file from the Trash.");
186
}
187
188
confirmation = await dialogService.confirm({
189
message,
190
detail,
191
primaryButton,
192
checkbox: {
193
label: nls.localize('doNotAskAgain', "Do not ask me again")
194
}
195
});
196
}
197
198
// Confirm for deleting permanently
199
else {
200
let { message, detail } = getDeleteMessage(distinctElements);
201
detail += detail ? '\n' : '';
202
detail += deleteDetail;
203
confirmation = await dialogService.confirm({
204
type: 'warning',
205
message,
206
detail,
207
primaryButton
208
});
209
}
210
211
// Check for confirmation checkbox
212
if (confirmation.confirmed && confirmation.checkboxChecked === true) {
213
await configurationService.updateValue(CONFIRM_DELETE_SETTING_KEY, false);
214
}
215
216
// Check for confirmation
217
if (!confirmation.confirmed) {
218
return;
219
}
220
221
// Call function
222
try {
223
const resourceFileEdits = distinctElements.map(e => new ResourceFileEdit(e.resource, undefined, { recursive: true, folder: e.isDirectory, ignoreIfNotExists, skipTrashBin: !useTrash, maxSize: MAX_UNDO_FILE_SIZE }));
224
const options = {
225
undoLabel: distinctElements.length > 1 ? nls.localize({ key: 'deleteBulkEdit', comment: ['Placeholder will be replaced by the number of files deleted'] }, "Delete {0} files", distinctElements.length) : nls.localize({ key: 'deleteFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file deleted'] }, "Delete {0}", distinctElements[0].name),
226
progressLabel: distinctElements.length > 1 ? nls.localize({ key: 'deletingBulkEdit', comment: ['Placeholder will be replaced by the number of files deleted'] }, "Deleting {0} files", distinctElements.length) : nls.localize({ key: 'deletingFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file deleted'] }, "Deleting {0}", distinctElements[0].name),
227
};
228
await explorerService.applyBulkEdit(resourceFileEdits, options);
229
} catch (error) {
230
231
// Handle error to delete file(s) from a modal confirmation dialog
232
let errorMessage: string;
233
let detailMessage: string | undefined;
234
let primaryButton: string;
235
if (useTrash) {
236
errorMessage = isWindows ? nls.localize('binFailed', "Failed to delete using the Recycle Bin. Do you want to permanently delete instead?") : nls.localize('trashFailed', "Failed to delete using the Trash. Do you want to permanently delete instead?");
237
detailMessage = deleteDetail;
238
primaryButton = nls.localize({ key: 'deletePermanentlyButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Delete Permanently");
239
} else {
240
errorMessage = toErrorMessage(error, false);
241
primaryButton = nls.localize({ key: 'retryButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Retry");
242
}
243
244
const res = await dialogService.confirm({
245
type: 'warning',
246
message: errorMessage,
247
detail: detailMessage,
248
primaryButton
249
});
250
251
if (res.confirmed) {
252
if (useTrash) {
253
useTrash = false; // Delete Permanently
254
}
255
256
skipConfirm = true;
257
ignoreIfNotExists = true;
258
259
return deleteFiles(explorerService, workingCopyFileService, dialogService, configurationService, filesConfigurationService, elements, useTrash, skipConfirm, ignoreIfNotExists);
260
}
261
}
262
}
263
264
function getMoveToTrashMessage(distinctElements: ExplorerItem[]): { message: string; detail: string } {
265
if (containsBothDirectoryAndFile(distinctElements)) {
266
return {
267
message: nls.localize('confirmMoveTrashMessageFilesAndDirectories', "Are you sure you want to delete the following {0} files/directories and their contents?", distinctElements.length),
268
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
269
};
270
}
271
272
if (distinctElements.length > 1) {
273
if (distinctElements[0].isDirectory) {
274
return {
275
message: nls.localize('confirmMoveTrashMessageMultipleDirectories', "Are you sure you want to delete the following {0} directories and their contents?", distinctElements.length),
276
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
277
};
278
}
279
280
return {
281
message: nls.localize('confirmMoveTrashMessageMultiple', "Are you sure you want to delete the following {0} files?", distinctElements.length),
282
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
283
};
284
}
285
286
if (distinctElements[0].isDirectory && !distinctElements[0].isSymbolicLink) {
287
return { message: nls.localize('confirmMoveTrashMessageFolder', "Are you sure you want to delete '{0}' and its contents?", distinctElements[0].name), detail: '' };
288
}
289
290
return { message: nls.localize('confirmMoveTrashMessageFile', "Are you sure you want to delete '{0}'?", distinctElements[0].name), detail: '' };
291
}
292
293
function getDeleteMessage(distinctElements: ExplorerItem[]): { message: string; detail: string } {
294
if (containsBothDirectoryAndFile(distinctElements)) {
295
return {
296
message: nls.localize('confirmDeleteMessageFilesAndDirectories', "Are you sure you want to permanently delete the following {0} files/directories and their contents?", distinctElements.length),
297
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
298
};
299
}
300
301
if (distinctElements.length > 1) {
302
if (distinctElements[0].isDirectory) {
303
return {
304
message: nls.localize('confirmDeleteMessageMultipleDirectories', "Are you sure you want to permanently delete the following {0} directories and their contents?", distinctElements.length),
305
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
306
};
307
}
308
309
return {
310
message: nls.localize('confirmDeleteMessageMultiple', "Are you sure you want to permanently delete the following {0} files?", distinctElements.length),
311
detail: getFileNamesMessage(distinctElements.map(e => e.resource))
312
};
313
}
314
315
if (distinctElements[0].isDirectory) {
316
return { message: nls.localize('confirmDeleteMessageFolder', "Are you sure you want to permanently delete '{0}' and its contents?", distinctElements[0].name), detail: '' };
317
}
318
319
return { message: nls.localize('confirmDeleteMessageFile', "Are you sure you want to permanently delete '{0}'?", distinctElements[0].name), detail: '' };
320
}
321
322
function containsBothDirectoryAndFile(distinctElements: ExplorerItem[]): boolean {
323
const directory = distinctElements.find(element => element.isDirectory);
324
const file = distinctElements.find(element => !element.isDirectory);
325
326
return !!directory && !!file;
327
}
328
329
330
export async function findValidPasteFileTarget(
331
explorerService: IExplorerService,
332
fileService: IFileService,
333
dialogService: IDialogService,
334
targetFolder: ExplorerItem,
335
fileToPaste: { resource: URI | string; isDirectory?: boolean; allowOverwrite: boolean },
336
incrementalNaming: 'simple' | 'smart' | 'disabled'
337
): Promise<URI | undefined> {
338
339
let name = typeof fileToPaste.resource === 'string' ? fileToPaste.resource : resources.basenameOrAuthority(fileToPaste.resource);
340
let candidate = resources.joinPath(targetFolder.resource, name);
341
342
// In the disabled case we must ask if it's ok to overwrite the file if it exists
343
if (incrementalNaming === 'disabled') {
344
const canOverwrite = await askForOverwrite(fileService, dialogService, candidate);
345
if (!canOverwrite) {
346
return;
347
}
348
}
349
350
while (true && !fileToPaste.allowOverwrite) {
351
if (!explorerService.findClosest(candidate)) {
352
break;
353
}
354
355
if (incrementalNaming !== 'disabled') {
356
name = incrementFileName(name, !!fileToPaste.isDirectory, incrementalNaming);
357
}
358
candidate = resources.joinPath(targetFolder.resource, name);
359
}
360
361
return candidate;
362
}
363
364
export function incrementFileName(name: string, isFolder: boolean, incrementalNaming: 'simple' | 'smart'): string {
365
if (incrementalNaming === 'simple') {
366
let namePrefix = name;
367
let extSuffix = '';
368
if (!isFolder) {
369
extSuffix = extname(name);
370
namePrefix = basename(name, extSuffix);
371
}
372
373
// name copy 5(.txt) => name copy 6(.txt)
374
// name copy(.txt) => name copy 2(.txt)
375
const suffixRegex = /^(.+ copy)( \d+)?$/;
376
if (suffixRegex.test(namePrefix)) {
377
return namePrefix.replace(suffixRegex, (match, g1?, g2?) => {
378
const number = (g2 ? parseInt(g2) : 1);
379
return number === 0
380
? `${g1}`
381
: (number < Constants.MAX_SAFE_SMALL_INTEGER
382
? `${g1} ${number + 1}`
383
: `${g1}${g2} copy`);
384
}) + extSuffix;
385
}
386
387
// name(.txt) => name copy(.txt)
388
return `${namePrefix} copy${extSuffix}`;
389
}
390
391
const separators = '[\\.\\-_]';
392
const maxNumber = Constants.MAX_SAFE_SMALL_INTEGER;
393
394
// file.1.txt=>file.2.txt
395
const suffixFileRegex = RegExp('(.*' + separators + ')(\\d+)(\\..*)$');
396
if (!isFolder && name.match(suffixFileRegex)) {
397
return name.replace(suffixFileRegex, (match, g1?, g2?, g3?) => {
398
const number = parseInt(g2);
399
return number < maxNumber
400
? g1 + String(number + 1).padStart(g2.length, '0') + g3
401
: `${g1}${g2}.1${g3}`;
402
});
403
}
404
405
// 1.file.txt=>2.file.txt
406
const prefixFileRegex = RegExp('(\\d+)(' + separators + '.*)(\\..*)$');
407
if (!isFolder && name.match(prefixFileRegex)) {
408
return name.replace(prefixFileRegex, (match, g1?, g2?, g3?) => {
409
const number = parseInt(g1);
410
return number < maxNumber
411
? String(number + 1).padStart(g1.length, '0') + g2 + g3
412
: `${g1}${g2}.1${g3}`;
413
});
414
}
415
416
// 1.txt=>2.txt
417
const prefixFileNoNameRegex = RegExp('(\\d+)(\\..*)$');
418
if (!isFolder && name.match(prefixFileNoNameRegex)) {
419
return name.replace(prefixFileNoNameRegex, (match, g1?, g2?) => {
420
const number = parseInt(g1);
421
return number < maxNumber
422
? String(number + 1).padStart(g1.length, '0') + g2
423
: `${g1}.1${g2}`;
424
});
425
}
426
427
// file.txt=>file.1.txt
428
const lastIndexOfDot = name.lastIndexOf('.');
429
if (!isFolder && lastIndexOfDot >= 0) {
430
return `${name.substr(0, lastIndexOfDot)}.1${name.substr(lastIndexOfDot)}`;
431
}
432
433
// 123 => 124
434
const noNameNoExtensionRegex = RegExp('(\\d+)$');
435
if (!isFolder && lastIndexOfDot === -1 && name.match(noNameNoExtensionRegex)) {
436
return name.replace(noNameNoExtensionRegex, (match, g1?) => {
437
const number = parseInt(g1);
438
return number < maxNumber
439
? String(number + 1).padStart(g1.length, '0')
440
: `${g1}.1`;
441
});
442
}
443
444
// file => file1
445
// file1 => file2
446
const noExtensionRegex = RegExp('(.*)(\\d*)$');
447
if (!isFolder && lastIndexOfDot === -1 && name.match(noExtensionRegex)) {
448
return name.replace(noExtensionRegex, (match, g1?, g2?) => {
449
let number = parseInt(g2);
450
if (isNaN(number)) {
451
number = 0;
452
}
453
return number < maxNumber
454
? g1 + String(number + 1).padStart(g2.length, '0')
455
: `${g1}${g2}.1`;
456
});
457
}
458
459
// folder.1=>folder.2
460
if (isFolder && name.match(/(\d+)$/)) {
461
return name.replace(/(\d+)$/, (match, ...groups) => {
462
const number = parseInt(groups[0]);
463
return number < maxNumber
464
? String(number + 1).padStart(groups[0].length, '0')
465
: `${groups[0]}.1`;
466
});
467
}
468
469
// 1.folder=>2.folder
470
if (isFolder && name.match(/^(\d+)/)) {
471
return name.replace(/^(\d+)(.*)$/, (match, ...groups) => {
472
const number = parseInt(groups[0]);
473
return number < maxNumber
474
? String(number + 1).padStart(groups[0].length, '0') + groups[1]
475
: `${groups[0]}${groups[1]}.1`;
476
});
477
}
478
479
// file/folder=>file.1/folder.1
480
return `${name}.1`;
481
}
482
483
/**
484
* Checks to see if the resource already exists, if so prompts the user if they would be ok with it being overwritten
485
* @param fileService The file service
486
* @param dialogService The dialog service
487
* @param targetResource The resource to be overwritten
488
* @return A boolean indicating if the user is ok with resource being overwritten, if the resource does not exist it returns true.
489
*/
490
async function askForOverwrite(fileService: IFileService, dialogService: IDialogService, targetResource: URI): Promise<boolean> {
491
const exists = await fileService.exists(targetResource);
492
if (!exists) {
493
return true;
494
}
495
// Ask for overwrite confirmation
496
const { confirmed } = await dialogService.confirm({
497
type: Severity.Warning,
498
message: nls.localize('confirmOverwrite', "A file or folder with the name '{0}' already exists in the destination folder. Do you want to replace it?", basename(targetResource.path)),
499
primaryButton: nls.localize('replaceButtonLabel', "&&Replace")
500
});
501
return confirmed;
502
}
503
504
// Global Compare with
505
export class GlobalCompareResourcesAction extends Action2 {
506
507
static readonly ID = 'workbench.files.action.compareFileWith';
508
static readonly LABEL = nls.localize2('globalCompareFile', "Compare Active File With...");
509
510
constructor() {
511
super({
512
id: GlobalCompareResourcesAction.ID,
513
title: GlobalCompareResourcesAction.LABEL,
514
f1: true,
515
category: Categories.File,
516
precondition: ActiveEditorContext,
517
metadata: {
518
description: nls.localize2('compareFileWithMeta', "Opens a picker to select a file to diff with the active editor.")
519
}
520
});
521
}
522
523
override async run(accessor: ServicesAccessor): Promise<void> {
524
const editorService = accessor.get(IEditorService);
525
const textModelService = accessor.get(ITextModelService);
526
const quickInputService = accessor.get(IQuickInputService);
527
528
const activeInput = editorService.activeEditor;
529
const activeResource = EditorResourceAccessor.getOriginalUri(activeInput);
530
if (activeResource && textModelService.canHandleResource(activeResource)) {
531
const picks = await quickInputService.quickAccess.pick('', { itemActivation: ItemActivation.SECOND });
532
if (picks?.length === 1) {
533
const resource = (picks[0] as unknown as { resource: unknown }).resource;
534
if (URI.isUri(resource) && textModelService.canHandleResource(resource)) {
535
editorService.openEditor({
536
original: { resource: activeResource },
537
modified: { resource: resource },
538
options: { pinned: true }
539
});
540
}
541
}
542
}
543
}
544
}
545
546
export class ToggleAutoSaveAction extends Action2 {
547
static readonly ID = 'workbench.action.toggleAutoSave';
548
549
constructor() {
550
super({
551
id: ToggleAutoSaveAction.ID,
552
title: nls.localize2('toggleAutoSave', "Toggle Auto Save"),
553
f1: true,
554
category: Categories.File,
555
metadata: { description: nls.localize2('toggleAutoSaveDescription', "Toggle the ability to save files automatically after typing") }
556
});
557
}
558
559
override run(accessor: ServicesAccessor): Promise<void> {
560
const filesConfigurationService = accessor.get(IFilesConfigurationService);
561
return filesConfigurationService.toggleAutoSave();
562
}
563
}
564
565
abstract class BaseSaveAllAction extends Action {
566
private lastDirtyState: boolean;
567
568
constructor(
569
id: string,
570
label: string,
571
@ICommandService protected commandService: ICommandService,
572
@INotificationService private notificationService: INotificationService,
573
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService
574
) {
575
super(id, label);
576
577
this.lastDirtyState = this.workingCopyService.hasDirty;
578
this.enabled = this.lastDirtyState;
579
580
this.registerListeners();
581
}
582
583
protected abstract doRun(context: unknown): Promise<void>;
584
585
private registerListeners(): void {
586
587
// update enablement based on working copy changes
588
this._register(this.workingCopyService.onDidChangeDirty(workingCopy => this.updateEnablement(workingCopy)));
589
}
590
591
private updateEnablement(workingCopy: IWorkingCopy): void {
592
const hasDirty = workingCopy.isDirty() || this.workingCopyService.hasDirty;
593
if (this.lastDirtyState !== hasDirty) {
594
this.enabled = hasDirty;
595
this.lastDirtyState = this.enabled;
596
}
597
}
598
599
override async run(context?: unknown): Promise<void> {
600
try {
601
await this.doRun(context);
602
} catch (error) {
603
onError(this.notificationService, error);
604
}
605
}
606
}
607
608
export class SaveAllInGroupAction extends BaseSaveAllAction {
609
610
static readonly ID = 'workbench.files.action.saveAllInGroup';
611
static readonly LABEL = nls.localize('saveAllInGroup', "Save All in Group");
612
613
override get class(): string {
614
return 'explorer-action ' + ThemeIcon.asClassName(Codicon.saveAll);
615
}
616
617
protected doRun(context: unknown): Promise<void> {
618
return this.commandService.executeCommand(SAVE_ALL_IN_GROUP_COMMAND_ID, {}, context);
619
}
620
}
621
622
export class CloseGroupAction extends Action {
623
624
static readonly ID = 'workbench.files.action.closeGroup';
625
static readonly LABEL = nls.localize('closeGroup', "Close Group");
626
627
constructor(id: string, label: string, @ICommandService private readonly commandService: ICommandService) {
628
super(id, label, ThemeIcon.asClassName(Codicon.closeAll));
629
}
630
631
override run(context?: unknown): Promise<void> {
632
return this.commandService.executeCommand(CLOSE_EDITORS_AND_GROUP_COMMAND_ID, {}, context);
633
}
634
}
635
636
export class FocusFilesExplorer extends Action2 {
637
638
static readonly ID = 'workbench.files.action.focusFilesExplorer';
639
static readonly LABEL = nls.localize2('focusFilesExplorer', "Focus on Files Explorer");
640
641
constructor() {
642
super({
643
id: FocusFilesExplorer.ID,
644
title: FocusFilesExplorer.LABEL,
645
f1: true,
646
category: Categories.File,
647
metadata: {
648
description: nls.localize2('focusFilesExplorerMetadata', "Moves focus to the file explorer view container.")
649
}
650
});
651
}
652
653
override async run(accessor: ServicesAccessor): Promise<void> {
654
const paneCompositeService = accessor.get(IPaneCompositePartService);
655
await paneCompositeService.openPaneComposite(VIEWLET_ID, ViewContainerLocation.Sidebar, true);
656
}
657
}
658
659
export class ShowActiveFileInExplorer extends Action2 {
660
661
static readonly ID = 'workbench.files.action.showActiveFileInExplorer';
662
static readonly LABEL = nls.localize2('showInExplorer', "Reveal Active File in Explorer View");
663
664
constructor() {
665
super({
666
id: ShowActiveFileInExplorer.ID,
667
title: ShowActiveFileInExplorer.LABEL,
668
f1: true,
669
category: Categories.File,
670
metadata: {
671
description: nls.localize2('showInExplorerMetadata', "Reveals and selects the active file within the explorer view.")
672
}
673
});
674
}
675
676
override async run(accessor: ServicesAccessor): Promise<void> {
677
const commandService = accessor.get(ICommandService);
678
const editorService = accessor.get(IEditorService);
679
const resource = EditorResourceAccessor.getOriginalUri(editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
680
if (resource) {
681
commandService.executeCommand(REVEAL_IN_EXPLORER_COMMAND_ID, resource);
682
}
683
}
684
}
685
686
export class OpenActiveFileInEmptyWorkspace extends Action2 {
687
688
static readonly ID = 'workbench.action.files.showOpenedFileInNewWindow';
689
static readonly LABEL = nls.localize2('openFileInEmptyWorkspace', "Open Active File in New Empty Workspace");
690
691
constructor(
692
) {
693
super({
694
id: OpenActiveFileInEmptyWorkspace.ID,
695
title: OpenActiveFileInEmptyWorkspace.LABEL,
696
f1: true,
697
category: Categories.File,
698
precondition: EmptyWorkspaceSupportContext,
699
metadata: {
700
description: nls.localize2('openFileInEmptyWorkspaceMetadata', "Opens the active file in a new window with no folders open.")
701
}
702
});
703
}
704
705
override async run(accessor: ServicesAccessor): Promise<void> {
706
const editorService = accessor.get(IEditorService);
707
const hostService = accessor.get(IHostService);
708
const dialogService = accessor.get(IDialogService);
709
const fileService = accessor.get(IFileService);
710
711
const fileResource = EditorResourceAccessor.getOriginalUri(editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
712
if (fileResource) {
713
if (fileService.hasProvider(fileResource)) {
714
hostService.openWindow([{ fileUri: fileResource }], { forceNewWindow: true });
715
} else {
716
dialogService.error(nls.localize('openFileToShowInNewWindow.unsupportedschema', "The active editor must contain an openable resource."));
717
}
718
}
719
}
720
}
721
722
export function validateFileName(pathService: IPathService, item: ExplorerItem, name: string, os: OperatingSystem): { content: string; severity: Severity } | null {
723
// Produce a well formed file name
724
name = getWellFormedFileName(name);
725
726
// Name not provided
727
if (!name || name.length === 0 || /^\s+$/.test(name)) {
728
return {
729
content: nls.localize('emptyFileNameError', "A file or folder name must be provided."),
730
severity: Severity.Error
731
};
732
}
733
734
// Relative paths only
735
if (name[0] === '/' || name[0] === '\\') {
736
return {
737
content: nls.localize('fileNameStartsWithSlashError', "A file or folder name cannot start with a slash."),
738
severity: Severity.Error
739
};
740
}
741
742
const names = coalesce(name.split(/[\\/]/));
743
const parent = item.parent;
744
745
if (name !== item.name) {
746
// Do not allow to overwrite existing file
747
const child = parent?.getChild(name);
748
if (child && child !== item) {
749
return {
750
content: nls.localize('fileNameExistsError', "A file or folder **{0}** already exists at this location. Please choose a different name.", name),
751
severity: Severity.Error
752
};
753
}
754
}
755
756
// Check for invalid file name.
757
if (names.some(folderName => !pathService.hasValidBasename(item.resource, os, folderName))) {
758
// Escape * characters
759
const escapedName = name.replace(/\*/g, '\\*'); // CodeQL [SM02383] This only processes filenames which are enforced against having backslashes in them farther up in the stack.
760
return {
761
content: nls.localize('invalidFileNameError', "The name **{0}** is not valid as a file or folder name. Please choose a different name.", trimLongName(escapedName)),
762
severity: Severity.Error
763
};
764
}
765
766
if (names.some(name => /^\s|\s$/.test(name))) {
767
return {
768
content: nls.localize('fileNameWhitespaceWarning', "Leading or trailing whitespace detected in file or folder name."),
769
severity: Severity.Warning
770
};
771
}
772
773
return null;
774
}
775
776
function trimLongName(name: string): string {
777
if (name?.length > 255) {
778
return `${name.substr(0, 255)}...`;
779
}
780
781
return name;
782
}
783
784
function getWellFormedFileName(filename: string): string {
785
if (!filename) {
786
return filename;
787
}
788
789
// Trim tabs
790
filename = trim(filename, '\t');
791
792
// Remove trailing slashes
793
filename = rtrim(filename, '/');
794
filename = rtrim(filename, '\\');
795
796
return filename;
797
}
798
799
export class CompareNewUntitledTextFilesAction extends Action2 {
800
801
static readonly ID = 'workbench.files.action.compareNewUntitledTextFiles';
802
static readonly LABEL = nls.localize2('compareNewUntitledTextFiles', "Compare New Untitled Text Files");
803
804
constructor() {
805
super({
806
id: CompareNewUntitledTextFilesAction.ID,
807
title: CompareNewUntitledTextFilesAction.LABEL,
808
f1: true,
809
category: Categories.File,
810
metadata: {
811
description: nls.localize2('compareNewUntitledTextFilesMeta', "Opens a new diff editor with two untitled files.")
812
}
813
});
814
}
815
816
override async run(accessor: ServicesAccessor): Promise<void> {
817
const editorService = accessor.get(IEditorService);
818
819
await editorService.openEditor({
820
original: { resource: undefined },
821
modified: { resource: undefined },
822
options: { pinned: true }
823
});
824
}
825
}
826
827
export class CompareWithClipboardAction extends Action2 {
828
829
static readonly ID = 'workbench.files.action.compareWithClipboard';
830
static readonly LABEL = nls.localize2('compareWithClipboard', "Compare Active File with Clipboard");
831
832
private registrationDisposal: IDisposable | undefined;
833
private static SCHEME_COUNTER = 0;
834
835
constructor() {
836
super({
837
id: CompareWithClipboardAction.ID,
838
title: CompareWithClipboardAction.LABEL,
839
f1: true,
840
category: Categories.File,
841
keybinding: { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.KeyC), weight: KeybindingWeight.WorkbenchContrib },
842
metadata: {
843
description: nls.localize2('compareWithClipboardMeta', "Opens a new diff editor to compare the active file with the contents of the clipboard.")
844
}
845
});
846
}
847
848
override async run(accessor: ServicesAccessor): Promise<void> {
849
const editorService = accessor.get(IEditorService);
850
const instantiationService = accessor.get(IInstantiationService);
851
const textModelService = accessor.get(ITextModelService);
852
const fileService = accessor.get(IFileService);
853
854
const resource = EditorResourceAccessor.getOriginalUri(editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
855
const scheme = `clipboardCompare${CompareWithClipboardAction.SCHEME_COUNTER++}`;
856
if (resource && (fileService.hasProvider(resource) || resource.scheme === Schemas.untitled)) {
857
if (!this.registrationDisposal) {
858
const provider = instantiationService.createInstance(ClipboardContentProvider);
859
this.registrationDisposal = textModelService.registerTextModelContentProvider(scheme, provider);
860
}
861
862
const name = resources.basename(resource);
863
const editorLabel = nls.localize('clipboardComparisonLabel', "Clipboard ↔ {0}", name);
864
865
await editorService.openEditor({
866
original: { resource: resource.with({ scheme }) },
867
modified: { resource: resource },
868
label: editorLabel,
869
options: { pinned: true }
870
}).finally(() => {
871
dispose(this.registrationDisposal);
872
this.registrationDisposal = undefined;
873
});
874
}
875
}
876
877
dispose(): void {
878
dispose(this.registrationDisposal);
879
this.registrationDisposal = undefined;
880
}
881
}
882
883
class ClipboardContentProvider implements ITextModelContentProvider {
884
constructor(
885
@IClipboardService private readonly clipboardService: IClipboardService,
886
@ILanguageService private readonly languageService: ILanguageService,
887
@IModelService private readonly modelService: IModelService
888
) { }
889
890
async provideTextContent(resource: URI): Promise<ITextModel> {
891
const text = await this.clipboardService.readText();
892
const model = this.modelService.createModel(text, this.languageService.createByFilepathOrFirstLine(resource), resource);
893
894
return model;
895
}
896
}
897
898
function onErrorWithRetry(notificationService: INotificationService, error: unknown, retry: () => Promise<unknown>): void {
899
notificationService.prompt(Severity.Error, toErrorMessage(error, false),
900
[{
901
label: nls.localize('retry', "Retry"),
902
run: () => retry()
903
}]
904
);
905
}
906
907
async function openExplorerAndCreate(accessor: ServicesAccessor, isFolder: boolean): Promise<void> {
908
const explorerService = accessor.get(IExplorerService);
909
const fileService = accessor.get(IFileService);
910
const configService = accessor.get(IConfigurationService);
911
const filesConfigService = accessor.get(IFilesConfigurationService);
912
const editorService = accessor.get(IEditorService);
913
const viewsService = accessor.get(IViewsService);
914
const notificationService = accessor.get(INotificationService);
915
const remoteAgentService = accessor.get(IRemoteAgentService);
916
const commandService = accessor.get(ICommandService);
917
const pathService = accessor.get(IPathService);
918
919
const wasHidden = !viewsService.isViewVisible(VIEW_ID);
920
const view = await viewsService.openView(VIEW_ID, true);
921
if (wasHidden) {
922
// Give explorer some time to resolve itself #111218
923
await timeout(500);
924
}
925
if (!view) {
926
// Can happen in empty workspace case (https://github.com/microsoft/vscode/issues/100604)
927
928
if (isFolder) {
929
throw new Error('Open a folder or workspace first.');
930
}
931
932
return commandService.executeCommand(NEW_UNTITLED_FILE_COMMAND_ID);
933
}
934
935
const stats = explorerService.getContext(false);
936
const stat = stats.length > 0 ? stats[0] : undefined;
937
let folder: ExplorerItem;
938
if (stat) {
939
folder = stat.isDirectory ? stat : (stat.parent || explorerService.roots[0]);
940
} else {
941
folder = explorerService.roots[0];
942
}
943
944
if (folder.isReadonly) {
945
throw new Error('Parent folder is readonly.');
946
}
947
948
const newStat = new NewExplorerItem(fileService, configService, filesConfigService, folder, isFolder);
949
folder.addChild(newStat);
950
951
const onSuccess = async (value: string): Promise<void> => {
952
try {
953
const resourceToCreate = resources.joinPath(folder.resource, value);
954
if (value.endsWith('/')) {
955
isFolder = true;
956
}
957
await explorerService.applyBulkEdit([new ResourceFileEdit(undefined, resourceToCreate, { folder: isFolder })], {
958
undoLabel: nls.localize('createBulkEdit', "Create {0}", value),
959
progressLabel: nls.localize('creatingBulkEdit', "Creating {0}", value),
960
confirmBeforeUndo: true
961
});
962
await refreshIfSeparator(value, explorerService);
963
964
if (isFolder) {
965
await explorerService.select(resourceToCreate, true);
966
} else {
967
await editorService.openEditor({ resource: resourceToCreate, options: { pinned: true } });
968
}
969
} catch (error) {
970
onErrorWithRetry(notificationService, error, () => onSuccess(value));
971
}
972
};
973
974
const os = (await remoteAgentService.getEnvironment())?.os ?? OS;
975
976
await explorerService.setEditable(newStat, {
977
validationMessage: value => validateFileName(pathService, newStat, value, os),
978
onFinish: async (value, success) => {
979
folder.removeChild(newStat);
980
await explorerService.setEditable(newStat, null);
981
if (success) {
982
onSuccess(value);
983
}
984
}
985
});
986
}
987
988
CommandsRegistry.registerCommand({
989
id: NEW_FILE_COMMAND_ID,
990
handler: async (accessor) => {
991
await openExplorerAndCreate(accessor, false);
992
}
993
});
994
995
CommandsRegistry.registerCommand({
996
id: NEW_FOLDER_COMMAND_ID,
997
handler: async (accessor) => {
998
await openExplorerAndCreate(accessor, true);
999
}
1000
});
1001
1002
export const renameHandler = async (accessor: ServicesAccessor) => {
1003
const explorerService = accessor.get(IExplorerService);
1004
const notificationService = accessor.get(INotificationService);
1005
const remoteAgentService = accessor.get(IRemoteAgentService);
1006
const pathService = accessor.get(IPathService);
1007
const configurationService = accessor.get(IConfigurationService);
1008
1009
const stats = explorerService.getContext(false);
1010
const stat = stats.length > 0 ? stats[0] : undefined;
1011
if (!stat) {
1012
return;
1013
}
1014
1015
const os = (await remoteAgentService.getEnvironment())?.os ?? OS;
1016
1017
await explorerService.setEditable(stat, {
1018
validationMessage: value => validateFileName(pathService, stat, value, os),
1019
onFinish: async (value, success) => {
1020
if (success) {
1021
const parentResource = stat.parent!.resource;
1022
const targetResource = resources.joinPath(parentResource, value);
1023
if (stat.resource.toString() !== targetResource.toString()) {
1024
try {
1025
await explorerService.applyBulkEdit([new ResourceFileEdit(stat.resource, targetResource)], {
1026
confirmBeforeUndo: configurationService.getValue<IFilesConfiguration>().explorer.confirmUndo === UndoConfirmLevel.Verbose,
1027
undoLabel: nls.localize('renameBulkEdit', "Rename {0} to {1}", stat.name, value),
1028
progressLabel: nls.localize('renamingBulkEdit', "Renaming {0} to {1}", stat.name, value),
1029
});
1030
await refreshIfSeparator(value, explorerService);
1031
} catch (e) {
1032
notificationService.error(e);
1033
}
1034
}
1035
}
1036
await explorerService.setEditable(stat, null);
1037
}
1038
});
1039
};
1040
1041
export const moveFileToTrashHandler = async (accessor: ServicesAccessor) => {
1042
const explorerService = accessor.get(IExplorerService);
1043
const stats = explorerService.getContext(true).filter(s => !s.isRoot);
1044
if (stats.length) {
1045
await deleteFiles(accessor.get(IExplorerService), accessor.get(IWorkingCopyFileService), accessor.get(IDialogService), accessor.get(IConfigurationService), accessor.get(IFilesConfigurationService), stats, true);
1046
}
1047
};
1048
1049
export const deleteFileHandler = async (accessor: ServicesAccessor) => {
1050
const explorerService = accessor.get(IExplorerService);
1051
const stats = explorerService.getContext(true).filter(s => !s.isRoot);
1052
1053
if (stats.length) {
1054
await deleteFiles(accessor.get(IExplorerService), accessor.get(IWorkingCopyFileService), accessor.get(IDialogService), accessor.get(IConfigurationService), accessor.get(IFilesConfigurationService), stats, false);
1055
}
1056
};
1057
1058
let pasteShouldMove = false;
1059
export const copyFileHandler = async (accessor: ServicesAccessor) => {
1060
const explorerService = accessor.get(IExplorerService);
1061
const stats = explorerService.getContext(true);
1062
if (stats.length > 0) {
1063
await explorerService.setToCopy(stats, false);
1064
pasteShouldMove = false;
1065
}
1066
};
1067
1068
export const cutFileHandler = async (accessor: ServicesAccessor) => {
1069
const explorerService = accessor.get(IExplorerService);
1070
const stats = explorerService.getContext(true);
1071
if (stats.length > 0) {
1072
await explorerService.setToCopy(stats, true);
1073
pasteShouldMove = true;
1074
}
1075
};
1076
1077
const downloadFileHandler = async (accessor: ServicesAccessor) => {
1078
const explorerService = accessor.get(IExplorerService);
1079
const notificationService = accessor.get(INotificationService);
1080
const instantiationService = accessor.get(IInstantiationService);
1081
1082
const context = explorerService.getContext(true);
1083
const explorerItems = context.length ? context : explorerService.roots;
1084
1085
const downloadHandler = instantiationService.createInstance(FileDownload);
1086
1087
try {
1088
await downloadHandler.download(explorerItems);
1089
} catch (error) {
1090
notificationService.error(error);
1091
1092
throw error;
1093
}
1094
};
1095
1096
CommandsRegistry.registerCommand({
1097
id: DOWNLOAD_COMMAND_ID,
1098
handler: downloadFileHandler
1099
});
1100
1101
const uploadFileHandler = async (accessor: ServicesAccessor) => {
1102
const explorerService = accessor.get(IExplorerService);
1103
const notificationService = accessor.get(INotificationService);
1104
const instantiationService = accessor.get(IInstantiationService);
1105
1106
const context = explorerService.getContext(false);
1107
const element = context.length ? context[0] : explorerService.roots[0];
1108
1109
try {
1110
const files = await triggerUpload();
1111
if (files) {
1112
const browserUpload = instantiationService.createInstance(BrowserFileUpload);
1113
await browserUpload.upload(element, files);
1114
}
1115
} catch (error) {
1116
notificationService.error(error);
1117
1118
throw error;
1119
}
1120
};
1121
1122
CommandsRegistry.registerCommand({
1123
id: UPLOAD_COMMAND_ID,
1124
handler: uploadFileHandler
1125
});
1126
1127
export const pasteFileHandler = async (accessor: ServicesAccessor, fileList?: FileList) => {
1128
const clipboardService = accessor.get(IClipboardService);
1129
const explorerService = accessor.get(IExplorerService);
1130
const fileService = accessor.get(IFileService);
1131
const notificationService = accessor.get(INotificationService);
1132
const editorService = accessor.get(IEditorService);
1133
const configurationService = accessor.get(IConfigurationService);
1134
const uriIdentityService = accessor.get(IUriIdentityService);
1135
const dialogService = accessor.get(IDialogService);
1136
const hostService = accessor.get(IHostService);
1137
1138
const context = explorerService.getContext(false);
1139
const hasNativeFilesToPaste = fileList && fileList.length > 0;
1140
const confirmPasteNative = hasNativeFilesToPaste && configurationService.getValue<boolean>('explorer.confirmPasteNative');
1141
1142
const toPaste = await getFilesToPaste(fileList, clipboardService, hostService);
1143
1144
if (confirmPasteNative && toPaste.files.length >= 1) {
1145
const message = toPaste.files.length > 1 ?
1146
nls.localize('confirmMultiPasteNative', "Are you sure you want to paste the following {0} items?", toPaste.files.length) :
1147
nls.localize('confirmPasteNative', "Are you sure you want to paste '{0}'?", basename(toPaste.type === 'paths' ? toPaste.files[0].fsPath : toPaste.files[0].name));
1148
const detail = toPaste.files.length > 1 ? getFileNamesMessage(toPaste.files.map(item => {
1149
if (URI.isUri(item)) {
1150
return item.fsPath;
1151
}
1152
1153
if (toPaste.type === 'paths') {
1154
const path = getPathForFile(item);
1155
if (path) {
1156
return path;
1157
}
1158
}
1159
1160
return item.name;
1161
})) : undefined;
1162
const confirmation = await dialogService.confirm({
1163
message,
1164
detail,
1165
checkbox: {
1166
label: nls.localize('doNotAskAgain', "Do not ask me again")
1167
},
1168
primaryButton: nls.localize({ key: 'pasteButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Paste")
1169
});
1170
1171
if (!confirmation.confirmed) {
1172
return;
1173
}
1174
1175
// Check for confirmation checkbox
1176
if (confirmation.checkboxChecked === true) {
1177
await configurationService.updateValue('explorer.confirmPasteNative', false);
1178
}
1179
}
1180
const element = context.length ? context[0] : explorerService.roots[0];
1181
const incrementalNaming = configurationService.getValue<IFilesConfiguration>().explorer.incrementalNaming;
1182
1183
const editableItem = explorerService.getEditable();
1184
// If it's an editable item, just do nothing
1185
if (editableItem) {
1186
return;
1187
}
1188
1189
try {
1190
let targets: URI[] = [];
1191
1192
if (toPaste.type === 'paths') { // Pasting from files on disk
1193
1194
// Check if target is ancestor of pasted folder
1195
const sourceTargetPairs = coalesce(await Promise.all(toPaste.files.map(async fileToPaste => {
1196
if (element.resource.toString() !== fileToPaste.toString() && resources.isEqualOrParent(element.resource, fileToPaste)) {
1197
throw new Error(nls.localize('fileIsAncestor', "File to paste is an ancestor of the destination folder"));
1198
}
1199
const fileToPasteStat = await fileService.stat(fileToPaste);
1200
1201
// Find target
1202
let target: ExplorerItem;
1203
if (uriIdentityService.extUri.isEqual(element.resource, fileToPaste)) {
1204
target = element.parent!;
1205
} else {
1206
target = element.isDirectory ? element : element.parent!;
1207
}
1208
1209
const targetFile = await findValidPasteFileTarget(
1210
explorerService,
1211
fileService,
1212
dialogService,
1213
target,
1214
{ resource: fileToPaste, isDirectory: fileToPasteStat.isDirectory, allowOverwrite: pasteShouldMove || incrementalNaming === 'disabled' },
1215
incrementalNaming
1216
);
1217
1218
if (!targetFile) {
1219
return undefined;
1220
}
1221
1222
return { source: fileToPaste, target: targetFile };
1223
})));
1224
1225
if (sourceTargetPairs.length >= 1) {
1226
// Move/Copy File
1227
if (pasteShouldMove) {
1228
const resourceFileEdits = sourceTargetPairs.map(pair => new ResourceFileEdit(pair.source, pair.target, { overwrite: incrementalNaming === 'disabled' }));
1229
const options = {
1230
confirmBeforeUndo: configurationService.getValue<IFilesConfiguration>().explorer.confirmUndo === UndoConfirmLevel.Verbose,
1231
progressLabel: sourceTargetPairs.length > 1 ? nls.localize({ key: 'movingBulkEdit', comment: ['Placeholder will be replaced by the number of files being moved'] }, "Moving {0} files", sourceTargetPairs.length)
1232
: nls.localize({ key: 'movingFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file moved.'] }, "Moving {0}", resources.basenameOrAuthority(sourceTargetPairs[0].target)),
1233
undoLabel: sourceTargetPairs.length > 1 ? nls.localize({ key: 'moveBulkEdit', comment: ['Placeholder will be replaced by the number of files being moved'] }, "Move {0} files", sourceTargetPairs.length)
1234
: nls.localize({ key: 'moveFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file moved.'] }, "Move {0}", resources.basenameOrAuthority(sourceTargetPairs[0].target))
1235
};
1236
await explorerService.applyBulkEdit(resourceFileEdits, options);
1237
} else {
1238
const resourceFileEdits = sourceTargetPairs.map(pair => new ResourceFileEdit(pair.source, pair.target, { copy: true, overwrite: incrementalNaming === 'disabled' }));
1239
await applyCopyResourceEdit(sourceTargetPairs.map(pair => pair.target), resourceFileEdits);
1240
}
1241
}
1242
1243
targets = sourceTargetPairs.map(pair => pair.target);
1244
1245
} else { // Pasting from file data
1246
const targetAndEdits = coalesce(await Promise.all(toPaste.files.map(async file => {
1247
const target = element.isDirectory ? element : element.parent!;
1248
1249
const targetFile = await findValidPasteFileTarget(
1250
explorerService,
1251
fileService,
1252
dialogService,
1253
target,
1254
{ resource: file.name, isDirectory: false, allowOverwrite: pasteShouldMove || incrementalNaming === 'disabled' },
1255
incrementalNaming
1256
);
1257
if (!targetFile) {
1258
return;
1259
}
1260
return {
1261
target: targetFile,
1262
edit: new ResourceFileEdit(undefined, targetFile, {
1263
overwrite: incrementalNaming === 'disabled',
1264
contents: (async () => VSBuffer.wrap(new Uint8Array(await file.arrayBuffer())))(),
1265
})
1266
};
1267
})));
1268
1269
await applyCopyResourceEdit(targetAndEdits.map(pair => pair.target), targetAndEdits.map(pair => pair.edit));
1270
targets = targetAndEdits.map(pair => pair.target);
1271
}
1272
1273
if (targets.length) {
1274
const firstTarget = targets[0];
1275
await explorerService.select(firstTarget);
1276
if (targets.length === 1) {
1277
const item = explorerService.findClosest(firstTarget);
1278
if (item && !item.isDirectory) {
1279
await editorService.openEditor({ resource: item.resource, options: { pinned: true, preserveFocus: true } });
1280
}
1281
}
1282
}
1283
} catch (e) {
1284
onError(notificationService, new Error(nls.localize('fileDeleted', "The file(s) to paste have been deleted or moved since you copied them. {0}", getErrorMessage(e))));
1285
} finally {
1286
if (pasteShouldMove) {
1287
// Cut is done. Make sure to clear cut state.
1288
await explorerService.setToCopy([], false);
1289
pasteShouldMove = false;
1290
}
1291
}
1292
1293
async function applyCopyResourceEdit(targets: readonly URI[], resourceFileEdits: ResourceFileEdit[]) {
1294
const undoLevel = configurationService.getValue<IFilesConfiguration>().explorer.confirmUndo;
1295
const options = {
1296
confirmBeforeUndo: undoLevel === UndoConfirmLevel.Default || undoLevel === UndoConfirmLevel.Verbose,
1297
progressLabel: targets.length > 1 ? nls.localize({ key: 'copyingBulkEdit', comment: ['Placeholder will be replaced by the number of files being copied'] }, "Copying {0} files", targets.length)
1298
: nls.localize({ key: 'copyingFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file copied.'] }, "Copying {0}", resources.basenameOrAuthority(targets[0])),
1299
undoLabel: targets.length > 1 ? nls.localize({ key: 'copyBulkEdit', comment: ['Placeholder will be replaced by the number of files being copied'] }, "Paste {0} files", targets.length)
1300
: nls.localize({ key: 'copyFileBulkEdit', comment: ['Placeholder will be replaced by the name of the file copied.'] }, "Paste {0}", resources.basenameOrAuthority(targets[0]))
1301
};
1302
await explorerService.applyBulkEdit(resourceFileEdits, options);
1303
}
1304
};
1305
1306
type FilesToPaste =
1307
| { type: 'paths'; files: URI[] }
1308
| { type: 'data'; files: File[] };
1309
1310
async function getFilesToPaste(fileList: FileList | undefined, clipboardService: IClipboardService, hostService: IHostService): Promise<FilesToPaste> {
1311
if (fileList && fileList.length > 0) {
1312
// with a `fileList` we support natively pasting file from disk from clipboard
1313
const resources = [...fileList].map(file => getPathForFile(file)).filter(filePath => !!filePath && isAbsolute(filePath)).map((filePath) => URI.file(filePath!));
1314
if (resources.length) {
1315
return { type: 'paths', files: resources, };
1316
}
1317
1318
// Support pasting files that we can't read from disk
1319
return { type: 'data', files: [...fileList].filter(file => !getPathForFile(file)) };
1320
} else {
1321
// otherwise we fallback to reading resources from our clipboard service
1322
return { type: 'paths', files: resources.distinctParents(await clipboardService.readResources(), resource => resource) };
1323
}
1324
}
1325
1326
export const openFilePreserveFocusHandler = async (accessor: ServicesAccessor) => {
1327
const editorService = accessor.get(IEditorService);
1328
const explorerService = accessor.get(IExplorerService);
1329
const stats = explorerService.getContext(true);
1330
1331
await editorService.openEditors(stats.filter(s => !s.isDirectory).map(s => ({
1332
resource: s.resource,
1333
options: { preserveFocus: true }
1334
})));
1335
};
1336
1337
class BaseSetActiveEditorReadonlyInSession extends Action2 {
1338
1339
constructor(
1340
id: string,
1341
title: ILocalizedString,
1342
private readonly newReadonlyState: true | false | 'toggle' | 'reset'
1343
) {
1344
super({
1345
id,
1346
title,
1347
f1: true,
1348
category: Categories.File,
1349
precondition: ActiveEditorCanToggleReadonlyContext
1350
});
1351
}
1352
1353
override async run(accessor: ServicesAccessor): Promise<void> {
1354
const editorService = accessor.get(IEditorService);
1355
const filesConfigurationService = accessor.get(IFilesConfigurationService);
1356
1357
const fileResource = EditorResourceAccessor.getOriginalUri(editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
1358
if (!fileResource) {
1359
return;
1360
}
1361
1362
await filesConfigurationService.updateReadonly(fileResource, this.newReadonlyState);
1363
}
1364
}
1365
1366
export class SetActiveEditorReadonlyInSession extends BaseSetActiveEditorReadonlyInSession {
1367
1368
static readonly ID = 'workbench.action.files.setActiveEditorReadonlyInSession';
1369
static readonly LABEL = nls.localize2('setActiveEditorReadonlyInSession', "Set Active Editor Read-only in Session");
1370
1371
constructor() {
1372
super(
1373
SetActiveEditorReadonlyInSession.ID,
1374
SetActiveEditorReadonlyInSession.LABEL,
1375
true
1376
);
1377
}
1378
}
1379
1380
export class SetActiveEditorWriteableInSession extends BaseSetActiveEditorReadonlyInSession {
1381
1382
static readonly ID = 'workbench.action.files.setActiveEditorWriteableInSession';
1383
static readonly LABEL = nls.localize2('setActiveEditorWriteableInSession', "Set Active Editor Writeable in Session");
1384
1385
constructor() {
1386
super(
1387
SetActiveEditorWriteableInSession.ID,
1388
SetActiveEditorWriteableInSession.LABEL,
1389
false
1390
);
1391
}
1392
}
1393
1394
export class ToggleActiveEditorReadonlyInSession extends BaseSetActiveEditorReadonlyInSession {
1395
1396
static readonly ID = 'workbench.action.files.toggleActiveEditorReadonlyInSession';
1397
static readonly LABEL = nls.localize2('toggleActiveEditorReadonlyInSession', "Toggle Active Editor Read-only in Session");
1398
1399
constructor() {
1400
super(
1401
ToggleActiveEditorReadonlyInSession.ID,
1402
ToggleActiveEditorReadonlyInSession.LABEL,
1403
'toggle'
1404
);
1405
}
1406
}
1407
1408
export class ResetActiveEditorReadonlyInSession extends BaseSetActiveEditorReadonlyInSession {
1409
1410
static readonly ID = 'workbench.action.files.resetActiveEditorReadonlyInSession';
1411
static readonly LABEL = nls.localize2('resetActiveEditorReadonlyInSession', "Reset Active Editor Read-only in Session");
1412
1413
constructor() {
1414
super(
1415
ResetActiveEditorReadonlyInSession.ID,
1416
ResetActiveEditorReadonlyInSession.LABEL,
1417
'reset'
1418
);
1419
}
1420
}
1421
1422