|
| 1 | +import type { QuickPickItem, Uri } from "vscode"; |
| 2 | +import { FileType, window, workspace } from "vscode"; |
| 3 | +import type { DatabaseItem } from "./local-databases"; |
| 4 | +import { |
| 5 | + encodeSourceArchiveUri, |
| 6 | + decodeSourceArchiveUri, |
| 7 | +} from "../common/vscode/archive-filesystem-provider"; |
| 8 | + |
| 9 | +interface SourceArchiveFileQuickPickItem extends QuickPickItem { |
| 10 | + uri: Uri; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Recursively collects all file URIs from a source archive directory. |
| 15 | + */ |
| 16 | +async function collectFiles( |
| 17 | + dirUri: Uri, |
| 18 | + sourceArchiveZipPath: string, |
| 19 | + prefix: string, |
| 20 | +): Promise<SourceArchiveFileQuickPickItem[]> { |
| 21 | + const entries = await workspace.fs.readDirectory(dirUri); |
| 22 | + const items: SourceArchiveFileQuickPickItem[] = []; |
| 23 | + |
| 24 | + for (const [name, type] of entries) { |
| 25 | + const childPath = prefix ? `${prefix}/${name}` : name; |
| 26 | + const childUri = encodeSourceArchiveUri({ |
| 27 | + sourceArchiveZipPath, |
| 28 | + pathWithinSourceArchive: `${decodeSourceArchiveUri(dirUri).pathWithinSourceArchive}/${name}`, |
| 29 | + }); |
| 30 | + |
| 31 | + if (type === FileType.File) { |
| 32 | + items.push({ |
| 33 | + label: name, |
| 34 | + description: prefix, |
| 35 | + uri: childUri, |
| 36 | + }); |
| 37 | + } else if (type === FileType.Directory) { |
| 38 | + const subItems = await collectFiles( |
| 39 | + childUri, |
| 40 | + sourceArchiveZipPath, |
| 41 | + childPath, |
| 42 | + ); |
| 43 | + items.push(...subItems); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return items; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Shows a Quick Pick to search for and open a file from the source archive |
| 52 | + * of the given database. |
| 53 | + */ |
| 54 | +export async function searchSourceArchiveFiles( |
| 55 | + databaseItem: DatabaseItem, |
| 56 | +): Promise<void> { |
| 57 | + let explorerUri: Uri; |
| 58 | + try { |
| 59 | + explorerUri = databaseItem.getSourceArchiveExplorerUri(); |
| 60 | + } catch (e) { |
| 61 | + void window.showErrorMessage(e instanceof Error ? e.message : String(e)); |
| 62 | + return; |
| 63 | + } |
| 64 | + const sourceArchiveZipPath = |
| 65 | + decodeSourceArchiveUri(explorerUri).sourceArchiveZipPath; |
| 66 | + |
| 67 | + const quickPick = window.createQuickPick<SourceArchiveFileQuickPickItem>(); |
| 68 | + quickPick.placeholder = "Go to File in Selected Database..."; |
| 69 | + quickPick.matchOnDescription = true; |
| 70 | + quickPick.busy = true; |
| 71 | + quickPick.show(); |
| 72 | + |
| 73 | + try { |
| 74 | + const items = await collectFiles(explorerUri, sourceArchiveZipPath, ""); |
| 75 | + // Sort items by file name, then by path |
| 76 | + items.sort((a, b) => { |
| 77 | + const nameCmp = a.label.localeCompare(b.label); |
| 78 | + if (nameCmp !== 0) { |
| 79 | + return nameCmp; |
| 80 | + } |
| 81 | + return (a.description ?? "").localeCompare(b.description ?? ""); |
| 82 | + }); |
| 83 | + quickPick.items = items; |
| 84 | + quickPick.busy = false; |
| 85 | + } catch (e) { |
| 86 | + quickPick.dispose(); |
| 87 | + void window.showErrorMessage( |
| 88 | + `Failed to read source archive: ${e instanceof Error ? e.message : String(e)}`, |
| 89 | + ); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + return new Promise<void>((resolve) => { |
| 94 | + quickPick.onDidAccept(async () => { |
| 95 | + const selected = quickPick.selectedItems[0]; |
| 96 | + quickPick.dispose(); |
| 97 | + if (selected) { |
| 98 | + const doc = await workspace.openTextDocument(selected.uri); |
| 99 | + await window.showTextDocument(doc); |
| 100 | + } |
| 101 | + resolve(); |
| 102 | + }); |
| 103 | + |
| 104 | + quickPick.onDidHide(() => { |
| 105 | + quickPick.dispose(); |
| 106 | + resolve(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
0 commit comments