aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/commands/runnables.ts82
1 files changed, 61 insertions, 21 deletions
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
index 5e88eeae0..b1d93fc34 100644
--- a/editors/code/src/commands/runnables.ts
+++ b/editors/code/src/commands/runnables.ts
@@ -5,7 +5,9 @@ import * as ra from '../rust-analyzer-api';
5import { Ctx, Cmd } from '../ctx'; 5import { Ctx, Cmd } from '../ctx';
6import { startDebugSession, getDebugConfiguration } from '../debug'; 6import { startDebugSession, getDebugConfiguration } from '../debug';
7 7
8async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise<RunnableQuickPick | undefined> { 8const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
9
10async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise<RunnableQuickPick | undefined> {
9 const editor = ctx.activeRustEditor; 11 const editor = ctx.activeRustEditor;
10 const client = ctx.client; 12 const client = ctx.client;
11 if (!editor || !client) return; 13 if (!editor || !client) return;
@@ -33,7 +35,41 @@ async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefi
33 } 35 }
34 items.push(new RunnableQuickPick(r)); 36 items.push(new RunnableQuickPick(r));
35 } 37 }
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 });
37} 73}
38 74
39export function run(ctx: Ctx): Cmd { 75export function run(ctx: Ctx): Cmd {
@@ -86,31 +122,35 @@ export function debugSingle(ctx: Ctx): Cmd {
86 }; 122 };
87} 123}
88 124
89export function newDebugConfig(ctx: Ctx): Cmd { 125async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise<void> {
90 return async () => { 126 const scope = ctx.activeRustEditor?.document.uri;
91 const scope = ctx.activeRustEditor?.document.uri; 127 if (!scope) return;
92 if (!scope) return;
93 128
94 const item = await selectRunnable(ctx, undefined); 129 const debugConfig = await getDebugConfiguration(ctx, item.runnable);
95 if (!item) return; 130 if (!debugConfig) return;
96 131
97 const debugConfig = await getDebugConfiguration(ctx, item.runnable); 132 const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
98 if (!debugConfig) return; 133 const configurations = wsLaunchSection.get<any[]>("configurations") || [];
99 134
100 const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); 135 const index = configurations.findIndex(c => c.name === debugConfig.name);
101 const configurations = wsLaunchSection.get<any[]>("configurations") || []; 136 if (index !== -1) {
137 const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
138 if (answer === "Cancel") return;
102 139
103 const index = configurations.findIndex(c => c.name === debugConfig.name); 140 configurations[index] = debugConfig;
104 if (index !== -1) { 141 } else {
105 const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); 142 configurations.push(debugConfig);
106 if (answer === "Cancel") return; 143 }
107 144
108 configurations[index] = debugConfig; 145 await wsLaunchSection.update("configurations", configurations);
109 } else { 146}
110 configurations.push(debugConfig); 147
111 } 148export function newDebugConfig(ctx: Ctx): Cmd {
149 return async () => {
150 const item = await selectRunnable(ctx, undefined, false);
151 if (!item) return;
112 152
113 await wsLaunchSection.update("configurations", configurations); 153 await makeDebugConfig(ctx, item);
114 }; 154 };
115} 155}
116 156