Skip to content

Commit be9b060

Browse files
committed
Runnable quick pick with buttons
1 parent 3ffc26e commit be9b060

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

editors/code/src/commands/runnables.ts

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import * as ra from '../rust-analyzer-api';
55
import { Ctx, Cmd } from '../ctx';
66
import { startDebugSession, getDebugConfiguration } from '../debug';
77

8-
async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise<RunnableQuickPick | undefined> {
8+
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
9+
10+
async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise<RunnableQuickPick | undefined> {
911
const editor = ctx.activeRustEditor;
1012
const client = ctx.client;
1113
if (!editor || !client) return;
@@ -33,7 +35,41 @@ async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefi
3335
}
3436
items.push(new RunnableQuickPick(r));
3537
}
36-
return await vscode.window.showQuickPick(items);
38+
39+
return await new Promise((resolve) => {
40+
const disposables: vscode.Disposable[] = [];
41+
const close = (result?: RunnableQuickPick) => {
42+
resolve(result);
43+
disposables.forEach(d => d.dispose());
44+
};
45+
46+
const quickPick = vscode.window.createQuickPick<RunnableQuickPick>();
47+
quickPick.items = items;
48+
quickPick.title = "Select Runnable";
49+
if (showButtons) {
50+
quickPick.buttons = quickPickButtons;
51+
}
52+
disposables.push(
53+
quickPick.onDidHide(() => close()),
54+
quickPick.onDidAccept(() => close(quickPick.selectedItems[0])),
55+
quickPick.onDidTriggerButton((_button) => {
56+
(async () => await makeDebugConfig(ctx, quickPick.activeItems[0]))();
57+
close();
58+
}),
59+
quickPick.onDidChangeActive((active) => {
60+
if (showButtons && active.length > 0) {
61+
if (active[0].label.startsWith('cargo')) {
62+
// save button makes no sense for `cargo test` or `cargo check`
63+
quickPick.buttons = [];
64+
} else if (quickPick.buttons.length === 0) {
65+
quickPick.buttons = quickPickButtons;
66+
}
67+
}
68+
}),
69+
quickPick
70+
);
71+
quickPick.show();
72+
});
3773
}
3874

3975
export function run(ctx: Ctx): Cmd {
@@ -86,31 +122,35 @@ export function debugSingle(ctx: Ctx): Cmd {
86122
};
87123
}
88124

89-
export function newDebugConfig(ctx: Ctx): Cmd {
90-
return async () => {
91-
const scope = ctx.activeRustEditor?.document.uri;
92-
if (!scope) return;
125+
async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise<void> {
126+
const scope = ctx.activeRustEditor?.document.uri;
127+
if (!scope) return;
93128

94-
const item = await selectRunnable(ctx, undefined);
95-
if (!item) return;
129+
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
130+
if (!debugConfig) return;
96131

97-
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
98-
if (!debugConfig) return;
132+
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
133+
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
99134

100-
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
101-
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
135+
const index = configurations.findIndex(c => c.name === debugConfig.name);
136+
if (index !== -1) {
137+
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
138+
if (answer === "Cancel") return;
102139

103-
const index = configurations.findIndex(c => c.name === debugConfig.name);
104-
if (index !== -1) {
105-
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
106-
if (answer === "Cancel") return;
140+
configurations[index] = debugConfig;
141+
} else {
142+
configurations.push(debugConfig);
143+
}
107144

108-
configurations[index] = debugConfig;
109-
} else {
110-
configurations.push(debugConfig);
111-
}
145+
await wsLaunchSection.update("configurations", configurations);
146+
}
147+
148+
export function newDebugConfig(ctx: Ctx): Cmd {
149+
return async () => {
150+
const item = await selectRunnable(ctx, undefined, false);
151+
if (!item) return;
112152

113-
await wsLaunchSection.update("configurations", configurations);
153+
await makeDebugConfig(ctx, item);
114154
};
115155
}
116156

0 commit comments

Comments
 (0)