|
| 1 | +import { glob } from "glob"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import { deleteFile } from "../../util/fileSystem"; |
| 5 | +import { getActiveEditor } from "../../util/getActiveEditor"; |
| 6 | +import { getFullCommand } from "../../util/getFullCommand"; |
| 7 | + |
| 8 | +interface Link { |
| 9 | + range: vscode.Range; |
| 10 | + uri: vscode.Uri; |
| 11 | + selected: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +const divider = "----------------------------------------"; |
| 15 | +const languageId = "search-results"; |
| 16 | +const openLink = "[Open files]"; |
| 17 | +const deleteLink = "[Delete files]"; |
| 18 | + |
| 19 | +export async function searchFiles(query?: string) { |
| 20 | + const lines: string[] = []; |
| 21 | + |
| 22 | + if (!query) { |
| 23 | + query = await getQuery(); |
| 24 | + if (!query) { |
| 25 | + return; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + for (const ws of vscode.workspace.workspaceFolders ?? []) { |
| 30 | + if (lines.length > 0) { |
| 31 | + lines.push(divider, ""); |
| 32 | + } |
| 33 | + lines.push(ws.name, ""); |
| 34 | + |
| 35 | + const files = await glob(`**/*${query}*`, { |
| 36 | + cwd: ws.uri.fsPath, |
| 37 | + dot: true, |
| 38 | + posix: true, |
| 39 | + }); |
| 40 | + |
| 41 | + for (const file of files.sort()) { |
| 42 | + lines.push(` ${file}`); |
| 43 | + } |
| 44 | + |
| 45 | + lines.push(""); |
| 46 | + } |
| 47 | + |
| 48 | + lines.push( |
| 49 | + divider, |
| 50 | + "", |
| 51 | + openLink, |
| 52 | + "", |
| 53 | + deleteLink, |
| 54 | + "", |
| 55 | + "Select a file by adding a '*' or '-' prefix", |
| 56 | + "", |
| 57 | + ); |
| 58 | + |
| 59 | + const document = await vscode.workspace.openTextDocument({ |
| 60 | + content: lines.join("\n"), |
| 61 | + language: languageId, |
| 62 | + }); |
| 63 | + |
| 64 | + await vscode.window.showTextDocument(document); |
| 65 | +} |
| 66 | + |
| 67 | +export function searchFilesOpenSelected() { |
| 68 | + return Promise.all( |
| 69 | + getSelectedLinks().map((link) => |
| 70 | + vscode.window.showTextDocument(link.uri, { preview: false }), |
| 71 | + ), |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +export async function searchFilesDeleteSelected() { |
| 76 | + const selectedLinks = getSelectedLinks(); |
| 77 | + |
| 78 | + const remove = |
| 79 | + selectedLinks.length > 0 && |
| 80 | + (await vscode.window.showInformationMessage( |
| 81 | + `Are you sure you want to delete ${selectedLinks.length} files?`, |
| 82 | + { modal: true }, |
| 83 | + "Move to Recycle Bin", |
| 84 | + )); |
| 85 | + |
| 86 | + if (remove) { |
| 87 | + await Promise.all(selectedLinks.map((link) => deleteFile(link.uri))); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function registerSearchResults(): vscode.Disposable { |
| 92 | + return vscode.languages.registerDocumentLinkProvider( |
| 93 | + { language: languageId }, |
| 94 | + { |
| 95 | + provideDocumentLinks(document: vscode.TextDocument): vscode.DocumentLink[] { |
| 96 | + return parseDocument(document).map( |
| 97 | + (link) => new vscode.DocumentLink(link.range, link.uri), |
| 98 | + ); |
| 99 | + }, |
| 100 | + }, |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +function getSelectedLinks() { |
| 105 | + return parseDocument(getActiveEditor().document).filter((link) => link.selected); |
| 106 | +} |
| 107 | + |
| 108 | +function parseDocument(document: vscode.TextDocument): Link[] { |
| 109 | + if (document.languageId !== languageId) { |
| 110 | + console.error("Active document is not a search result"); |
| 111 | + return []; |
| 112 | + } |
| 113 | + |
| 114 | + const links: Link[] = []; |
| 115 | + const wsTexts = document.getText().split(divider + "\n"); |
| 116 | + let lineNumber = 0; |
| 117 | + let hasSelectedLink = false; |
| 118 | + |
| 119 | + wsTexts.forEach((wsText, index) => { |
| 120 | + const lines = wsText.split("\n"); |
| 121 | + const wsName = lines[0]; |
| 122 | + const wsPath = vscode.workspace.workspaceFolders?.find((ws) => ws.name === wsName)?.uri |
| 123 | + .fsPath; |
| 124 | + |
| 125 | + if (index === wsTexts.length - 1) { |
| 126 | + if (!hasSelectedLink) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + for (const lineText of lines) { |
| 131 | + const range = new vscode.Range(lineNumber, 0, lineNumber, 0 + lineText.length); |
| 132 | + |
| 133 | + lineNumber++; |
| 134 | + |
| 135 | + if (lineText.trim() === "") { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + const command = (() => { |
| 140 | + if (lineText === openLink) { |
| 141 | + return "searchFilesOpenSelected"; |
| 142 | + } |
| 143 | + if (lineText === deleteLink) { |
| 144 | + return "searchFilesDeleteSelected"; |
| 145 | + } |
| 146 | + return null; |
| 147 | + })(); |
| 148 | + |
| 149 | + if (command == null) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + const uri = vscode.Uri.parse(`command:${getFullCommand(command)}`); |
| 154 | + |
| 155 | + links.push({ range, uri, selected: false }); |
| 156 | + } |
| 157 | + |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + if (wsPath == null || lines.length < 2) { |
| 162 | + lineNumber += lines.length + 1; |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + lineNumber++; |
| 167 | + |
| 168 | + for (let i = 1; i < lines.length; i++) { |
| 169 | + const lineText = lines[i]; |
| 170 | + const match = lineText.match(/^\s*([-*]\s+)?/); |
| 171 | + const offset = match?.[0]?.length ?? 0; |
| 172 | + const selected = match?.[1] != null; |
| 173 | + const relativePath = lineText.slice(offset).trimEnd(); |
| 174 | + |
| 175 | + const range = new vscode.Range( |
| 176 | + lineNumber, |
| 177 | + offset, |
| 178 | + lineNumber, |
| 179 | + offset + relativePath.length, |
| 180 | + ); |
| 181 | + |
| 182 | + lineNumber++; |
| 183 | + |
| 184 | + if (relativePath === "") { |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + const absPath = path.resolve(wsPath, relativePath); |
| 189 | + const uri = vscode.Uri.file(absPath); |
| 190 | + |
| 191 | + if (selected) { |
| 192 | + hasSelectedLink = true; |
| 193 | + } |
| 194 | + |
| 195 | + links.push({ range, uri, selected }); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + return links; |
| 200 | +} |
| 201 | + |
| 202 | +async function getQuery(): Promise<string | undefined> { |
| 203 | + const query = await vscode.window.showInputBox({ |
| 204 | + prompt: "Search query", |
| 205 | + placeHolder: "Search query", |
| 206 | + ignoreFocusOut: true, |
| 207 | + validateInput: (value) => { |
| 208 | + if (value.trim()) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + return "Can't be empty"; |
| 212 | + }, |
| 213 | + }); |
| 214 | + return query ? query.trim() : undefined; |
| 215 | +} |
0 commit comments