Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Commit

Permalink
Add debug code lens for main functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalke committed Mar 17, 2019
1 parent 19e453b commit 2277c68
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: node_js
node_js:
- '8'
sudo: false
os:
- linux
before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
sh -e /etc/init.d/xvfb start; sleep 3; fi
install:
- npm install
- "./node_modules/.bin/vsce package"
script:
- npm test --silent
deploy:
provider: releases
api_key:
secure: Cubt88tWEdFNNIcY3yfcyOJiKFGkKzuDtZukRSGoubKZ40iHEXL0T5tbuMQ7AL+Gy43JjhymXjmLXnQ1avks99KJ2v30V7b82JaamGlK6jxvC4dD5CmkcT5wfpbVItwVnK4l/LrsAmEvhi8sUfLfS8p22eVTUITQk9zlxxkWw5NOCYbMzsNSGTsbsGf5KWAxKEWBseLztZsNQQq3Xfm5rK+pTd6cG1I3uOFZnGt225ztTQf3rjB4CqSO0mjMTh186jghQatAhYGhId0lMg9hK9TpugDCGbhZqGErs5YRAi4/3Bx+f3rHBUV3QfewGS4IxfqM7AeWhcaaWa5pbKSXFZkBbrWWQMLIhBtuljxuaCD/52HucA72RK02nkUXqXnP+slKbXyeP7xfsJz8yG7FUWg/uhFbXz73uI0nG2dE7uSSjRYnHR4qjYarR16QMTwjgPSjI11aRKYiEPx6zVMV1JA+LR2DhIIl33jJXOKRLPuuWLFpNnB4jS4caQY61Ha+FDCNf5pOrieEAv6DoYVnjfN5Jks8KlDlHXtsBV4q6McY68RpgbCun7sme9+9O7LlQG0vJJ3k6pNQP8ishQBBOmE+3abbRYE17ja1Nhp2NTsOYWUpvajPO2HPfIuOq26PRjQcg3c5vXiKl0hTEiJ95GpW95WL8p0aGtZy1UyufSA=
file_glob: true
file: rust-test-lens-*.vsix
on:
repo: hdevalke/rust-test-lens
tags: true
skip_cleanup: true

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "rust-test-lens" extension will be documented in this

Check [Keep a Changelog](https://keepachangelog.com/) for recommendations on how to structure this file.

## 0.1.0

- Adds a Debug codelens to the main function in binaries and examples.

## 0.0.5

- Solves the problem if there are still multiple binaries found. E.g. integration tests.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Rust Test Lens

[![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.

![test lens](images/test_codelens.png)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "rust-test-lens",
"displayName": "Rust Test Lens",
"description": "Adds a code lens to quickly run or debug a single test for your Rust code.",
"version": "0.0.5",
"version": "0.1.0",
"publisher": "hdevalke",
"engines": {
"vscode": "^1.30.2"
Expand Down
72 changes: 53 additions & 19 deletions src/RustCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ export class RustCodeLensProvider implements CodeLensProvider {
return this._onDidChange.event;
}

public async provideCodeLenses(doc: TextDocument,
token: CancellationToken): Promise<CodeLens[]> {
public async provideCodeLenses(doc: TextDocument,
token: CancellationToken): Promise<CodeLens[]> {
if (token.isCancellationRequested) {
return [];
}

let lenses: CodeLens[] = this.testMethodLenses(doc);
lenses.push(...this.mainMethodLenses(doc));
return lenses;
}

private mainMethodLenses(doc: TextDocument): any {
const text = doc.getText();
const reFnMain = /fn\s+(main)\s*\(\s*\)/g;
const match = reFnMain.exec(text);
let lenses: CodeLens[] = [];
if (match !== null) {
const codelens = this.makeLens(reFnMain.lastIndex, match[1], doc);
if (codelens !== undefined) {
lenses.push(codelens);
}
}
return lenses;
}

private testMethodLenses(doc: TextDocument) {
const text = doc.getText();
const reTest = /#\[test\]/g;
const reFnTest = /fn\s+(.+)\s*\(\s*\)/g;
Expand All @@ -30,37 +51,50 @@ export class RustCodeLensProvider implements CodeLensProvider {
const match = reFnTest.exec(text);
const fn = match === null ? null : match[1];
if (fn) {
const startIdx = reFnTest.lastIndex - fn.length - 3;
const start = doc.positionAt(startIdx);
const end = doc.positionAt(reFnTest.lastIndex);
const range = new Range(start, end);
const debugConfig = this.createDebugConfig(fn, doc.fileName);
if (debugConfig) {
lenses.push(new CodeLens(range, {
title: 'Debug test',
command: "extension.debugTest",
tooltip: 'Debug Test',
arguments: [debugConfig]
}));
const codelens = this.makeLens(reFnTest.lastIndex, fn, doc);
if (codelens !== undefined) {
lenses.push(codelens);
}
}
}
return lenses;
}

private makeLens(index: number, fn: string, doc: TextDocument) {
const startIdx = index - fn.length;
const start = doc.positionAt(startIdx);
const end = doc.positionAt(index);
const range = new Range(start, end);
const debugConfig = this.createDebugConfig(fn, doc.fileName);
if (debugConfig) {
return new CodeLens(range, {
title: 'Debug',
command: "extension.debugTest",
tooltip: 'Debug',
arguments: [debugConfig]
});
}
}

createDebugConfig(fn: string, uri: string): DebugConfiguration | undefined {
const pkg = this.rustTests.getPackage(fn, uri);
if (pkg) {
const args = fn === "main"
? [
"build",
`--package=${pkg.name}`
]
: [
"test",
"--no-run",
`--package=${pkg.name}`
];
const debugConfig = {
type: "lldb",
request: "launch",
name: `Debug ${fn} in ${basename(uri)}`,
cargo: {
args: [
"test",
"--no-run",
`--package=${pkg.name}`
],
args: args,
filter: {} as any,
},
args: [fn],
Expand Down

0 comments on commit 2277c68

Please sign in to comment.