diff options
author | vsrs <[email protected]> | 2020-05-11 14:06:57 +0100 |
---|---|---|
committer | vsrs <[email protected]> | 2020-05-11 14:06:57 +0100 |
commit | 155f0601421620086a256c9e313568d5bd7391e0 (patch) | |
tree | 88e80aaffb643fd168e272abd67281757e55a8d9 /editors/code/src/debug.ts | |
parent | eb892d707c379eff514df9c2a6b2203f38874b14 (diff) |
"rust-analyzer.debug" command
Diffstat (limited to 'editors/code/src/debug.ts')
-rw-r--r-- | editors/code/src/debug.ts | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts new file mode 100644 index 000000000..4f4b88adf --- /dev/null +++ b/editors/code/src/debug.ts | |||
@@ -0,0 +1,95 @@ | |||
1 | import * as os from "os"; | ||
2 | import * as vscode from 'vscode'; | ||
3 | import * as ra from './rust-analyzer-api'; | ||
4 | |||
5 | import { Cargo } from './cargo'; | ||
6 | import { Ctx } from "./ctx"; | ||
7 | |||
8 | const debugOutput = vscode.window.createOutputChannel("Debug"); | ||
9 | type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration; | ||
10 | |||
11 | function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { | ||
12 | return { | ||
13 | type: "lldb", | ||
14 | request: "launch", | ||
15 | name: config.label, | ||
16 | program: executable, | ||
17 | args: config.extraArgs, | ||
18 | cwd: config.cwd, | ||
19 | sourceMap: sourceFileMap, | ||
20 | sourceLanguages: ["rust"] | ||
21 | }; | ||
22 | } | ||
23 | |||
24 | function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { | ||
25 | return { | ||
26 | type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg", | ||
27 | request: "launch", | ||
28 | name: config.label, | ||
29 | program: executable, | ||
30 | args: config.extraArgs, | ||
31 | cwd: config.cwd, | ||
32 | sourceFileMap: sourceFileMap, | ||
33 | }; | ||
34 | } | ||
35 | |||
36 | async function getDebugExecutable(config: ra.Runnable): Promise<string> { | ||
37 | const cargo = new Cargo(config.cwd || '.', debugOutput); | ||
38 | const executable = await cargo.executableFromArgs(config.args); | ||
39 | |||
40 | // if we are here, there were no compilation errors. | ||
41 | return executable; | ||
42 | } | ||
43 | |||
44 | export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> { | ||
45 | const editor = ctx.activeRustEditor; | ||
46 | if (!editor) return; | ||
47 | |||
48 | const knownEngines: Record<string, DebugConfigProvider> = { | ||
49 | "vadimcn.vscode-lldb": getLldbDebugConfig, | ||
50 | "ms-vscode.cpptools": getCppvsDebugConfig | ||
51 | }; | ||
52 | const debugOptions = ctx.config.debug; | ||
53 | |||
54 | let debugEngine = null; | ||
55 | if (debugOptions.engine === "auto") { | ||
56 | for (var engineId in knownEngines) { | ||
57 | debugEngine = vscode.extensions.getExtension(engineId); | ||
58 | if (debugEngine) break; | ||
59 | } | ||
60 | } | ||
61 | else { | ||
62 | debugEngine = vscode.extensions.getExtension(debugOptions.engine); | ||
63 | } | ||
64 | |||
65 | if (!debugEngine) { | ||
66 | vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)` | ||
67 | + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`); | ||
68 | return; | ||
69 | } | ||
70 | |||
71 | debugOutput.clear(); | ||
72 | if (ctx.config.debug.openUpDebugPane) { | ||
73 | debugOutput.show(true); | ||
74 | } | ||
75 | |||
76 | const executable = await getDebugExecutable(config); | ||
77 | const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); | ||
78 | if (debugConfig.type in debugOptions.engineSettings) { | ||
79 | const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; | ||
80 | for (var key in settingsMap) { | ||
81 | debugConfig[key] = settingsMap[key]; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | return debugConfig; | ||
86 | } | ||
87 | |||
88 | export async function startDebugSession(ctx: Ctx, config: ra.Runnable): Promise<boolean> { | ||
89 | const debugConfig = await getDebugConfiguration(ctx, config); | ||
90 | if (!debugConfig) return false; | ||
91 | |||
92 | debugOutput.appendLine("Launching debug configuration:"); | ||
93 | debugOutput.appendLine(JSON.stringify(debugConfig, null, 2)); | ||
94 | return vscode.debug.startDebugging(undefined, debugConfig); | ||
95 | } | ||