Skip to content

Commit 5e63a42

Browse files
authored
fix: Add overwrite confirmation (#636)
Signed-off-by: sheche <[email protected]>
1 parent 16d35e8 commit 5e63a42

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/views/DragAndDropController.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as path from "path";
5+
import * as fse from "fs-extra";
56
import { commands, DataTransfer, DataTransferItem, TreeDragAndDropController, Uri, window, workspace, WorkspaceEdit } from "vscode";
67
import { Commands } from "../commands";
78
import { Explorer } from "../constants";
@@ -203,11 +204,32 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
203204
"Move",
204205
);
205206

206-
if (choice === "Move") {
207+
if (choice === "Move" && await this.confirmOverwrite(newPath)) {
207208
const edit = new WorkspaceEdit();
208-
edit.renameFile(sourceUri, Uri.file(newPath));
209+
edit.renameFile(sourceUri, Uri.file(newPath), { overwrite: true });
209210
await workspace.applyEdit(edit);
210211
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
211212
}
212213
}
214+
215+
/**
216+
* Confirm the overwrite action when the target file already exists.
217+
* @param file the path of the target file.
218+
*/
219+
private async confirmOverwrite(file: string): Promise<boolean> {
220+
if (await fse.pathExists(file)) {
221+
const name = path.basename(file);
222+
const ans = await window.showWarningMessage(
223+
`A file or folder with the name '${name}' already exists in the destination folder. Do you want to replace it?`,
224+
{
225+
modal: true,
226+
detail: "This action is irreversible!",
227+
},
228+
"Replace",
229+
);
230+
return ans === "Replace";
231+
}
232+
233+
return true;
234+
}
213235
}

0 commit comments

Comments
 (0)