[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / tools / lldb-dap / src-ts / debug-adapter-factory.ts
blob2be21bfdf0dd69c0b0bcca29a234b7e526ca1f0f
1 import * as vscode from "vscode";
2 import { LLDBDapOptions } from "./types";
4 /**
5  * This class defines a factory used to find the lldb-dap binary to use
6  * depending on the session configuration.
7  */
8 export class LLDBDapDescriptorFactory
9   implements vscode.DebugAdapterDescriptorFactory
11   private lldbDapOptions: LLDBDapOptions;
13   constructor(lldbDapOptions: LLDBDapOptions) {
14     this.lldbDapOptions = lldbDapOptions;
15   }
17   static async isValidDebugAdapterPath(
18     pathUri: vscode.Uri,
19   ): Promise<Boolean> {
20     try {
21       const fileStats = await vscode.workspace.fs.stat(pathUri);
22       if (!(fileStats.type & vscode.FileType.File)) {
23         return false;
24       }
25     } catch (err) {
26       return false;
27     }
28     return true;
29   }
31   async createDebugAdapterDescriptor(
32     session: vscode.DebugSession,
33     executable: vscode.DebugAdapterExecutable | undefined,
34   ): Promise<vscode.DebugAdapterDescriptor | undefined> {
35     const config = vscode.workspace.getConfiguration(
36       "lldb-dap",
37       session.workspaceFolder,
38     );
39     const customPath = config.get<string>("executable-path");
40     const path: string = customPath || executable!!.command;
42     const fileUri = vscode.Uri.file(path);
43     if (!(await LLDBDapDescriptorFactory.isValidDebugAdapterPath(fileUri))) {
44       LLDBDapDescriptorFactory.showLLDBDapNotFoundMessage(fileUri.path);
45     }
46     return this.lldbDapOptions.createDapExecutableCommand(session, executable);
47   }
49   /**
50    * Shows a message box when the debug adapter's path is not found
51    */
52   static async showLLDBDapNotFoundMessage(path: string) {
53     const openSettingsAction = "Open Settings";
54     const callbackValue = await vscode.window.showErrorMessage(
55       `Debug adapter path: ${path} is not a valid file`,
56       openSettingsAction,
57     );
59     if (openSettingsAction === callbackValue) {
60       vscode.commands.executeCommand(
61         "workbench.action.openSettings",
62         "lldb-dap.executable-path",
63       );
64     }
65   }