From be9b0609d55f9f49e4473b4ab2bc55583974fc2f Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 14 May 2020 13:22:52 +0300 Subject: Runnable quick pick with buttons --- editors/code/src/commands/runnables.ts | 82 +++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 21 deletions(-) (limited to 'editors/code/src/commands/runnables.ts') 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'; import { Ctx, Cmd } from '../ctx'; import { startDebugSession, getDebugConfiguration } from '../debug'; -async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise { +const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; + +async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise { const editor = ctx.activeRustEditor; const client = ctx.client; if (!editor || !client) return; @@ -33,7 +35,41 @@ async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefi } items.push(new RunnableQuickPick(r)); } - return await vscode.window.showQuickPick(items); + + return await new Promise((resolve) => { + const disposables: vscode.Disposable[] = []; + const close = (result?: RunnableQuickPick) => { + resolve(result); + disposables.forEach(d => d.dispose()); + }; + + const quickPick = vscode.window.createQuickPick(); + quickPick.items = items; + quickPick.title = "Select Runnable"; + if (showButtons) { + quickPick.buttons = quickPickButtons; + } + disposables.push( + quickPick.onDidHide(() => close()), + quickPick.onDidAccept(() => close(quickPick.selectedItems[0])), + quickPick.onDidTriggerButton((_button) => { + (async () => await makeDebugConfig(ctx, quickPick.activeItems[0]))(); + close(); + }), + quickPick.onDidChangeActive((active) => { + if (showButtons && active.length > 0) { + if (active[0].label.startsWith('cargo')) { + // save button makes no sense for `cargo test` or `cargo check` + quickPick.buttons = []; + } else if (quickPick.buttons.length === 0) { + quickPick.buttons = quickPickButtons; + } + } + }), + quickPick + ); + quickPick.show(); + }); } export function run(ctx: Ctx): Cmd { @@ -86,31 +122,35 @@ export function debugSingle(ctx: Ctx): Cmd { }; } -export function newDebugConfig(ctx: Ctx): Cmd { - return async () => { - const scope = ctx.activeRustEditor?.document.uri; - if (!scope) return; +async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise { + const scope = ctx.activeRustEditor?.document.uri; + if (!scope) return; - const item = await selectRunnable(ctx, undefined); - if (!item) return; + const debugConfig = await getDebugConfiguration(ctx, item.runnable); + if (!debugConfig) return; - const debugConfig = await getDebugConfiguration(ctx, item.runnable); - if (!debugConfig) return; + const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); + const configurations = wsLaunchSection.get("configurations") || []; - const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); - const configurations = wsLaunchSection.get("configurations") || []; + const index = configurations.findIndex(c => c.name === debugConfig.name); + if (index !== -1) { + const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); + if (answer === "Cancel") return; - const index = configurations.findIndex(c => c.name === debugConfig.name); - if (index !== -1) { - const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); - if (answer === "Cancel") return; + configurations[index] = debugConfig; + } else { + configurations.push(debugConfig); + } - configurations[index] = debugConfig; - } else { - configurations.push(debugConfig); - } + await wsLaunchSection.update("configurations", configurations); +} + +export function newDebugConfig(ctx: Ctx): Cmd { + return async () => { + const item = await selectRunnable(ctx, undefined, false); + if (!item) return; - await wsLaunchSection.update("configurations", configurations); + await makeDebugConfig(ctx, item); }; } -- cgit v1.2.3