-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathgoRunTestCodelens.ts
More file actions
196 lines (176 loc) · 5.89 KB
/
goRunTestCodelens.ts
File metadata and controls
196 lines (176 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import { CancellationToken, CodeLens, TextDocument } from 'vscode';
import { getGoConfig } from './config';
import { GoBaseCodeLensProvider } from './goBaseCodelens';
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
import { GoExtensionContext } from './context';
import { GO_MODE } from './goMode';
import { experiments } from './experimental';
export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
static activate(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) {
const testCodeLensProvider = new this(goCtx);
const setEnabled = () => {
const updatedGoConfig = getGoConfig();
if (updatedGoConfig['enableCodeLens']) {
testCodeLensProvider.setEnabled(
updatedGoConfig['enableCodeLens']['runtest'] && !experiments.testExplorer
);
}
};
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, testCodeLensProvider));
ctx.subscriptions.push(experiments.onDidChange(() => setEnabled()));
ctx.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
if (e.affectsConfiguration('go')) {
setEnabled();
}
})
);
}
constructor(private readonly goCtx: GoExtensionContext) {
super();
}
private readonly benchmarkRegex = /^Benchmark.+/;
public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
if (!this.enabled) {
return [];
}
const config = getGoConfig(document.uri);
const codeLensConfig = config.get<{ [key: string]: any }>('enableCodeLens');
const codelensEnabled = codeLensConfig ? codeLensConfig['runtest'] : false;
if (!codelensEnabled || !document.fileName.endsWith('_test.go')) {
return [];
}
const codelenses = await Promise.all([
this.getCodeLensForPackage(document, token),
this.getCodeLensForFunctions(document, token)
]);
return ([] as CodeLens[]).concat(...codelenses);
}
private async getCodeLensForPackage(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx);
const symbols = await documentSymbolProvider.provideDocumentSymbols(document);
if (!symbols || symbols.length === 0) {
return [];
}
const pkg = symbols[0];
if (!pkg) {
return [];
}
const range = pkg.range;
const packageCodeLens = [
new CodeLens(range, {
title: 'run package tests',
command: 'go.test.package'
}),
new CodeLens(range, {
title: 'run file tests',
command: 'go.test.file'
})
];
if (pkg.children.some((sym) => sym.kind === vscode.SymbolKind.Function && this.benchmarkRegex.test(sym.name))) {
packageCodeLens.push(
new CodeLens(range, {
title: 'run package benchmarks',
command: 'go.benchmark.package'
}),
new CodeLens(range, {
title: 'run file benchmarks',
command: 'go.benchmark.file'
})
);
}
return packageCodeLens;
}
private async getCodeLensForFunctions(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
const testPromise = async (): Promise<CodeLens[]> => {
const codelens: CodeLens[] = [];
const testFunctions = await getTestFunctions(this.goCtx, document, token);
if (!testFunctions) {
return codelens;
}
const simpleRunRegex = /t.Run\("([^"]+)",/;
for (const f of testFunctions) {
const functionName = f.name;
codelens.push(
new CodeLens(f.range, {
title: 'run test',
command: 'go.test.cursor',
arguments: [{ functionName }]
}),
new CodeLens(f.range, {
title: 'debug test',
command: 'go.debug.cursor',
arguments: [{ functionName }]
})
);
if (getGoConfig(document.uri).get<{ [key: string]: boolean }>('enableCodeLens')?.rrtest) {
codelens.push(
new CodeLens(f.range, {
title: 'rr test',
command: 'go.rr.cursor',
arguments: [{ functionName }]
})
);
}
for (let i = f.range.start.line; i < f.range.end.line; i++) {
const line = document.lineAt(i);
const simpleMatch = line.text.match(simpleRunRegex);
// BUG: this does not handle nested subtests. This should
// be solved once codelens is handled by gopls and not by
// vscode.
if (simpleMatch) {
const subTestName = simpleMatch[1];
codelens.push(
new CodeLens(line.range, {
title: 'run test',
command: 'go.subtest.cursor',
arguments: [{ functionName, subTestName }]
}),
new CodeLens(line.range, {
title: 'debug test',
command: 'go.debug.subtest.cursor',
arguments: [{ functionName, subTestName }]
})
);
}
}
}
return codelens;
};
const benchmarkPromise = async (): Promise<CodeLens[]> => {
const benchmarkFunctions = await getBenchmarkFunctions(this.goCtx, document, token);
if (!benchmarkFunctions) {
return [];
}
const codelens: CodeLens[] = [];
for (const f of benchmarkFunctions) {
codelens.push(
new CodeLens(f.range, {
title: 'run benchmark',
command: 'go.benchmark.cursor',
arguments: [{ functionName: f.name }]
})
);
codelens.push(
new CodeLens(f.range, {
title: 'debug benchmark',
command: 'go.debug.cursor',
arguments: [{ functionName: f.name }]
})
);
}
return codelens;
};
const codelenses = await Promise.all([testPromise(), benchmarkPromise()]);
return ([] as CodeLens[]).concat(...codelenses);
}
}