diff --git a/README.md b/README.md index 946ed46..29c420f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/hdevalke/rust-test-lens.svg?branch=master)](https://travis-ci.org/hdevalke/rust-test-lens) -A code lens to debug test in rust source files. +A code lens to debug rust tests and binaries. ![test lens](images/test_codelens.png) @@ -14,4 +14,8 @@ Depends on the LLDB extension [vscode-lldb](https://marketplace.visualstudio.com This extension can be enabled/disabled using the following setting: -* `rust_test_lens.enable`: true/false to enable/disable this extension +* `rust-test-lens.enable`: true/false to enable/disable this extension +* `rust-test-lens.args.main`: An array of strings with the application arguments. +* `rust-test-lens.args.tests`: An array of strings with extra arguments. + Defaults to `--no-capture`. + Run `cargo test -- --help` to see all possible options. diff --git a/package-lock.json b/package-lock.json index d6c5b96..35976a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rust-test-lens", - "version": "0.2.0", + "version": "0.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 56b124a..69d3560 100644 --- a/package.json +++ b/package.json @@ -57,12 +57,24 @@ ], "configuration": { "type": "object", - "title": "Rust Test Lens configuration", + "title": "Rust Test Lens", "properties": { "rust-test-lens.enabled": { "type": "boolean", "default": true, "description": "Enable or disable this extension" + }, + "rust-test-lens.args.main": { + "type": "array", + "default": [], + "description": "Enable or disable this extension" + }, + "rust-test-lens.args.tests": { + "type": "array", + "default": [ + "--nocapture" + ], + "description": "Enable or disable this extension" } } } diff --git a/src/RustCodeLensProvider.ts b/src/RustCodeLensProvider.ts index ab85918..f1d725f 100644 --- a/src/RustCodeLensProvider.ts +++ b/src/RustCodeLensProvider.ts @@ -15,7 +15,9 @@ import { basename, dirname } from "path"; export class RustCodeLensProvider implements CodeLensProvider { constructor( private _onDidChange: EventEmitter, - private rustTests: RustTests + private rustTests: RustTests, + private main_args: string[], + private test_args: string[] ) {} get onDidChangeCodeLenses(): Event { @@ -35,6 +37,11 @@ export class RustCodeLensProvider implements CodeLensProvider { return lenses; } + public update_args(main_args: string[], test_args: string[]) { + this.main_args = main_args; + this.test_args = test_args; + } + private mainMethodLenses(doc: TextDocument): any { const text = doc.getText(); const reFnMain = /fn\s+(main)\s*\(\s*\)/g; @@ -90,10 +97,10 @@ export class RustCodeLensProvider implements CodeLensProvider { ): DebugConfiguration | undefined { const pkg = this.rustTests.getPackage(fn, doc.uri); if (pkg) { - const args = - fn === "main" - ? ["build", `--package=${pkg.name}`] - : ["test", "--no-run", `--package=${pkg.name}`]; + const is_main = fn === "main"; + const args = is_main + ? ["build", `--package=${pkg.name}`] + : ["test", "--no-run", `--package=${pkg.name}`]; const bin = this.rustTests.getBin(doc.fileName, pkg); const filter = this.rustTests.getFilter(doc.fileName, pkg, bin); @@ -106,7 +113,7 @@ export class RustCodeLensProvider implements CodeLensProvider { } args.push(`--manifest-path=${pkg.manifest_path}`); - + const extra_args = is_main ? this.main_args : [fn, ...this.test_args]; return { type: "lldb", request: "launch", @@ -115,7 +122,7 @@ export class RustCodeLensProvider implements CodeLensProvider { args: args, filter: filter }, - args: [fn], + args: extra_args, cwd: `${dirname(pkg.manifest_path)}` }; } diff --git a/src/extension.ts b/src/extension.ts index e1c5785..c69adf8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -8,7 +8,8 @@ import { debug, OutputChannel, workspace, - WorkspaceFolder + WorkspaceFolder, + Disposable } from "vscode"; import { RustCodeLensProvider } from "./RustCodeLensProvider"; import { RustTests } from "./RustTests"; @@ -36,27 +37,30 @@ export async function activate(context: ExtensionContext) { } if (metaMap.size !== 0) { const rustTests: RustTests = new RustTests(metaMap); + const main_args = config.get("args.main", []); + const test_args = config.get("args.tests", ["--nocapture"]); const codeLensProvider = new RustCodeLensProvider( onDidChange, - rustTests + rustTests, + main_args, + test_args ); - context.subscriptions.push( - languages.registerCodeLensProvider( - { scheme: "file", language: "rust" }, - codeLensProvider - ) + const disposable = languages.registerCodeLensProvider( + { scheme: "file", language: "rust" }, + codeLensProvider ); - let disposable = commands.registerCommand( - "extension.debugTest", - debugConfig => { - const json = JSON.stringify(debugConfig, null, 2); - outputChannel.appendLine(`Debugging: ${json}`); - debug - .startDebugging(undefined, debugConfig) - .then(r => console.log("Result", r)); - } + + context.subscriptions.push( + workspace.onDidChangeConfiguration(e => { + const config = workspace.getConfiguration("rust-test-lens"); + const main_args: string[] = config.get("args.main", []); + const test_args = config.get("args.tests", ["--nocapture"]); + codeLensProvider.update_args(main_args, test_args); + }) ); + context.subscriptions.push(disposable); + context.subscriptions.push(registerCmdDebugTest()); } } else { outputChannel.append( @@ -66,5 +70,15 @@ export async function activate(context: ExtensionContext) { } } +function registerCmdDebugTest(): Disposable { + return commands.registerCommand("extension.debugTest", debugConfig => { + const json = JSON.stringify(debugConfig, null, 2); + outputChannel.appendLine(`Debugging: ${json}`); + debug + .startDebugging(undefined, debugConfig) + .then(r => console.log("Result", r)); + }); +} + // this method is called when your extension is deactivated export function deactivate() {}