diff options
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/runnables.ts | 149 |
1 files changed, 43 insertions, 106 deletions
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index ae328d2a4..c1b872bce 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts | |||
@@ -1,43 +1,46 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import * as ra from '../rust-analyzer-api'; | 3 | import * as ra from '../rust-analyzer-api'; |
4 | import * as os from "os"; | ||
5 | 4 | ||
6 | import { Ctx, Cmd } from '../ctx'; | 5 | import { Ctx, Cmd } from '../ctx'; |
7 | import { Cargo } from '../cargo'; | 6 | import { startDebugSession } from '../debug'; |
7 | |||
8 | async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise<RunnableQuickPick | undefined> { | ||
9 | const editor = ctx.activeRustEditor; | ||
10 | const client = ctx.client; | ||
11 | if (!editor || !client) return; | ||
12 | |||
13 | const textDocument: lc.TextDocumentIdentifier = { | ||
14 | uri: editor.document.uri.toString(), | ||
15 | }; | ||
16 | |||
17 | const runnables = await client.sendRequest(ra.runnables, { | ||
18 | textDocument, | ||
19 | position: client.code2ProtocolConverter.asPosition( | ||
20 | editor.selection.active, | ||
21 | ), | ||
22 | }); | ||
23 | const items: RunnableQuickPick[] = []; | ||
24 | if (prevRunnable) { | ||
25 | items.push(prevRunnable); | ||
26 | } | ||
27 | for (const r of runnables) { | ||
28 | if ( | ||
29 | prevRunnable && | ||
30 | JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) | ||
31 | ) { | ||
32 | continue; | ||
33 | } | ||
34 | items.push(new RunnableQuickPick(r)); | ||
35 | } | ||
36 | return await vscode.window.showQuickPick(items); | ||
37 | } | ||
8 | 38 | ||
9 | export function run(ctx: Ctx): Cmd { | 39 | export function run(ctx: Ctx): Cmd { |
10 | let prevRunnable: RunnableQuickPick | undefined; | 40 | let prevRunnable: RunnableQuickPick | undefined; |
11 | 41 | ||
12 | return async () => { | 42 | return async () => { |
13 | const editor = ctx.activeRustEditor; | 43 | const item = await selectRunnable(ctx, prevRunnable); |
14 | const client = ctx.client; | ||
15 | if (!editor || !client) return; | ||
16 | |||
17 | const textDocument: lc.TextDocumentIdentifier = { | ||
18 | uri: editor.document.uri.toString(), | ||
19 | }; | ||
20 | |||
21 | const runnables = await client.sendRequest(ra.runnables, { | ||
22 | textDocument, | ||
23 | position: client.code2ProtocolConverter.asPosition( | ||
24 | editor.selection.active, | ||
25 | ), | ||
26 | }); | ||
27 | const items: RunnableQuickPick[] = []; | ||
28 | if (prevRunnable) { | ||
29 | items.push(prevRunnable); | ||
30 | } | ||
31 | for (const r of runnables) { | ||
32 | if ( | ||
33 | prevRunnable && | ||
34 | JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) | ||
35 | ) { | ||
36 | continue; | ||
37 | } | ||
38 | items.push(new RunnableQuickPick(r)); | ||
39 | } | ||
40 | const item = await vscode.window.showQuickPick(items); | ||
41 | if (!item) return; | 44 | if (!item) return; |
42 | 45 | ||
43 | item.detail = 'rerun'; | 46 | item.detail = 'rerun'; |
@@ -64,88 +67,22 @@ export function runSingle(ctx: Ctx): Cmd { | |||
64 | }; | 67 | }; |
65 | } | 68 | } |
66 | 69 | ||
67 | function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { | 70 | export function debug(ctx: Ctx): Cmd { |
68 | return { | 71 | let prevDebuggee: RunnableQuickPick | undefined; |
69 | type: "lldb", | ||
70 | request: "launch", | ||
71 | name: config.label, | ||
72 | program: executable, | ||
73 | args: config.extraArgs, | ||
74 | cwd: config.cwd, | ||
75 | sourceMap: sourceFileMap, | ||
76 | sourceLanguages: ["rust"] | ||
77 | }; | ||
78 | } | ||
79 | |||
80 | function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration { | ||
81 | return { | ||
82 | type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg', | ||
83 | request: "launch", | ||
84 | name: config.label, | ||
85 | program: executable, | ||
86 | args: config.extraArgs, | ||
87 | cwd: config.cwd, | ||
88 | sourceFileMap: sourceFileMap, | ||
89 | }; | ||
90 | } | ||
91 | 72 | ||
92 | const debugOutput = vscode.window.createOutputChannel("Debug"); | 73 | return async () => { |
93 | 74 | const item = await selectRunnable(ctx, prevDebuggee); | |
94 | async function getDebugExecutable(config: ra.Runnable): Promise<string> { | 75 | if (!item) return; |
95 | const cargo = new Cargo(config.cwd || '.', debugOutput); | ||
96 | const executable = await cargo.executableFromArgs(config.args); | ||
97 | 76 | ||
98 | // if we are here, there were no compilation errors. | 77 | item.detail = 'restart'; |
99 | return executable; | 78 | prevDebuggee = item; |
79 | return await startDebugSession(ctx, item.runnable); | ||
80 | }; | ||
100 | } | 81 | } |
101 | 82 | ||
102 | type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration; | ||
103 | |||
104 | export function debugSingle(ctx: Ctx): Cmd { | 83 | export function debugSingle(ctx: Ctx): Cmd { |
105 | return async (config: ra.Runnable) => { | 84 | return async (config: ra.Runnable) => { |
106 | const editor = ctx.activeRustEditor; | 85 | await startDebugSession(ctx, config); |
107 | if (!editor) return; | ||
108 | |||
109 | const knownEngines: Record<string, DebugConfigProvider> = { | ||
110 | "vadimcn.vscode-lldb": getLldbDebugConfig, | ||
111 | "ms-vscode.cpptools": getCppvsDebugConfig | ||
112 | }; | ||
113 | const debugOptions = ctx.config.debug; | ||
114 | |||
115 | let debugEngine = null; | ||
116 | if (debugOptions.engine === "auto") { | ||
117 | for (var engineId in knownEngines) { | ||
118 | debugEngine = vscode.extensions.getExtension(engineId); | ||
119 | if (debugEngine) break; | ||
120 | } | ||
121 | } | ||
122 | else { | ||
123 | debugEngine = vscode.extensions.getExtension(debugOptions.engine); | ||
124 | } | ||
125 | |||
126 | if (!debugEngine) { | ||
127 | vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)` | ||
128 | + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | debugOutput.clear(); | ||
133 | if (ctx.config.debug.openUpDebugPane) { | ||
134 | debugOutput.show(true); | ||
135 | } | ||
136 | |||
137 | const executable = await getDebugExecutable(config); | ||
138 | const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); | ||
139 | if (debugConfig.type in debugOptions.engineSettings) { | ||
140 | const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; | ||
141 | for (var key in settingsMap) { | ||
142 | debugConfig[key] = settingsMap[key]; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | debugOutput.appendLine("Launching debug configuration:"); | ||
147 | debugOutput.appendLine(JSON.stringify(debugConfig, null, 2)); | ||
148 | return vscode.debug.startDebugging(undefined, debugConfig); | ||
149 | }; | 86 | }; |
150 | } | 87 | } |
151 | 88 | ||