|
| 1 | +import path from "path"; |
1 | 2 | import vscode, { commands, l10n } from "vscode";
|
2 | 3 | import IBMi from "../../api/IBMi";
|
3 | 4 | import { instance } from "../../instantiate";
|
4 |
| -import { ConnectionConfig, IBMiObject, LIBRARY_LIST_MIMETYPE, OBJECT_BROWSER_DRAG_MIMETYPE, WithLibrary } from "../../typings"; |
| 5 | +import { ConnectionConfig, IBMiObject, LIBRARY_LIST_MIMETYPE, URI_LIST_MIMETYPE, URI_LIST_SEPARATOR, WithLibrary } from "../../typings"; |
5 | 6 | import { VscodeTools } from "../Tools";
|
6 | 7 |
|
7 | 8 | export function initializeLibraryListView(context: vscode.ExtensionContext) {
|
@@ -86,14 +87,14 @@ export function initializeLibraryListView(context: vscode.ExtensionContext) {
|
86 | 87 | }
|
87 | 88 | }),
|
88 | 89 |
|
89 |
| - vscode.commands.registerCommand(`code-for-ibmi.changeUserLibraryList`, async () => { |
| 90 | + vscode.commands.registerCommand(`code-for-ibmi.changeUserLibraryList`, async (libraries?: string[]) => { |
90 | 91 | const connection = instance.getConnection();
|
91 | 92 | if (connection) {
|
92 | 93 | const content = connection.getContent();
|
93 | 94 | const config = connection.getConfig();
|
94 | 95 | const libraryList = config.libraryList;
|
95 | 96 |
|
96 |
| - const newLibraryListStr = await vscode.window.showInputBox({ |
| 97 | + const newLibraryListStr = libraries?.join(",") || await vscode.window.showInputBox({ |
97 | 98 | prompt: l10n.t(`Changing library list (can use "*reset")`),
|
98 | 99 | value: libraryList.map(lib => connection.upperCaseName(lib)).join(`, `)
|
99 | 100 | });
|
@@ -270,29 +271,58 @@ export function initializeLibraryListView(context: vscode.ExtensionContext) {
|
270 | 271 |
|
271 | 272 | class LibraryListDragAndDrop implements vscode.TreeDragAndDropController<LibraryListNode> {
|
272 | 273 | readonly dragMimeTypes = [];
|
273 |
| - readonly dropMimeTypes = [OBJECT_BROWSER_DRAG_MIMETYPE]; |
| 274 | + readonly dropMimeTypes = [URI_LIST_MIMETYPE]; |
274 | 275 |
|
275 |
| - handleDrag(source: readonly LibraryListNode[], dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Thenable<void> | void { |
| 276 | + handleDrag(source: readonly LibraryListNode[], dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken) { |
276 | 277 | dataTransfer.set(LIBRARY_LIST_MIMETYPE, new vscode.DataTransferItem(source));
|
277 | 278 | }
|
278 | 279 |
|
279 |
| - handleDrop(target: LibraryListNode | undefined, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Thenable<void> | void { |
280 |
| - const libraryItem = dataTransfer.get(LIBRARY_LIST_MIMETYPE)?.value[0] as LibraryListNode; |
281 |
| - if (libraryItem && libraryItem !== target) { |
282 |
| - const currentLibrary = libraryItem.contextValue === 'currentLibrary'; |
| 280 | + handleDrop(target: LibraryListNode | undefined, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken) { |
| 281 | + const libraries = this.getLibraries(dataTransfer)?.map(library => library.toUpperCase()); |
| 282 | + const config = instance.getConnection()?.getConfig(); |
| 283 | + if (config && libraries?.length) { |
| 284 | + if (target?.contextValue === 'currentLibrary') { |
| 285 | + //Dropped on current library: change current library |
| 286 | + vscode.commands.executeCommand(`code-for-ibmi.setCurrentLibrary`, { library: libraries[0] } as WithLibrary); |
| 287 | + } |
| 288 | + else { |
| 289 | + const libraryList = config.libraryList; |
| 290 | + |
| 291 | + libraries.forEach(library => { |
| 292 | + const index = libraryList.findIndex(lib => lib === library); |
| 293 | + if (index > -1) { |
| 294 | + libraryList.splice(index, 1); |
| 295 | + } |
| 296 | + }); |
283 | 297 |
|
284 |
| - if (target) { |
285 |
| - if (target?.contextValue === 'currentLibrary') { |
286 |
| - //Dropped on current library: become current library |
287 |
| - vscode.commands.executeCommand(`code-for-ibmi.setCurrentLibrary`, libraryItem); |
| 298 | + if (target) { |
| 299 | + //Dropped on a library: push it down and move to its position |
| 300 | + const index = libraryList.findIndex(lib => lib === target.library); |
| 301 | + const moved = libraryList.splice(index, libraryList.length - index, ...libraries); |
| 302 | + libraryList.push(...moved); |
288 | 303 | }
|
289 | 304 | else {
|
290 |
| - //Dropped on a library: push it down and move to its position |
| 305 | + //Dropped at the bottom of the list, after the last item: move to the last position |
| 306 | + libraryList.push(...libraries); |
291 | 307 | }
|
| 308 | + vscode.commands.executeCommand(`code-for-ibmi.changeUserLibraryList`, libraryList); |
292 | 309 | }
|
293 |
| - else { |
294 |
| - //Dropped at the bottom after the last item: move to the last position |
295 |
| - } |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + getLibraries(dataTransfer: vscode.DataTransfer) { |
| 314 | + const libraryListData = dataTransfer.get(LIBRARY_LIST_MIMETYPE); |
| 315 | + const urisData = dataTransfer.get(URI_LIST_MIMETYPE); |
| 316 | + if (libraryListData) { |
| 317 | + return (libraryListData.value as LibraryListNode[]).map(node => node.library); |
| 318 | + } |
| 319 | + else if (urisData && urisData.value) { |
| 320 | + return String(urisData.value).split(URI_LIST_SEPARATOR) |
| 321 | + .map(uri => vscode.Uri.parse(uri)) |
| 322 | + .filter(uri => uri.scheme === "object") |
| 323 | + .map(uri => path.parse(uri.path)) |
| 324 | + .filter(path => path.ext?.toUpperCase() === ".LIB") |
| 325 | + .map(path => path.name); |
296 | 326 | }
|
297 | 327 | }
|
298 | 328 | }
|
|
0 commit comments