Fix some cases of incremental naming disabled (#165253)

This commit is contained in:
Logan Ramos
2022-11-02 12:05:41 -04:00
committed by GitHub
parent db1c4d4f85
commit 6aaf830b9c
2 changed files with 45 additions and 11 deletions
@@ -301,10 +301,26 @@ function containsBothDirectoryAndFile(distinctElements: ExplorerItem[]): boolean
}
export function findValidPasteFileTarget(explorerService: IExplorerService, targetFolder: ExplorerItem, fileToPaste: { resource: URI; isDirectory?: boolean; allowOverwrite: boolean }, incrementalNaming: 'simple' | 'smart' | 'disabled'): URI {
let name = resources.basenameOrAuthority(fileToPaste.resource);
export async function findValidPasteFileTarget(
explorerService: IExplorerService,
fileService: IFileService,
dialogService: IDialogService,
targetFolder: ExplorerItem,
fileToPaste: { resource: URI; isDirectory?: boolean; allowOverwrite: boolean },
incrementalNaming: 'simple' | 'smart' | 'disabled'
): Promise<URI | undefined> {
let name = resources.basenameOrAuthority(fileToPaste.resource);
let candidate = resources.joinPath(targetFolder.resource, name);
// In the disabled case we must ask if it's ok to overwrite the file if it exists
if (incrementalNaming === 'disabled') {
const canOverwrite = await askForOverwrite(fileService, dialogService, candidate);
if (!canOverwrite) {
return;
}
}
while (true && !fileToPaste.allowOverwrite) {
if (!explorerService.findClosest(candidate)) {
break;
@@ -1064,13 +1080,17 @@ export const pasteFileHandler = async (accessor: ServicesAccessor) => {
target = element.isDirectory ? element : element.parent!;
}
const targetFile = findValidPasteFileTarget(explorerService, target, { resource: fileToPaste, isDirectory: fileToPasteStat.isDirectory, allowOverwrite: pasteShouldMove || incrementalNaming === 'disabled' }, incrementalNaming);
const targetFile = await findValidPasteFileTarget(
explorerService,
fileService,
dialogService,
target,
{ resource: fileToPaste, isDirectory: fileToPasteStat.isDirectory, allowOverwrite: pasteShouldMove || incrementalNaming === 'disabled' },
incrementalNaming
);
if (incrementalNaming === 'disabled') {
const canOverwrite = await askForOverwrite(fileService, dialogService, targetFile);
if (!canOverwrite) {
return;
}
if (!targetFile) {
return undefined;
}
return { source: fileToPaste, target: targetFile };
@@ -1079,7 +1099,7 @@ export const pasteFileHandler = async (accessor: ServicesAccessor) => {
if (sourceTargetPairs.length >= 1) {
// Move/Copy File
if (pasteShouldMove) {
const resourceFileEdits = sourceTargetPairs.map(pair => new ResourceFileEdit(pair.source, pair.target));
const resourceFileEdits = sourceTargetPairs.map(pair => new ResourceFileEdit(pair.source, pair.target, { overwrite: incrementalNaming === 'disabled' }));
const options = {
confirmBeforeUndo: configurationService.getValue<IFilesConfiguration>().explorer.confirmUndo === UndoConfirmLevel.Verbose,
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)
@@ -1276,8 +1276,22 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {
// Reuse duplicate action when user copies
const explorerConfig = this.configurationService.getValue<IFilesConfiguration>().explorer;
const resourceFileEdits = sources.map(({ resource, isDirectory }) =>
(new ResourceFileEdit(resource, findValidPasteFileTarget(this.explorerService, target, { resource, isDirectory, allowOverwrite: false }, explorerConfig.incrementalNaming), { copy: true })));
const resourceFileEdits: ResourceFileEdit[] = [];
for (const { resource, isDirectory } of sources) {
const allowOverwrite = explorerConfig.incrementalNaming === 'disabled';
const newResource = await findValidPasteFileTarget(this.explorerService,
this.fileService,
this.dialogService,
target,
{ resource, isDirectory, allowOverwrite },
explorerConfig.incrementalNaming
);
if (!newResource) {
continue;
}
const resourceEdit = new ResourceFileEdit(resource, newResource, { copy: true, overwrite: allowOverwrite });
resourceFileEdits.push(resourceEdit);
}
const labelSufix = getFileOrFolderLabelSufix(sources);
await this.explorerService.applyBulkEdit(resourceFileEdits, {
confirmBeforeUndo: explorerConfig.confirmUndo === UndoConfirmLevel.Default || explorerConfig.confirmUndo === UndoConfirmLevel.Verbose,