]> git.lizzy.rs Git - rust.git/commitdiff
Uniformed way to get Debug Lens target executable.
authorvsrs <vit@conrlab.com>
Wed, 6 May 2020 13:01:35 +0000 (16:01 +0300)
committervsrs <vit@conrlab.com>
Wed, 6 May 2020 13:06:21 +0000 (16:06 +0300)
editors/code/src/commands/runnables.ts
editors/code/src/config.ts

index d77e8188c75f67d9e8784be3cdbebcdc83e5cd23..7bb8727e7cdbf8555dd71fd876358bac50368535 100644 (file)
@@ -64,29 +64,19 @@ export function runSingle(ctx: Ctx): Cmd {
     };
 }
 
-function getLldbDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): vscode.DebugConfiguration {
+function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
     return {
         type: "lldb",
         request: "launch",
         name: config.label,
-        cargo: {
-            args: config.args,
-        },
+        program: executable,
         args: config.extraArgs,
         cwd: config.cwd,
         sourceMap: sourceFileMap
     };
 }
 
-const debugOutput = vscode.window.createOutputChannel("Debug");
-
-async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): Promise<vscode.DebugConfiguration> {
-    debugOutput.clear();
-
-    const cargo = new Cargo(config.cwd || '.', debugOutput);
-    const executable = await cargo.executableFromArgs(config.args);
-
-    // if we are here, there were no compilation errors.
+function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
     return {
         type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg',
         request: "launch",
@@ -98,36 +88,53 @@ async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<st
     };
 }
 
+const debugOutput = vscode.window.createOutputChannel("Debug");
+
+async function getDebugExecutable(config: ra.Runnable): Promise<string> {
+    debugOutput.clear();
+
+    const cargo = new Cargo(config.cwd || '.', debugOutput);
+    const executable = await cargo.executableFromArgs(config.args);
+
+    // if we are here, there were no compilation errors.
+    return executable;
+}
+
+type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
+
 export function debugSingle(ctx: Ctx): Cmd {
     return async (config: ra.Runnable) => {
         const editor = ctx.activeRustEditor;
         if (!editor) return;
 
-        const lldbId = "vadimcn.vscode-lldb";
-        const cpptoolsId = "ms-vscode.cpptools";
+        const knownEngines: Record<string, DebugConfigProvider> = {
+            "vadimcn.vscode-lldb": getLldbDebugConfig,
+            "ms-vscode.cpptools": getCppvsDebugConfig
+        };
+        const debugOptions = ctx.config.debug;
 
-        const debugEngineId = ctx.config.debug.engine;
         let debugEngine = null;
-        if (debugEngineId === "auto") {
-            debugEngine = vscode.extensions.getExtension(lldbId);
-            if (!debugEngine) {
-                debugEngine = vscode.extensions.getExtension(cpptoolsId);
+        if (debugOptions.engine === "auto") {
+            for (var engineId in knownEngines) {
+                debugEngine = vscode.extensions.getExtension(engineId);
+                if (debugEngine) break;
             }
         }
         else {
-            debugEngine = vscode.extensions.getExtension(debugEngineId);
+            debugEngine = vscode.extensions.getExtension(debugOptions.engine);
         }
 
         if (!debugEngine) {
-            vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})`
-                + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`);
+            vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)`
+                + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`);
             return;
         }
 
-        const debugConfig = lldbId === debugEngine.id
-            ? getLldbDebugConfig(config, ctx.config.debug.sourceFileMap)
-            : await getCppvsDebugConfig(config, ctx.config.debug.sourceFileMap);
+        const executable = await getDebugExecutable(config);
+        const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap);
 
+        debugOutput.appendLine("Launching debug configuration:");
+        debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
         return vscode.debug.startDebugging(undefined, debugConfig);
     };
 }
index 110e541800ed8f131c9dc8eb2c36704d23d94c80..8bceaaf7245332a691a40f91868f1b5e632de273 100644 (file)
@@ -108,10 +108,12 @@ export class Config {
     }
 
     get debug() {
+        // "/rustc/<id>" used by suggestions only.
+        const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
+
         return {
             engine: this.get<string>("debug.engine"),
-            sourceFileMap: this.get<Record<string, string>>("debug.sourceFileMap"),
+            sourceFileMap: sourceFileMap,
         };
     }
-
 }