|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as path from "path"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { createProcessList } from "../process-list"; |
| 18 | + |
| 19 | +interface ProcessQuickPick extends vscode.QuickPickItem { |
| 20 | + processId?: number; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Prompts the user to select a running process. |
| 25 | + * |
| 26 | + * The return value must be a string so that it is compatible with VS Code's |
| 27 | + * string substitution infrastructure. The value will eventually be converted |
| 28 | + * to a number by the debug configuration provider. |
| 29 | + * |
| 30 | + * @param configuration The related debug configuration, if any |
| 31 | + * @returns The pid of the process as a string or undefined if cancelled. |
| 32 | + */ |
| 33 | +export async function pickProcess( |
| 34 | + configuration?: vscode.DebugConfiguration |
| 35 | +): Promise<string | undefined> { |
| 36 | + const processList = createProcessList(); |
| 37 | + const selectedProcess = await vscode.window.showQuickPick<ProcessQuickPick>( |
| 38 | + processList.listAllProcesses().then((processes): ProcessQuickPick[] => { |
| 39 | + // Sort by start date in descending order |
| 40 | + processes.sort((a, b) => b.start - a.start); |
| 41 | + // Filter by program if requested |
| 42 | + if (typeof configuration?.program === "string") { |
| 43 | + const program = configuration.program; |
| 44 | + const programBaseName = path.basename(program); |
| 45 | + processes = processes |
| 46 | + .filter(proc => path.basename(proc.command) === programBaseName) |
| 47 | + .sort((a, b) => { |
| 48 | + // Bring exact command matches to the top |
| 49 | + const aIsExactMatch = a.command === program ? 1 : 0; |
| 50 | + const bIsExactMatch = b.command === program ? 1 : 0; |
| 51 | + return bIsExactMatch - aIsExactMatch; |
| 52 | + }); |
| 53 | + // Show a better message if all processes were filtered out |
| 54 | + if (processes.length === 0) { |
| 55 | + return [ |
| 56 | + { |
| 57 | + label: "No processes matched the debug configuration's program", |
| 58 | + }, |
| 59 | + ]; |
| 60 | + } |
| 61 | + } |
| 62 | + // Convert to a QuickPickItem |
| 63 | + return processes.map(proc => { |
| 64 | + return { |
| 65 | + processId: proc.id, |
| 66 | + label: path.basename(proc.command), |
| 67 | + description: proc.id.toString(), |
| 68 | + detail: proc.arguments, |
| 69 | + } satisfies ProcessQuickPick; |
| 70 | + }); |
| 71 | + }), |
| 72 | + { |
| 73 | + placeHolder: "Select a process to attach the debugger to", |
| 74 | + matchOnDetail: true, |
| 75 | + matchOnDescription: true, |
| 76 | + } |
| 77 | + ); |
| 78 | + return selectedProcess?.processId?.toString(); |
| 79 | +} |
0 commit comments