mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Move run commands to commands.ts
This commit is contained in:
parent
131ccd9540
commit
0ced18eee0
3 changed files with 138 additions and 137 deletions
|
@ -8,6 +8,7 @@ import { spawnSync } from 'child_process';
|
||||||
import { RunnableQuickPick, selectRunnable, createTask } from './run';
|
import { RunnableQuickPick, selectRunnable, createTask } from './run';
|
||||||
import { AstInspector } from './ast_inspector';
|
import { AstInspector } from './ast_inspector';
|
||||||
import { isRustDocument, sleep, isRustEditor } from './util';
|
import { isRustDocument, sleep, isRustEditor } from './util';
|
||||||
|
import { startDebugSession, makeDebugConfig } from './debug';
|
||||||
|
|
||||||
export * from './ast_inspector';
|
export * from './ast_inspector';
|
||||||
export * from './run';
|
export * from './run';
|
||||||
|
@ -197,20 +198,6 @@ export function toggleInlayHints(ctx: Ctx): Cmd {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function run(ctx: Ctx): Cmd {
|
|
||||||
let prevRunnable: RunnableQuickPick | undefined;
|
|
||||||
|
|
||||||
return async () => {
|
|
||||||
const item = await selectRunnable(ctx, prevRunnable);
|
|
||||||
if (!item) return;
|
|
||||||
|
|
||||||
item.detail = 'rerun';
|
|
||||||
prevRunnable = item;
|
|
||||||
const task = createTask(item.runnable);
|
|
||||||
return await vscode.tasks.executeTask(task);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Opens the virtual file that will show the syntax tree
|
// Opens the virtual file that will show the syntax tree
|
||||||
//
|
//
|
||||||
// The contents of the file come from the `TextDocumentContentProvider`
|
// The contents of the file come from the `TextDocumentContentProvider`
|
||||||
|
@ -368,3 +355,62 @@ export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd {
|
||||||
await applySnippetWorkspaceEdit(edit);
|
await applySnippetWorkspaceEdit(edit);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function run(ctx: Ctx): Cmd {
|
||||||
|
let prevRunnable: RunnableQuickPick | undefined;
|
||||||
|
|
||||||
|
return async () => {
|
||||||
|
const item = await selectRunnable(ctx, prevRunnable);
|
||||||
|
if (!item) return;
|
||||||
|
|
||||||
|
item.detail = 'rerun';
|
||||||
|
prevRunnable = item;
|
||||||
|
const task = createTask(item.runnable);
|
||||||
|
return await vscode.tasks.executeTask(task);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function runSingle(ctx: Ctx): Cmd {
|
||||||
|
return async (runnable: ra.Runnable) => {
|
||||||
|
const editor = ctx.activeRustEditor;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const task = createTask(runnable);
|
||||||
|
task.group = vscode.TaskGroup.Build;
|
||||||
|
task.presentationOptions = {
|
||||||
|
reveal: vscode.TaskRevealKind.Always,
|
||||||
|
panel: vscode.TaskPanelKind.Dedicated,
|
||||||
|
clear: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return vscode.tasks.executeTask(task);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function debug(ctx: Ctx): Cmd {
|
||||||
|
let prevDebuggee: RunnableQuickPick | undefined;
|
||||||
|
|
||||||
|
return async () => {
|
||||||
|
const item = await selectRunnable(ctx, prevDebuggee, true);
|
||||||
|
if (!item) return;
|
||||||
|
|
||||||
|
item.detail = 'restart';
|
||||||
|
prevDebuggee = item;
|
||||||
|
return await startDebugSession(ctx, item.runnable);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function debugSingle(ctx: Ctx): Cmd {
|
||||||
|
return async (config: ra.Runnable) => {
|
||||||
|
await startDebugSession(ctx, config);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function newDebugConfig(ctx: Ctx): Cmd {
|
||||||
|
return async () => {
|
||||||
|
const item = await selectRunnable(ctx, undefined, true, false);
|
||||||
|
if (!item) return;
|
||||||
|
|
||||||
|
await makeDebugConfig(ctx, item.runnable);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -9,40 +9,53 @@ import { Ctx } from "./ctx";
|
||||||
const debugOutput = vscode.window.createOutputChannel("Debug");
|
const debugOutput = vscode.window.createOutputChannel("Debug");
|
||||||
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
|
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
|
||||||
|
|
||||||
function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
|
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
|
||||||
return {
|
const scope = ctx.activeRustEditor?.document.uri;
|
||||||
type: "lldb",
|
if (!scope) return;
|
||||||
request: "launch",
|
|
||||||
name: config.label,
|
const debugConfig = await getDebugConfiguration(ctx, runnable);
|
||||||
program: executable,
|
if (!debugConfig) return;
|
||||||
args: config.extraArgs,
|
|
||||||
cwd: config.cwd,
|
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
|
||||||
sourceMap: sourceFileMap,
|
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
|
||||||
sourceLanguages: ["rust"]
|
|
||||||
};
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
await wsLaunchSection.update("configurations", configurations);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
|
export async function startDebugSession(ctx: Ctx, runnable: ra.Runnable): Promise<boolean> {
|
||||||
return {
|
let debugConfig: vscode.DebugConfiguration | undefined = undefined;
|
||||||
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
|
let message = "";
|
||||||
request: "launch",
|
|
||||||
name: config.label,
|
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
|
||||||
program: executable,
|
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
|
||||||
args: config.extraArgs,
|
|
||||||
cwd: config.cwd,
|
const index = configurations.findIndex(c => c.name === runnable.label);
|
||||||
sourceFileMap: sourceFileMap,
|
if (-1 !== index) {
|
||||||
};
|
debugConfig = configurations[index];
|
||||||
|
message = " (from launch.json)";
|
||||||
|
debugOutput.clear();
|
||||||
|
} else {
|
||||||
|
debugConfig = await getDebugConfiguration(ctx, runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!debugConfig) return false;
|
||||||
|
|
||||||
|
debugOutput.appendLine(`Launching debug configuration${message}:`);
|
||||||
|
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
|
||||||
|
return vscode.debug.startDebugging(undefined, debugConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDebugExecutable(config: ra.Runnable): Promise<string> {
|
async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> {
|
||||||
const cargo = new Cargo(config.cwd || '.', debugOutput);
|
|
||||||
const executable = await cargo.executableFromArgs(config.args);
|
|
||||||
|
|
||||||
// if we are here, there were no compilation errors.
|
|
||||||
return executable;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> {
|
|
||||||
const editor = ctx.activeRustEditor;
|
const editor = ctx.activeRustEditor;
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
|
@ -78,8 +91,8 @@ export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Prom
|
||||||
return path.normalize(p).replace(wsFolder, '${workspaceRoot}');
|
return path.normalize(p).replace(wsFolder, '${workspaceRoot}');
|
||||||
}
|
}
|
||||||
|
|
||||||
const executable = await getDebugExecutable(config);
|
const executable = await getDebugExecutable(runnable);
|
||||||
const debugConfig = knownEngines[debugEngine.id](config, simplifyPath(executable), debugOptions.sourceFileMap);
|
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), debugOptions.sourceFileMap);
|
||||||
if (debugConfig.type in debugOptions.engineSettings) {
|
if (debugConfig.type in debugOptions.engineSettings) {
|
||||||
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
|
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
|
||||||
for (var key in settingsMap) {
|
for (var key in settingsMap) {
|
||||||
|
@ -100,25 +113,35 @@ export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Prom
|
||||||
return debugConfig;
|
return debugConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function startDebugSession(ctx: Ctx, config: ra.Runnable): Promise<boolean> {
|
async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
|
||||||
let debugConfig: vscode.DebugConfiguration | undefined = undefined;
|
const cargo = new Cargo(runnable.cwd || '.', debugOutput);
|
||||||
let message = "";
|
const executable = await cargo.executableFromArgs(runnable.args);
|
||||||
|
|
||||||
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
|
// if we are here, there were no compilation errors.
|
||||||
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
|
return executable;
|
||||||
|
}
|
||||||
const index = configurations.findIndex(c => c.name === config.label);
|
|
||||||
if (-1 !== index) {
|
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
|
||||||
debugConfig = configurations[index];
|
return {
|
||||||
message = " (from launch.json)";
|
type: "lldb",
|
||||||
debugOutput.clear();
|
request: "launch",
|
||||||
} else {
|
name: runnable.label,
|
||||||
debugConfig = await getDebugConfiguration(ctx, config);
|
program: executable,
|
||||||
}
|
args: runnable.extraArgs,
|
||||||
|
cwd: runnable.cwd,
|
||||||
if (!debugConfig) return false;
|
sourceMap: sourceFileMap,
|
||||||
|
sourceLanguages: ["rust"]
|
||||||
debugOutput.appendLine(`Launching debug configuration${message}:`);
|
};
|
||||||
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
|
}
|
||||||
return vscode.debug.startDebugging(undefined, debugConfig);
|
|
||||||
|
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
|
||||||
|
return {
|
||||||
|
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
|
||||||
|
request: "launch",
|
||||||
|
name: runnable.label,
|
||||||
|
program: executable,
|
||||||
|
args: runnable.extraArgs,
|
||||||
|
cwd: runnable.cwd,
|
||||||
|
sourceFileMap: sourceFileMap,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,8 @@ import * as lc from 'vscode-languageclient';
|
||||||
import * as ra from './lsp_ext';
|
import * as ra from './lsp_ext';
|
||||||
import * as toolchain from "./toolchain";
|
import * as toolchain from "./toolchain";
|
||||||
|
|
||||||
import { Ctx, Cmd } from './ctx';
|
import { Ctx } from './ctx';
|
||||||
import { startDebugSession, getDebugConfiguration } from './debug';
|
import { makeDebugConfig } from './debug';
|
||||||
|
|
||||||
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
|
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ export async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick,
|
||||||
quickPick.onDidHide(() => close()),
|
quickPick.onDidHide(() => close()),
|
||||||
quickPick.onDidAccept(() => close(quickPick.selectedItems[0])),
|
quickPick.onDidAccept(() => close(quickPick.selectedItems[0])),
|
||||||
quickPick.onDidTriggerButton((_button) => {
|
quickPick.onDidTriggerButton((_button) => {
|
||||||
(async () => await makeDebugConfig(ctx, quickPick.activeItems[0]))();
|
(async () => await makeDebugConfig(ctx, quickPick.activeItems[0].runnable))();
|
||||||
close();
|
close();
|
||||||
}),
|
}),
|
||||||
quickPick.onDidChangeActive((active) => {
|
quickPick.onDidChangeActive((active) => {
|
||||||
|
@ -84,74 +84,6 @@ export async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function runSingle(ctx: Ctx): Cmd {
|
|
||||||
return async (runnable: ra.Runnable) => {
|
|
||||||
const editor = ctx.activeRustEditor;
|
|
||||||
if (!editor) return;
|
|
||||||
|
|
||||||
const task = createTask(runnable);
|
|
||||||
task.group = vscode.TaskGroup.Build;
|
|
||||||
task.presentationOptions = {
|
|
||||||
reveal: vscode.TaskRevealKind.Always,
|
|
||||||
panel: vscode.TaskPanelKind.Dedicated,
|
|
||||||
clear: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
return vscode.tasks.executeTask(task);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function debug(ctx: Ctx): Cmd {
|
|
||||||
let prevDebuggee: RunnableQuickPick | undefined;
|
|
||||||
|
|
||||||
return async () => {
|
|
||||||
const item = await selectRunnable(ctx, prevDebuggee, true);
|
|
||||||
if (!item) return;
|
|
||||||
|
|
||||||
item.detail = 'restart';
|
|
||||||
prevDebuggee = item;
|
|
||||||
return await startDebugSession(ctx, item.runnable);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function debugSingle(ctx: Ctx): Cmd {
|
|
||||||
return async (config: ra.Runnable) => {
|
|
||||||
await startDebugSession(ctx, config);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise<void> {
|
|
||||||
const scope = ctx.activeRustEditor?.document.uri;
|
|
||||||
if (!scope) return;
|
|
||||||
|
|
||||||
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
|
|
||||||
if (!debugConfig) return;
|
|
||||||
|
|
||||||
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
|
|
||||||
const configurations = wsLaunchSection.get<any[]>("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;
|
|
||||||
|
|
||||||
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, true, false);
|
|
||||||
if (!item) return;
|
|
||||||
|
|
||||||
await makeDebugConfig(ctx, item);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class RunnableQuickPick implements vscode.QuickPickItem {
|
export class RunnableQuickPick implements vscode.QuickPickItem {
|
||||||
public label: string;
|
public label: string;
|
||||||
public description?: string | undefined;
|
public description?: string | undefined;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue