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

Commit 2277c68

Browse files
committed
Add debug code lens for main functions
1 parent 19e453b commit 2277c68

File tree

5 files changed

+85
-20
lines changed

5 files changed

+85
-20
lines changed

.travis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
sudo: false
5+
os:
6+
- linux
7+
before_install:
8+
- if [ $TRAVIS_OS_NAME == "linux" ]; then export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
9+
sh -e /etc/init.d/xvfb start; sleep 3; fi
10+
install:
11+
- npm install
12+
- "./node_modules/.bin/vsce package"
13+
script:
14+
- npm test --silent
15+
deploy:
16+
provider: releases
17+
api_key:
18+
secure: Cubt88tWEdFNNIcY3yfcyOJiKFGkKzuDtZukRSGoubKZ40iHEXL0T5tbuMQ7AL+Gy43JjhymXjmLXnQ1avks99KJ2v30V7b82JaamGlK6jxvC4dD5CmkcT5wfpbVItwVnK4l/LrsAmEvhi8sUfLfS8p22eVTUITQk9zlxxkWw5NOCYbMzsNSGTsbsGf5KWAxKEWBseLztZsNQQq3Xfm5rK+pTd6cG1I3uOFZnGt225ztTQf3rjB4CqSO0mjMTh186jghQatAhYGhId0lMg9hK9TpugDCGbhZqGErs5YRAi4/3Bx+f3rHBUV3QfewGS4IxfqM7AeWhcaaWa5pbKSXFZkBbrWWQMLIhBtuljxuaCD/52HucA72RK02nkUXqXnP+slKbXyeP7xfsJz8yG7FUWg/uhFbXz73uI0nG2dE7uSSjRYnHR4qjYarR16QMTwjgPSjI11aRKYiEPx6zVMV1JA+LR2DhIIl33jJXOKRLPuuWLFpNnB4jS4caQY61Ha+FDCNf5pOrieEAv6DoYVnjfN5Jks8KlDlHXtsBV4q6McY68RpgbCun7sme9+9O7LlQG0vJJ3k6pNQP8ishQBBOmE+3abbRYE17ja1Nhp2NTsOYWUpvajPO2HPfIuOq26PRjQcg3c5vXiKl0hTEiJ95GpW95WL8p0aGtZy1UyufSA=
19+
file_glob: true
20+
file: rust-test-lens-*.vsix
21+
on:
22+
repo: hdevalke/rust-test-lens
23+
tags: true
24+
skip_cleanup: true
25+

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to the "rust-test-lens" extension will be documented in this
44

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

7+
## 0.1.0
8+
9+
- Adds a Debug codelens to the main function in binaries and examples.
10+
711
## 0.0.5
812

913
- Solves the problem if there are still multiple binaries found. E.g. integration tests.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Rust Test Lens
22

3+
[![Build Status](https://travis-ci.org/hdevalke/rust-test-lens.svg?branch=master)](https://travis-ci.org/hdevalke/rust-test-lens)
4+
35
A code lens to debug test in rust source files.
46

57
![test lens](images/test_codelens.png)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "rust-test-lens",
33
"displayName": "Rust Test Lens",
44
"description": "Adds a code lens to quickly run or debug a single test for your Rust code.",
5-
"version": "0.0.5",
5+
"version": "0.1.0",
66
"publisher": "hdevalke",
77
"engines": {
88
"vscode": "^1.30.2"

src/RustCodeLensProvider.ts

+53-19
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,32 @@ export class RustCodeLensProvider implements CodeLensProvider {
1616
return this._onDidChange.event;
1717
}
1818

19-
public async provideCodeLenses(doc: TextDocument,
20-
token: CancellationToken): Promise<CodeLens[]> {
19+
public async provideCodeLenses(doc: TextDocument,
20+
token: CancellationToken): Promise<CodeLens[]> {
2121
if (token.isCancellationRequested) {
2222
return [];
2323
}
24+
25+
let lenses: CodeLens[] = this.testMethodLenses(doc);
26+
lenses.push(...this.mainMethodLenses(doc));
27+
return lenses;
28+
}
29+
30+
private mainMethodLenses(doc: TextDocument): any {
31+
const text = doc.getText();
32+
const reFnMain = /fn\s+(main)\s*\(\s*\)/g;
33+
const match = reFnMain.exec(text);
34+
let lenses: CodeLens[] = [];
35+
if (match !== null) {
36+
const codelens = this.makeLens(reFnMain.lastIndex, match[1], doc);
37+
if (codelens !== undefined) {
38+
lenses.push(codelens);
39+
}
40+
}
41+
return lenses;
42+
}
43+
44+
private testMethodLenses(doc: TextDocument) {
2445
const text = doc.getText();
2546
const reTest = /#\[test\]/g;
2647
const reFnTest = /fn\s+(.+)\s*\(\s*\)/g;
@@ -30,37 +51,50 @@ export class RustCodeLensProvider implements CodeLensProvider {
3051
const match = reFnTest.exec(text);
3152
const fn = match === null ? null : match[1];
3253
if (fn) {
33-
const startIdx = reFnTest.lastIndex - fn.length - 3;
34-
const start = doc.positionAt(startIdx);
35-
const end = doc.positionAt(reFnTest.lastIndex);
36-
const range = new Range(start, end);
37-
const debugConfig = this.createDebugConfig(fn, doc.fileName);
38-
if (debugConfig) {
39-
lenses.push(new CodeLens(range, {
40-
title: 'Debug test',
41-
command: "extension.debugTest",
42-
tooltip: 'Debug Test',
43-
arguments: [debugConfig]
44-
}));
54+
const codelens = this.makeLens(reFnTest.lastIndex, fn, doc);
55+
if (codelens !== undefined) {
56+
lenses.push(codelens);
4557
}
4658
}
4759
}
4860
return lenses;
4961
}
5062

63+
private makeLens(index: number, fn: string, doc: TextDocument) {
64+
const startIdx = index - fn.length;
65+
const start = doc.positionAt(startIdx);
66+
const end = doc.positionAt(index);
67+
const range = new Range(start, end);
68+
const debugConfig = this.createDebugConfig(fn, doc.fileName);
69+
if (debugConfig) {
70+
return new CodeLens(range, {
71+
title: 'Debug',
72+
command: "extension.debugTest",
73+
tooltip: 'Debug',
74+
arguments: [debugConfig]
75+
});
76+
}
77+
}
78+
5179
createDebugConfig(fn: string, uri: string): DebugConfiguration | undefined {
5280
const pkg = this.rustTests.getPackage(fn, uri);
5381
if (pkg) {
82+
const args = fn === "main"
83+
? [
84+
"build",
85+
`--package=${pkg.name}`
86+
]
87+
: [
88+
"test",
89+
"--no-run",
90+
`--package=${pkg.name}`
91+
];
5492
const debugConfig = {
5593
type: "lldb",
5694
request: "launch",
5795
name: `Debug ${fn} in ${basename(uri)}`,
5896
cargo: {
59-
args: [
60-
"test",
61-
"--no-run",
62-
`--package=${pkg.name}`
63-
],
97+
args: args,
6498
filter: {} as any,
6599
},
66100
args: [fn],

0 commit comments

Comments
 (0)