Skip to content

Commit 4e6e6d3

Browse files
authored
feat: Support copy files from file explorer (#638)
Signed-off-by: sheche <[email protected]>
1 parent eed2e9f commit 4e6e6d3

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/views/DragAndDropController.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
import * as path from "path";
55
import * as fse from "fs-extra";
6-
import { commands, DataTransfer, DataTransferItem, TreeDragAndDropController, Uri, window, workspace, WorkspaceEdit } from "vscode";
7-
import { Commands } from "../commands";
6+
import { DataTransfer, DataTransferItem, TreeDragAndDropController, Uri, window, workspace, WorkspaceEdit } from "vscode";
87
import { Explorer } from "../constants";
98
import { BaseSymbolNode } from "./baseSymbolNode";
109
import { ContainerNode, ContainerType } from "./containerNode";
@@ -136,7 +135,7 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
136135
this.addReferencedLibraries([source?.uri!]);
137136
} else if (target instanceof PackageRootNode || target instanceof PackageNode
138137
|| target instanceof FolderNode) {
139-
await this.move(source!, target);
138+
await this.move(Uri.parse(source!.uri!), Uri.parse(target.uri!));
140139
}
141140
}
142141

@@ -159,7 +158,9 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
159158
this.addReferencedLibraries(uris);
160159
} else if (target instanceof PackageRootNode || target instanceof PackageNode
161160
|| target instanceof FolderNode) {
162-
// TODO: copy the resources to other nodes
161+
for (const uri of uris) {
162+
await this.copy(Uri.parse(uri), Uri.parse(target.uri!));
163+
}
163164
}
164165
}
165166

@@ -240,9 +241,7 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
240241
/**
241242
* Trigger a workspace edit that move the source node into the target node.
242243
*/
243-
private async move(source: DataNode, target: DataNode): Promise<void> {
244-
const sourceUri = Uri.parse(source.uri!);
245-
const targetUri = Uri.parse(target.uri!);
244+
private async move(sourceUri: Uri, targetUri: Uri): Promise<void> {
246245
if (sourceUri === targetUri) {
247246
return;
248247
}
@@ -258,7 +257,20 @@ export class DragAndDropController implements TreeDragAndDropController<Explorer
258257
const edit = new WorkspaceEdit();
259258
edit.renameFile(sourceUri, Uri.file(newPath), { overwrite: true });
260259
await workspace.applyEdit(edit);
261-
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
260+
}
261+
}
262+
263+
/**
264+
* Copy the file from source uri to the target uri.
265+
*/
266+
private async copy(sourceUri: Uri, targetUri: Uri): Promise<void> {
267+
if (sourceUri === targetUri) {
268+
return;
269+
}
270+
271+
const newPath = path.join(targetUri.fsPath, path.basename(sourceUri.fsPath));
272+
if (await this.confirmOverwrite(newPath)) {
273+
await workspace.fs.copy(sourceUri, Uri.file(newPath), { overwrite: true });
262274
}
263275
}
264276

0 commit comments

Comments
 (0)