Skip to content

Commit 61e8f39

Browse files
bors[bot]matklad
andauthored
Merge #4708
4708: Move run commands to commands.ts r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents fe6508b + 0ced18e commit 61e8f39

File tree

3 files changed

+134
-133
lines changed

3 files changed

+134
-133
lines changed

editors/code/src/commands.ts

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { spawnSync } from 'child_process';
88
import { RunnableQuickPick, selectRunnable, createTask } from './run';
99
import { AstInspector } from './ast_inspector';
1010
import { isRustDocument, sleep, isRustEditor } from './util';
11+
import { startDebugSession, makeDebugConfig } from './debug';
1112

1213
export * from './ast_inspector';
1314
export * from './run';
@@ -197,20 +198,6 @@ export function toggleInlayHints(ctx: Ctx): Cmd {
197198
};
198199
}
199200

200-
export function run(ctx: Ctx): Cmd {
201-
let prevRunnable: RunnableQuickPick | undefined;
202-
203-
return async () => {
204-
const item = await selectRunnable(ctx, prevRunnable);
205-
if (!item) return;
206-
207-
item.detail = 'rerun';
208-
prevRunnable = item;
209-
const task = createTask(item.runnable);
210-
return await vscode.tasks.executeTask(task);
211-
};
212-
}
213-
214201
// Opens the virtual file that will show the syntax tree
215202
//
216203
// The contents of the file come from the `TextDocumentContentProvider`
@@ -368,3 +355,62 @@ export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd {
368355
await applySnippetWorkspaceEdit(edit);
369356
};
370357
}
358+
359+
export function run(ctx: Ctx): Cmd {
360+
let prevRunnable: RunnableQuickPick | undefined;
361+
362+
return async () => {
363+
const item = await selectRunnable(ctx, prevRunnable);
364+
if (!item) return;
365+
366+
item.detail = 'rerun';
367+
prevRunnable = item;
368+
const task = createTask(item.runnable);
369+
return await vscode.tasks.executeTask(task);
370+
};
371+
}
372+
373+
export function runSingle(ctx: Ctx): Cmd {
374+
return async (runnable: ra.Runnable) => {
375+
const editor = ctx.activeRustEditor;
376+
if (!editor) return;
377+
378+
const task = createTask(runnable);
379+
task.group = vscode.TaskGroup.Build;
380+
task.presentationOptions = {
381+
reveal: vscode.TaskRevealKind.Always,
382+
panel: vscode.TaskPanelKind.Dedicated,
383+
clear: true,
384+
};
385+
386+
return vscode.tasks.executeTask(task);
387+
};
388+
}
389+
390+
export function debug(ctx: Ctx): Cmd {
391+
let prevDebuggee: RunnableQuickPick | undefined;
392+
393+
return async () => {
394+
const item = await selectRunnable(ctx, prevDebuggee, true);
395+
if (!item) return;
396+
397+
item.detail = 'restart';
398+
prevDebuggee = item;
399+
return await startDebugSession(ctx, item.runnable);
400+
};
401+
}
402+
403+
export function debugSingle(ctx: Ctx): Cmd {
404+
return async (config: ra.Runnable) => {
405+
await startDebugSession(ctx, config);
406+
};
407+
}
408+
409+
export function newDebugConfig(ctx: Ctx): Cmd {
410+
return async () => {
411+
const item = await selectRunnable(ctx, undefined, true, false);
412+
if (!item) return;
413+
414+
await makeDebugConfig(ctx, item.runnable);
415+
};
416+
}

editors/code/src/debug.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,53 @@ import { Ctx } from "./ctx";
99
const debugOutput = vscode.window.createOutputChannel("Debug");
1010
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
1111

12-
function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
13-
return {
14-
type: "lldb",
15-
request: "launch",
16-
name: config.label,
17-
program: executable,
18-
args: config.extraArgs,
19-
cwd: config.cwd,
20-
sourceMap: sourceFileMap,
21-
sourceLanguages: ["rust"]
22-
};
23-
}
12+
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
13+
const scope = ctx.activeRustEditor?.document.uri;
14+
if (!scope) return;
2415

25-
function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
26-
return {
27-
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
28-
request: "launch",
29-
name: config.label,
30-
program: executable,
31-
args: config.extraArgs,
32-
cwd: config.cwd,
33-
sourceFileMap: sourceFileMap,
34-
};
16+
const debugConfig = await getDebugConfiguration(ctx, runnable);
17+
if (!debugConfig) return;
18+
19+
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
20+
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
21+
22+
const index = configurations.findIndex(c => c.name === debugConfig.name);
23+
if (index !== -1) {
24+
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
25+
if (answer === "Cancel") return;
26+
27+
configurations[index] = debugConfig;
28+
} else {
29+
configurations.push(debugConfig);
30+
}
31+
32+
await wsLaunchSection.update("configurations", configurations);
3533
}
3634

37-
async function getDebugExecutable(config: ra.Runnable): Promise<string> {
38-
const cargo = new Cargo(config.cwd || '.', debugOutput);
39-
const executable = await cargo.executableFromArgs(config.args);
35+
export async function startDebugSession(ctx: Ctx, runnable: ra.Runnable): Promise<boolean> {
36+
let debugConfig: vscode.DebugConfiguration | undefined = undefined;
37+
let message = "";
4038

41-
// if we are here, there were no compilation errors.
42-
return executable;
39+
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
40+
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
41+
42+
const index = configurations.findIndex(c => c.name === runnable.label);
43+
if (-1 !== index) {
44+
debugConfig = configurations[index];
45+
message = " (from launch.json)";
46+
debugOutput.clear();
47+
} else {
48+
debugConfig = await getDebugConfiguration(ctx, runnable);
49+
}
50+
51+
if (!debugConfig) return false;
52+
53+
debugOutput.appendLine(`Launching debug configuration${message}:`);
54+
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
55+
return vscode.debug.startDebugging(undefined, debugConfig);
4356
}
4457

45-
export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> {
58+
async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> {
4659
const editor = ctx.activeRustEditor;
4760
if (!editor) return;
4861

@@ -78,8 +91,8 @@ export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Prom
7891
return path.normalize(p).replace(wsFolder, '${workspaceRoot}');
7992
}
8093

81-
const executable = await getDebugExecutable(config);
82-
const debugConfig = knownEngines[debugEngine.id](config, simplifyPath(executable), debugOptions.sourceFileMap);
94+
const executable = await getDebugExecutable(runnable);
95+
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), debugOptions.sourceFileMap);
8396
if (debugConfig.type in debugOptions.engineSettings) {
8497
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
8598
for (var key in settingsMap) {
@@ -100,25 +113,35 @@ export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Prom
100113
return debugConfig;
101114
}
102115

103-
export async function startDebugSession(ctx: Ctx, config: ra.Runnable): Promise<boolean> {
104-
let debugConfig: vscode.DebugConfiguration | undefined = undefined;
105-
let message = "";
106-
107-
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
108-
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
116+
async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
117+
const cargo = new Cargo(runnable.cwd || '.', debugOutput);
118+
const executable = await cargo.executableFromArgs(runnable.args);
109119

110-
const index = configurations.findIndex(c => c.name === config.label);
111-
if (-1 !== index) {
112-
debugConfig = configurations[index];
113-
message = " (from launch.json)";
114-
debugOutput.clear();
115-
} else {
116-
debugConfig = await getDebugConfiguration(ctx, config);
117-
}
120+
// if we are here, there were no compilation errors.
121+
return executable;
122+
}
118123

119-
if (!debugConfig) return false;
124+
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
125+
return {
126+
type: "lldb",
127+
request: "launch",
128+
name: runnable.label,
129+
program: executable,
130+
args: runnable.extraArgs,
131+
cwd: runnable.cwd,
132+
sourceMap: sourceFileMap,
133+
sourceLanguages: ["rust"]
134+
};
135+
}
120136

121-
debugOutput.appendLine(`Launching debug configuration${message}:`);
122-
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
123-
return vscode.debug.startDebugging(undefined, debugConfig);
137+
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
138+
return {
139+
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
140+
request: "launch",
141+
name: runnable.label,
142+
program: executable,
143+
args: runnable.extraArgs,
144+
cwd: runnable.cwd,
145+
sourceFileMap: sourceFileMap,
146+
};
124147
}

editors/code/src/run.ts

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as lc from 'vscode-languageclient';
33
import * as ra from './lsp_ext';
44
import * as toolchain from "./toolchain";
55

6-
import { Ctx, Cmd } from './ctx';
7-
import { startDebugSession, getDebugConfiguration } from './debug';
6+
import { Ctx } from './ctx';
7+
import { makeDebugConfig } from './debug';
88

99
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
1010

@@ -65,7 +65,7 @@ export async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick,
6565
quickPick.onDidHide(() => close()),
6666
quickPick.onDidAccept(() => close(quickPick.selectedItems[0])),
6767
quickPick.onDidTriggerButton((_button) => {
68-
(async () => await makeDebugConfig(ctx, quickPick.activeItems[0]))();
68+
(async () => await makeDebugConfig(ctx, quickPick.activeItems[0].runnable))();
6969
close();
7070
}),
7171
quickPick.onDidChangeActive((active) => {
@@ -84,74 +84,6 @@ export async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick,
8484
});
8585
}
8686

87-
export function runSingle(ctx: Ctx): Cmd {
88-
return async (runnable: ra.Runnable) => {
89-
const editor = ctx.activeRustEditor;
90-
if (!editor) return;
91-
92-
const task = createTask(runnable);
93-
task.group = vscode.TaskGroup.Build;
94-
task.presentationOptions = {
95-
reveal: vscode.TaskRevealKind.Always,
96-
panel: vscode.TaskPanelKind.Dedicated,
97-
clear: true,
98-
};
99-
100-
return vscode.tasks.executeTask(task);
101-
};
102-
}
103-
104-
export function debug(ctx: Ctx): Cmd {
105-
let prevDebuggee: RunnableQuickPick | undefined;
106-
107-
return async () => {
108-
const item = await selectRunnable(ctx, prevDebuggee, true);
109-
if (!item) return;
110-
111-
item.detail = 'restart';
112-
prevDebuggee = item;
113-
return await startDebugSession(ctx, item.runnable);
114-
};
115-
}
116-
117-
export function debugSingle(ctx: Ctx): Cmd {
118-
return async (config: ra.Runnable) => {
119-
await startDebugSession(ctx, config);
120-
};
121-
}
122-
123-
async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise<void> {
124-
const scope = ctx.activeRustEditor?.document.uri;
125-
if (!scope) return;
126-
127-
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
128-
if (!debugConfig) return;
129-
130-
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
131-
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
132-
133-
const index = configurations.findIndex(c => c.name === debugConfig.name);
134-
if (index !== -1) {
135-
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
136-
if (answer === "Cancel") return;
137-
138-
configurations[index] = debugConfig;
139-
} else {
140-
configurations.push(debugConfig);
141-
}
142-
143-
await wsLaunchSection.update("configurations", configurations);
144-
}
145-
146-
export function newDebugConfig(ctx: Ctx): Cmd {
147-
return async () => {
148-
const item = await selectRunnable(ctx, undefined, true, false);
149-
if (!item) return;
150-
151-
await makeDebugConfig(ctx, item);
152-
};
153-
}
154-
15587
export class RunnableQuickPick implements vscode.QuickPickItem {
15688
public label: string;
15789
public description?: string | undefined;

0 commit comments

Comments
 (0)