|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
4 | 4 | import * as path from "path";
|
| 5 | +import * as fse from "fs-extra"; |
5 | 6 | import { commands, DataTransfer, DataTransferItem, TreeDragAndDropController, Uri, window, workspace, WorkspaceEdit } from "vscode";
|
6 | 7 | import { Commands } from "../commands";
|
7 | 8 | import { Explorer } from "../constants";
|
@@ -203,11 +204,32 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
|
203 | 204 | "Move",
|
204 | 205 | );
|
205 | 206 |
|
206 |
| - if (choice === "Move") { |
| 207 | + if (choice === "Move" && await this.confirmOverwrite(newPath)) { |
207 | 208 | const edit = new WorkspaceEdit();
|
208 |
| - edit.renameFile(sourceUri, Uri.file(newPath)); |
| 209 | + edit.renameFile(sourceUri, Uri.file(newPath), { overwrite: true }); |
209 | 210 | await workspace.applyEdit(edit);
|
210 | 211 | commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
|
211 | 212 | }
|
212 | 213 | }
|
| 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 | + } |
213 | 235 | }
|
0 commit comments