From 97166e2ad9307b3f4cca33d2c82149be9eb5a633 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 10 Feb 2021 14:28:13 +0300 Subject: Add **Copy Run Command Line** command for vscode This is useful when you want to, e.g., run a specific test in a terminal with `--release`. --- editors/code/package.json | 5 +++++ editors/code/src/commands.ts | 14 +++++++++++++- editors/code/src/main.ts | 1 + editors/code/src/run.ts | 19 ++++++++++++------- 4 files changed, 31 insertions(+), 8 deletions(-) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index e5d439050..55825456e 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -133,6 +133,11 @@ "title": "Run", "category": "Rust Analyzer" }, + { + "command": "rust-analyzer.copyRunCommandLine", + "title": "Copy Run Command Line", + "category": "Rust Analyzer" + }, { "command": "rust-analyzer.debug", "title": "Debug", diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index cbda619ea..e3a23c0d2 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -5,7 +5,7 @@ import * as ra from './lsp_ext'; import { Ctx, Cmd } from './ctx'; import { applySnippetWorkspaceEdit, applySnippetTextEdits } from './snippets'; import { spawnSync } from 'child_process'; -import { RunnableQuickPick, selectRunnable, createTask } from './run'; +import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run'; import { AstInspector } from './ast_inspector'; import { isRustDocument, sleep, isRustEditor } from './util'; import { startDebugSession, makeDebugConfig } from './debug'; @@ -572,6 +572,18 @@ export function runSingle(ctx: Ctx): Cmd { }; } +export function copyRunCommandLine(ctx: Ctx) { + let prevRunnable: RunnableQuickPick | undefined; + return async () => { + const item = await selectRunnable(ctx, prevRunnable); + if (!item) return; + const args = createArgs(item.runnable); + const commandLine = ["cargo", ...args].join(" "); + await vscode.env.clipboard.writeText(commandLine); + await vscode.window.showInformationMessage("Cargo invocation copied to the clipboard."); + }; +} + export function debug(ctx: Ctx): Cmd { let prevDebuggee: RunnableQuickPick | undefined; diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 1900d900a..43cae5c7f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -108,6 +108,7 @@ async function tryActivate(context: vscode.ExtensionContext) { ctx.registerCommand('viewHir', commands.viewHir); ctx.registerCommand('expandMacro', commands.expandMacro); ctx.registerCommand('run', commands.run); + ctx.registerCommand('copyRunCommandLine', commands.copyRunCommandLine); ctx.registerCommand('debug', commands.debug); ctx.registerCommand('newDebugConfig', commands.newDebugConfig); ctx.registerCommand('openDocs', commands.openDocs); diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts index 17573cd82..f42baed16 100644 --- a/editors/code/src/run.ts +++ b/editors/code/src/run.ts @@ -128,13 +128,7 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise throw `Unexpected runnable kind: ${runnable.kind}`; } - const args = [...runnable.args.cargoArgs]; // should be a copy! - if (runnable.args.cargoExtraArgs) { - args.push(...runnable.args.cargoExtraArgs); // Append user-specified cargo options. - } - if (runnable.args.executableArgs.length > 0) { - args.push('--', ...runnable.args.executableArgs); - } + const args = createArgs(runnable); const definition: tasks.CargoTaskDefinition = { type: tasks.TASK_TYPE, @@ -151,3 +145,14 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise return cargoTask; } + +export function createArgs(runnable: ra.Runnable): string[] { + const args = [...runnable.args.cargoArgs]; // should be a copy! + if (runnable.args.cargoExtraArgs) { + args.push(...runnable.args.cargoExtraArgs); // Append user-specified cargo options. + } + if (runnable.args.executableArgs.length > 0) { + args.push('--', ...runnable.args.executableArgs); + } + return args; +} -- cgit v1.2.3