Skip to content

Commit

Permalink
Merge #4448
Browse files Browse the repository at this point in the history
4448: Generate configuration for launch.json r=vsrs a=vsrs

This PR adds two new commands: `"rust-analyzer.debug"` and `"rust-analyzer.newDebugConfig"`. The former is a supplement to the existing `"rust-analyzer.run"` command and works the same way: asks for a runnable and starts new debug session. The latter allows adding a new configuration to **launch.json** (or to update an existing one).

If the new option `"rust-analyzer.debug.useLaunchJson"` is set to true then `"rust-analyzer.debug"` and Debug Lens will first look for existing debug configuration in **launch.json**. That is, it has become possible to specify startup arguments, env variables, etc.

`"rust-analyzer.debug.useLaunchJson"` is false by default, but it might be worth making true the default value. Personally I prefer true, but I'm not sure if it is good for all value.

----
I think that this PR also solves #3441.
Both methods to update launch.json mentioned in the issue do not work:
1. Menu. It is only possible to add a launch.json configuration template via a debug adapter. And anyway it's only a template and it is impossible to specify arguments from an extension.

2. DebugConfigurationProvider. The exact opposite situation: it is possible to specify all debug session settings, but it is impossible to export these settings to launch.json.

Separate `"rust-analyzer.newDebugConfig"` command looks better for me.

----
Fixes #4450
Fixes #3441

Co-authored-by: vsrs <vit@conrlab.com>
Co-authored-by: vsrs <62505555+vsrs@users.noreply.github.com>
  • Loading branch information
3 people authored May 15, 2020
2 parents 982b92f + a4ecaa7 commit d51c1f6
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 101 deletions.
5 changes: 4 additions & 1 deletion crates/rust-analyzer/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,14 +1011,17 @@ fn to_lsp_runnable(
runnable: Runnable,
) -> Result<lsp_ext::Runnable> {
let spec = CargoTargetSpec::for_file(world, file_id)?;
let target = spec.as_ref().map(|s| s.target.clone());
let (args, extra_args) = CargoTargetSpec::runnable_args(spec, &runnable.kind)?;
let line_index = world.analysis().file_line_index(file_id)?;
let label = match &runnable.kind {
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
RunnableKind::TestMod { path } => format!("test-mod {}", path),
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id),
RunnableKind::Bin => "run binary".to_string(),
RunnableKind::Bin => {
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
}
};
Ok(lsp_ext::Runnable {
range: to_proto::range(&line_index, runnable.range),
Expand Down
10 changes: 10 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@
"title": "Run",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.debug",
"title": "Debug",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.newDebugConfig",
"title": "Generate launch configuration",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.analyzerStatus",
"title": "Status",
Expand Down
15 changes: 14 additions & 1 deletion editors/code/src/cargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ export class Cargo {
async executableFromArgs(args: readonly string[]): Promise<string> {
const cargoArgs = [...args, "--message-format=json"];

const artifacts = await this.artifactsFromArgs(cargoArgs);
// arguments for a runnable from the quick pick should be updated.
// see crates\rust-analyzer\src\main_loop\handlers.rs, handle_code_lens
if (cargoArgs[0] === "run") {
cargoArgs[0] = "build";
} else if (cargoArgs.indexOf("--no-run") === -1) {
cargoArgs.push("--no-run");
}

let artifacts = await this.artifactsFromArgs(cargoArgs);
if (cargoArgs[0] === "test") {
// for instance, `crates\rust-analyzer\tests\heavy_tests\main.rs` tests
// produce 2 artifacts: {"kind": "bin"} and {"kind": "test"}
artifacts = artifacts.filter(a => a.isTest);
}

if (artifacts.length === 0) {
throw new Error('No compilation artifacts');
Expand Down
201 changes: 103 additions & 98 deletions editors/code/src/commands/runnables.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,82 @@
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import * as ra from '../rust-analyzer-api';
import * as os from "os";

import { Ctx, Cmd } from '../ctx';
import { Cargo } from '../cargo';
import { startDebugSession, getDebugConfiguration } from '../debug';

export function run(ctx: Ctx): Cmd {
let prevRunnable: RunnableQuickPick | undefined;
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];

return async () => {
const editor = ctx.activeRustEditor;
const client = ctx.client;
if (!editor || !client) return;
async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise<RunnableQuickPick | undefined> {
const editor = ctx.activeRustEditor;
const client = ctx.client;
if (!editor || !client) return;

const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(),
};
const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(),
};

const runnables = await client.sendRequest(ra.runnables, {
textDocument,
position: client.code2ProtocolConverter.asPosition(
editor.selection.active,
),
});
const items: RunnableQuickPick[] = [];
if (prevRunnable) {
items.push(prevRunnable);
const runnables = await client.sendRequest(ra.runnables, {
textDocument,
position: client.code2ProtocolConverter.asPosition(
editor.selection.active,
),
});
const items: RunnableQuickPick[] = [];
if (prevRunnable) {
items.push(prevRunnable);
}
for (const r of runnables) {
if (
prevRunnable &&
JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
) {
continue;
}
for (const r of runnables) {
if (
prevRunnable &&
JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)
) {
continue;
}
items.push(new RunnableQuickPick(r));
items.push(new RunnableQuickPick(r));
}

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<RunnableQuickPick>();
quickPick.items = items;
quickPick.title = "Select Runnable";
if (showButtons) {
quickPick.buttons = quickPickButtons;
}
const item = await vscode.window.showQuickPick(items);
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 {
let prevRunnable: RunnableQuickPick | undefined;

return async () => {
const item = await selectRunnable(ctx, prevRunnable);
if (!item) return;

item.detail = 'rerun';
Expand All @@ -64,88 +103,54 @@ export function runSingle(ctx: Ctx): Cmd {
};
}

function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
return {
type: "lldb",
request: "launch",
name: config.label,
program: executable,
args: config.extraArgs,
cwd: config.cwd,
sourceMap: sourceFileMap,
sourceLanguages: ["rust"]
export function debug(ctx: Ctx): Cmd {
let prevDebuggee: RunnableQuickPick | undefined;

return async () => {
const item = await selectRunnable(ctx, prevDebuggee);
if (!item) return;

item.detail = 'restart';
prevDebuggee = item;
return await startDebugSession(ctx, item.runnable);
};
}

function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
return {
type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg',
request: "launch",
name: config.label,
program: executable,
args: config.extraArgs,
cwd: config.cwd,
sourceFileMap: sourceFileMap,
export function debugSingle(ctx: Ctx): Cmd {
return async (config: ra.Runnable) => {
await startDebugSession(ctx, config);
};
}

const debugOutput = vscode.window.createOutputChannel("Debug");

async function getDebugExecutable(config: ra.Runnable): Promise<string> {
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;
}
async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise<void> {
const scope = ctx.activeRustEditor?.document.uri;
if (!scope) return;

type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
if (!debugConfig) return;

export function debugSingle(ctx: Ctx): Cmd {
return async (config: ra.Runnable) => {
const editor = ctx.activeRustEditor;
if (!editor) return;
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
const configurations = wsLaunchSection.get<any[]>("configurations") || [];

const knownEngines: Record<string, DebugConfigProvider> = {
"vadimcn.vscode-lldb": getLldbDebugConfig,
"ms-vscode.cpptools": getCppvsDebugConfig
};
const debugOptions = ctx.config.debug;

let debugEngine = null;
if (debugOptions.engine === "auto") {
for (var engineId in knownEngines) {
debugEngine = vscode.extensions.getExtension(engineId);
if (debugEngine) break;
}
}
else {
debugEngine = vscode.extensions.getExtension(debugOptions.engine);
}
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;

if (!debugEngine) {
vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)`
+ ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`);
return;
}
configurations[index] = debugConfig;
} else {
configurations.push(debugConfig);
}

debugOutput.clear();
if (ctx.config.debug.openUpDebugPane) {
debugOutput.show(true);
}
await wsLaunchSection.update("configurations", configurations);
}

const executable = await getDebugExecutable(config);
const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap);
if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
for (var key in settingsMap) {
debugConfig[key] = settingsMap[key];
}
}
export function newDebugConfig(ctx: Ctx): Cmd {
return async () => {
const item = await selectRunnable(ctx, undefined, false);
if (!item) return;

debugOutput.appendLine("Launching debug configuration:");
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
return vscode.debug.startDebugging(undefined, debugConfig);
await makeDebugConfig(ctx, item);
};
}

Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Config {
engine: this.get<string>("debug.engine"),
engineSettings: this.get<object>("debug.engineSettings"),
openUpDebugPane: this.get<boolean>("debug.openUpDebugPane"),
sourceFileMap: sourceFileMap,
sourceFileMap: sourceFileMap
};
}
}
Loading

0 comments on commit d51c1f6

Please sign in to comment.