|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +export class LaunchUriHandler implements vscode.UriHandler { |
| 4 | + async handleUri(uri: vscode.Uri) { |
| 5 | + try { |
| 6 | + const params = new URLSearchParams(uri.query); |
| 7 | + if (uri.path == '/start') { |
| 8 | + // Some properties have default values |
| 9 | + let debugConfig: vscode.DebugConfiguration = { |
| 10 | + type: 'lldb-dap', |
| 11 | + request: 'launch', |
| 12 | + name: '', |
| 13 | + }; |
| 14 | + // The `config` parameter allows providing a complete JSON-encoded configuration |
| 15 | + const configJson = params.get("config"); |
| 16 | + if (configJson !== null) { |
| 17 | + Object.assign(debugConfig, JSON.parse(configJson)); |
| 18 | + } |
| 19 | + // Furthermore, some frequently used parameters can also be provided as separate parameters |
| 20 | + const stringKeys = ["name", "request", "program", "cwd", "debuggerRoot"]; |
| 21 | + const numberKeys = ["pid"]; |
| 22 | + const arrayKeys = [ |
| 23 | + "args", "initCommands", "preRunCommands", "stopCommands", "exitCommands", |
| 24 | + "terminateCommands", "launchCommands", "attachCommands" |
| 25 | + ]; |
| 26 | + for (const key of stringKeys) { |
| 27 | + const value = params.get(key); |
| 28 | + if (value) { |
| 29 | + debugConfig[key] = value; |
| 30 | + } |
| 31 | + } |
| 32 | + for (const key of numberKeys) { |
| 33 | + const value = params.get(key); |
| 34 | + if (value) { |
| 35 | + debugConfig[key] = Number(value); |
| 36 | + } |
| 37 | + } |
| 38 | + for (const key of arrayKeys) { |
| 39 | + // `getAll()` returns an array of strings. |
| 40 | + const value = params.getAll(key); |
| 41 | + if (value) { |
| 42 | + debugConfig[key] = value; |
| 43 | + } |
| 44 | + } |
| 45 | + // Report an error if we received any unknown parameters |
| 46 | + const supportedKeys = new Set<string>(["config"].concat(stringKeys).concat(numberKeys).concat(arrayKeys)); |
| 47 | + const presentKeys = new Set<string>(params.keys()); |
| 48 | + // FIXME: Use `Set.difference` as soon as ES2024 is widely available |
| 49 | + const unknownKeys = new Set<string>(); |
| 50 | + for (const k of presentKeys.keys()) { |
| 51 | + if (!supportedKeys.has(k)) { |
| 52 | + unknownKeys.add(k); |
| 53 | + } |
| 54 | + } |
| 55 | + if (unknownKeys.size > 0) { |
| 56 | + throw new Error(`Unsupported URL parameters: ${Array.from(unknownKeys.keys()).join(", ")}`); |
| 57 | + } |
| 58 | + // Prodide a default for the config name |
| 59 | + const defaultName = debugConfig.request == 'launch' ? "URL-based Launch" : "URL-based Attach"; |
| 60 | + debugConfig.name = debugConfig.name || debugConfig.program || defaultName; |
| 61 | + // Force the type to `lldb-dap`. We don't want to allow launching any other |
| 62 | + // Debug Adapters using this URI scheme. |
| 63 | + if (debugConfig.type != "lldb-dap") { |
| 64 | + throw new Error(`Unsupported debugger type: ${debugConfig.type}`); |
| 65 | + } |
| 66 | + await vscode.debug.startDebugging(undefined, debugConfig); |
| 67 | + } else { |
| 68 | + throw new Error(`Unsupported Uri path: ${uri.path}`); |
| 69 | + } |
| 70 | + } catch (err) { |
| 71 | + if (err instanceof Error) { |
| 72 | + await vscode.window.showErrorMessage(`Failed to handle lldb-dap URI request: ${err.message}`); |
| 73 | + } else { |
| 74 | + await vscode.window.showErrorMessage(`Failed to handle lldb-dap URI request: ${JSON.stringify(err)}`); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments