@@ -16,11 +16,32 @@ export class RustCodeLensProvider implements CodeLensProvider {
16
16
return this . _onDidChange . event ;
17
17
}
18
18
19
- public async provideCodeLenses ( doc : TextDocument ,
20
- token : CancellationToken ) : Promise < CodeLens [ ] > {
19
+ public async provideCodeLenses ( doc : TextDocument ,
20
+ token : CancellationToken ) : Promise < CodeLens [ ] > {
21
21
if ( token . isCancellationRequested ) {
22
22
return [ ] ;
23
23
}
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 = / f n \s + ( m a i n ) \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 ) {
24
45
const text = doc . getText ( ) ;
25
46
const reTest = / # \[ t e s t \] / g;
26
47
const reFnTest = / f n \s + ( .+ ) \s * \( \s * \) / g;
@@ -30,37 +51,50 @@ export class RustCodeLensProvider implements CodeLensProvider {
30
51
const match = reFnTest . exec ( text ) ;
31
52
const fn = match === null ? null : match [ 1 ] ;
32
53
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 ) ;
45
57
}
46
58
}
47
59
}
48
60
return lenses ;
49
61
}
50
62
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
+
51
79
createDebugConfig ( fn : string , uri : string ) : DebugConfiguration | undefined {
52
80
const pkg = this . rustTests . getPackage ( fn , uri ) ;
53
81
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
+ ] ;
54
92
const debugConfig = {
55
93
type : "lldb" ,
56
94
request : "launch" ,
57
95
name : `Debug ${ fn } in ${ basename ( uri ) } ` ,
58
96
cargo : {
59
- args : [
60
- "test" ,
61
- "--no-run" ,
62
- `--package=${ pkg . name } `
63
- ] ,
97
+ args : args ,
64
98
filter : { } as any ,
65
99
} ,
66
100
args : [ fn ] ,
0 commit comments