|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 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 | +import * as vscode from "vscode"; |
| 15 | +import { Workbench } from "../../src/utilities/command"; |
| 16 | +import { DebugAdapter } from "../../src/debugger/debugAdapter"; |
| 17 | +import { WorkspaceContext } from "../../src/WorkspaceContext"; |
| 18 | + |
| 19 | +export async function continueSession(): Promise<void> { |
| 20 | + await vscode.commands.executeCommand(Workbench.ACTION_DEBUG_CONTINUE); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Waits for a specific message from the debug adapter. |
| 25 | + * |
| 26 | + * @param name The name of the debug session to wait for. |
| 27 | + * @param matches A function to match the desired message. |
| 28 | + * @param workspaceContext The workspace context containing toolchain information. |
| 29 | + * @returns A promise that resolves with the matching message. |
| 30 | + */ |
| 31 | +export async function waitForDebugAdapterMessage( |
| 32 | + name: string, |
| 33 | + matches: (message: any) => boolean, |
| 34 | + workspaceContext: WorkspaceContext |
| 35 | +): Promise<any> { |
| 36 | + return await new Promise<any>(res => { |
| 37 | + const disposable = vscode.debug.registerDebugAdapterTrackerFactory( |
| 38 | + DebugAdapter.getLaunchConfigType(workspaceContext.toolchain.swiftVersion), |
| 39 | + { |
| 40 | + createDebugAdapterTracker: function ( |
| 41 | + session: vscode.DebugSession |
| 42 | + ): vscode.ProviderResult<vscode.DebugAdapterTracker> { |
| 43 | + if (session.name !== name) { |
| 44 | + return; |
| 45 | + } |
| 46 | + return { |
| 47 | + onDidSendMessage(message) { |
| 48 | + if (matches(message)) { |
| 49 | + disposable.dispose(); |
| 50 | + res(message); |
| 51 | + } |
| 52 | + }, |
| 53 | + }; |
| 54 | + }, |
| 55 | + } |
| 56 | + ); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Waits for a specific command to be sent by the debug adapter. |
| 62 | + * |
| 63 | + * @param name The name of the debug session to wait for. |
| 64 | + * @param command The command to wait for. |
| 65 | + * @param workspaceContext The workspace context containing toolchain information. |
| 66 | + * @returns A promise that resolves with the matching command message. |
| 67 | + */ |
| 68 | +export async function waitForDebugAdapterCommand( |
| 69 | + name: string, |
| 70 | + command: string, |
| 71 | + workspaceContext: WorkspaceContext |
| 72 | +): Promise<any> { |
| 73 | + return await waitForDebugAdapterMessage( |
| 74 | + name, |
| 75 | + (m: any) => m.command === command, |
| 76 | + workspaceContext |
| 77 | + ); |
| 78 | +} |
0 commit comments