-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathDragAndDropController.ts
415 lines (376 loc) · 15.3 KB
/
DragAndDropController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import * as fse from "fs-extra";
import { DataTransfer, DataTransferItem, TreeDragAndDropController, Uri, window, workspace, WorkspaceEdit } from "vscode";
import { Explorer } from "../constants";
import { ContainerNode, ContainerType } from "./containerNode";
import { DataNode } from "./dataNode";
import { ExplorerNode } from "./explorerNode";
import { FileNode } from "./fileNode";
import { FolderNode } from "./folderNode";
import { explorerNodeCache } from "./nodeCache/explorerNodeCache";
import { PackageNode } from "./packageNode";
import { PackageRootNode } from "./packageRootNode";
import { PrimaryTypeNode } from "./PrimaryTypeNode";
import { ProjectNode } from "./projectNode";
import { WorkspaceNode } from "./workspaceNode";
import { addLibraryGlobs } from "../controllers/libraryController";
import { sendError, sendInfo } from "vscode-extension-telemetry-wrapper";
import { DocumentSymbolNode } from "./documentSymbolNode";
export class DragAndDropController implements TreeDragAndDropController<ExplorerNode> {
dropMimeTypes: string[] = [
Explorer.Mime.JavaProjectExplorer,
Explorer.Mime.TextUriList,
];
dragMimeTypes: string[] = [
Explorer.Mime.TextUriList,
];
public handleDrag(source: ExplorerNode[], treeDataTransfer: DataTransfer): void {
// select many is not supported yet
const dragItem = source[0];
this.addDragToEditorDataTransfer(dragItem, treeDataTransfer);
this.addInternalDragDataTransfer(dragItem, treeDataTransfer);
sendInfo("", {
dndType: "drag",
dragFrom: dragItem.computeContextValue() || "unknown",
});
}
public async handleDrop(target: ExplorerNode | undefined, dataTransfer: DataTransfer): Promise<void> {
const data = dataTransfer.get(Explorer.Mime.JavaProjectExplorer);
if (data) {
await this.dropFromJavaProjectExplorer(target, data.value);
return;
}
const uris: string | undefined = await dataTransfer.get(Explorer.Mime.TextUriList)?.asString();
if (!uris) {
return;
}
const uriList: string[] = uris.split(/\r?\n/g).map(u => {
try {
const uri = Uri.parse(u, true /* strict */);
if (uri.scheme !== "file") {
return undefined;
}
// Ideally, the file dragged from file explorer should not contain fragment
// in its uri. If it does, then the uri should be generated from dragging document
// symbol node from the Java Project explorer, and we should ignore it.
if (uri.fragment) {
return undefined;
}
return u;
} catch (e) {
sendError(e);
return undefined;
}
}).filter(Boolean) as string[];
if (uriList.length) {
await this.dropFromFileExplorer(target, uriList);
return;
}
}
/**
* Add data transfer that is used when node is dropped to the editor.
* @param node node being dragged.
* @param treeDataTransfer A map containing a mapping of the mime type of the corresponding transferred data.
*/
private addDragToEditorDataTransfer(node: ExplorerNode, treeDataTransfer: DataTransfer) {
if ((node instanceof PrimaryTypeNode || node instanceof FileNode) && node.uri) {
treeDataTransfer.set(Explorer.Mime.TextUriList, new DataTransferItem(node.uri));
} else if ((node instanceof DocumentSymbolNode)) {
const parent = (node.getParent() as PrimaryTypeNode);
if (parent.uri) {
const range = (node as DocumentSymbolNode).range;
const fragment = `#L${range.start.line + 1},${range.start.character + 1}`;
const uri = parent.uri + fragment;
treeDataTransfer.set(Explorer.Mime.TextUriList, new DataTransferItem(uri));
}
}
}
/**
* Add data transfer that is used when node is dropped into other Java Project Explorer node.
* @param node node being dragged.
* @param treeDataTransfer A map containing a mapping of the mime type of the corresponding transferred data.
*/
private addInternalDragDataTransfer(node: ExplorerNode, treeDataTransfer: DataTransfer): void {
// draggable node must have uri
if (!(node instanceof DataNode) || !node.uri) {
return;
}
// whether the node can be dropped will be check in handleDrop(...)
treeDataTransfer.set(Explorer.Mime.JavaProjectExplorer, new DataTransferItem(node.uri));
}
/**
* Handle the DnD event which comes from Java Project explorer itself.
* @param target the drop node.
* @param uri uri in the data transfer.
*/
private async dropFromJavaProjectExplorer(target: ExplorerNode | undefined, uri: string): Promise<void> {
const source: DataNode | undefined = explorerNodeCache.getDataNode(Uri.parse(uri));
if (!this.isDraggableNode(source)) {
sendInfo("", {
dndType: "drop",
dragFrom: source?.computeContextValue() || "unknown",
dropTo: target?.computeContextValue() || "unknown",
draggable: "false",
});
return;
}
if (!this.isDroppableNode(target)) {
sendInfo("", {
dndType: "drop",
dragFrom: source?.computeContextValue() || "unknown",
dropTo: target?.computeContextValue() || "unknown",
draggable: "true",
droppable: "false",
});
return;
}
if (this.isTheSameOrParent(target!, source!)) {
return;
}
if (target instanceof ContainerNode) {
if (target.getContainerType() !== ContainerType.ReferencedLibrary
|| !(target.getParent() as ProjectNode).isUnmanagedFolder()) {
sendInfo("", {
dndType: "drop",
dragFrom: source?.computeContextValue() || "unknown",
dropTo: "Referenced Libraries",
draggable: "true",
droppable: "false",
});
return;
}
this.addReferencedLibraries([source?.uri!]);
sendInfo("", {
dndType: "drop",
dragFrom: source?.computeContextValue() || "unknown",
dropTo: "Referenced Libraries",
draggable: "true",
droppable: "true",
});
} else if (target instanceof PackageRootNode || target instanceof PackageNode
|| target instanceof FolderNode) {
await this.move(Uri.parse(source!.uri!), Uri.parse(target.uri!));
sendInfo("", {
dndType: "drop",
dragFrom: source?.computeContextValue() || "unknown",
dropTo: target?.computeContextValue() || "unknown",
draggable: "true",
droppable: "true",
});
}
}
/**
* Handle the DnD event which comes from VS Code's file explorer or system file explorer.
* @param target the drop node.
* @param uris uris of the dragged files.
*/
private async dropFromFileExplorer(target: ExplorerNode | undefined, uris: string[]): Promise<void> {
if (!this.isDroppableNode(target)) {
sendInfo("", {
dndType: "drop",
dragFrom: "File Explorer",
dropTo: target?.computeContextValue() || "unknown",
draggable: "true",
droppable: "false",
});
return;
}
if (target instanceof ContainerNode) {
if (target.getContainerType() !== ContainerType.ReferencedLibrary
|| !(target.getParent() as ProjectNode).isUnmanagedFolder()) {
sendInfo("", {
dndType: "drop",
dragFrom: "File Explorer",
dropTo: "Referenced Libraries",
draggable: "true",
droppable: "false",
});
return;
}
this.addReferencedLibraries(uris);
sendInfo("", {
dndType: "drop",
dragFrom: "File Explorer",
dropTo: "Referenced Libraries",
draggable: "true",
droppable: "true",
});
} else if (target instanceof PackageRootNode || target instanceof PackageNode
|| target instanceof FolderNode) {
for (const uri of uris) {
await this.copy(Uri.parse(uri), Uri.parse(target.uri!));
}
sendInfo("", {
dndType: "drop",
dragFrom: "File Explorer",
dropTo: target?.computeContextValue() || "unknown",
draggable: "true",
droppable: "true",
});
}
}
/**
* Check whether the dragged node is draggable.
* @param node the dragged node.
*/
private isDraggableNode(node: DataNode | undefined): boolean {
if (!node?.uri) {
return false;
}
if (node instanceof WorkspaceNode || node instanceof ProjectNode
|| node instanceof PackageRootNode || node instanceof ContainerNode
|| node instanceof DocumentSymbolNode) {
return false;
}
return this.isUnderSourceRoot(node);
}
/**
* Check whether the node is under source root.
*
* Note: There is one exception: The primary type directly under an unmanaged folder project,
* in that case, `true` is returned.
* @param node DataNode
*/
private isUnderSourceRoot(node: DataNode): boolean {
let parent = node.getParent();
while (parent) {
if (parent instanceof ContainerNode) {
return false;
}
if (parent instanceof PackageRootNode) {
return parent.isSourceRoot();
}
parent = parent.getParent();
}
return true;
}
/**
* Check whether the node is able to be dropped.
*/
private isDroppableNode(node: ExplorerNode | undefined): boolean {
// drop to root is not supported yet
if (!node) {
return false;
}
if (node instanceof DataNode && !(node instanceof ContainerNode) && !node.uri) {
return false;
}
if (node instanceof WorkspaceNode || node instanceof ProjectNode
|| node instanceof DocumentSymbolNode) {
return false;
}
let parent: ExplorerNode | undefined = node;
while (parent) {
if (parent instanceof ProjectNode) {
return false;
} else if (parent instanceof PackageRootNode) {
return parent.isSourceRoot();
} else if (parent instanceof PackageNode) {
return parent.isSourcePackage();
} else if (parent instanceof ContainerNode) {
if (parent.getContainerType() === ContainerType.ReferencedLibrary) {
return (parent.getParent() as ProjectNode).isUnmanagedFolder();
}
return false;
}
parent = parent.getParent();
}
return false;
}
/**
* Check whether the target node is the same or parent of the source node.
* If the target node's file path is parent of the source node's, `true`
* well be returned as well.
*/
private isTheSameOrParent(target: ExplorerNode, source: DataNode): boolean {
if (target.isItselfOrAncestorOf(source, 1 /*levelToCheck*/)) {
return true;
}
if ((target instanceof DataNode) && target.uri && source.uri) {
const targetPath = Uri.parse(target.uri).fsPath;
const sourcePath = Uri.parse(source.uri).fsPath;
return path.relative(sourcePath, targetPath) === "..";
}
return false;
}
/**
* Trigger a workspace edit that move the source node into the target node.
*/
private async move(sourceUri: Uri, targetUri: Uri): Promise<void> {
if (sourceUri === targetUri) {
return;
}
const newPath = path.join(targetUri.fsPath, path.basename(sourceUri.fsPath));
const choice = await window.showInformationMessage(
`Are you sure you want to move '${path.basename(sourceUri.fsPath)}' into '${path.basename(targetUri.fsPath)}'?`,
{ modal: true },
"Move",
);
if (choice === "Move" && await this.confirmOverwrite(newPath)) {
const edit = new WorkspaceEdit();
edit.renameFile(sourceUri, Uri.file(newPath), { overwrite: true });
await workspace.applyEdit(edit);
}
}
/**
* Copy the file from source uri to the target uri.
*/
private async copy(sourceUri: Uri, targetUri: Uri): Promise<void> {
if (sourceUri === targetUri) {
return;
}
const newPath = path.join(targetUri.fsPath, path.basename(sourceUri.fsPath));
if (await this.confirmOverwrite(newPath)) {
await workspace.fs.copy(sourceUri, Uri.file(newPath), { overwrite: true });
}
}
/**
* Confirm the overwrite action when the target file already exists.
* @param file the path of the target file.
*/
private async confirmOverwrite(file: string): Promise<boolean> {
if (await fse.pathExists(file)) {
const name = path.basename(file);
const ans = await window.showWarningMessage(
`A file or folder with the name '${name}' already exists in the destination folder. Do you want to replace it?`,
{
modal: true,
detail: "This action is irreversible!",
},
"Replace",
);
return ans === "Replace";
}
return true;
}
/**
* Parse the input uri strings to pattern strings and set them to
* the setting: 'java.project.referencedLibraries'.
* @param uriStrings uri strings
*/
private async addReferencedLibraries(uriStrings: string[]): Promise<void> {
const pattern = (await Promise.all(uriStrings.map(async uriString => {
try {
const uri = Uri.parse(uriString, true /* strict */);
if (uri.scheme !== "file") {
return undefined;
}
const isDirectory = (await fse.stat(uri.fsPath)).isDirectory();
// only jars and folders can be dropped into referenced libraries.
if (!isDirectory && path.extname(uri.fsPath) !== ".jar") {
return undefined;
}
const uriPath = workspace.asRelativePath(uri, false);
return isDirectory ? uriPath + "/**/*.jar" : uriPath;
} catch (e) {
sendError(e);
return undefined;
}
}))).filter(Boolean);
if (pattern) {
addLibraryGlobs(pattern as string[]);
}
}
}