Skip to content

Commit de59d5c

Browse files
committed
Make lenses configurable
1 parent 432d1ea commit de59d5c

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,21 @@
595595
"scope": "machine-overridable"
596596
},
597597
"swift.showTestCodeLenses": {
598-
"type": "boolean",
598+
"type": [
599+
"boolean",
600+
"array"
601+
],
599602
"default": true,
600-
"markdownDescription": "Controls whether or not to show inline code lenses for running and debugging tests inline, above test and suite declarations.",
601-
"scope": "application"
603+
"markdownDescription": "Controls whether or not to show inline code lenses for running and debugging tests inline, above test and suite declarations. If set to an array, specify one or more of the following: 'run', 'debug', 'coverage'.",
604+
"scope": "application",
605+
"items": {
606+
"type": "string",
607+
"enum": [
608+
"run",
609+
"debug",
610+
"coverage"
611+
]
612+
}
602613
}
603614
}
604615
},
@@ -1814,4 +1825,4 @@
18141825
"vscode-languageclient": "^9.0.1",
18151826
"xml2js": "^0.6.2"
18161827
}
1817-
}
1828+
}

src/TestExplorer/TestCodeLensProvider.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import * as vscode from "vscode";
1616
import { TestExplorer } from "./TestExplorer";
1717
import { flattenTestItemCollection } from "./TestUtils";
18-
import configuration from "../configuration";
18+
import configuration, { ValidCodeLens } from "../configuration";
1919

2020
export class TestCodeLensProvider implements vscode.CodeLensProvider, vscode.Disposable {
2121
private onDidChangeCodeLensesEmitter = new vscode.EventEmitter<void>();
@@ -37,37 +37,59 @@ export class TestCodeLensProvider implements vscode.CodeLensProvider, vscode.Dis
3737
document: vscode.TextDocument,
3838
_token: vscode.CancellationToken
3939
): vscode.ProviderResult<vscode.CodeLens[]> {
40-
if (configuration.showTestCodeLenses === false) {
40+
const config = configuration.showTestCodeLenses;
41+
if (config === false || (Array.isArray(config) && config.length === 0)) {
4142
return [];
4243
}
4344

4445
const items = flattenTestItemCollection(this.testExplorer.controller.items);
4546
return items
4647
.filter(item => item.uri?.fsPath === document.uri.fsPath)
47-
.flatMap(item => this.codeLensesForTestItem(item));
48+
.flatMap(item => this.codeLensesForTestItem(item, config));
4849
}
4950

50-
private codeLensesForTestItem(item: vscode.TestItem): vscode.CodeLens[] {
51+
private codeLensesForTestItem(
52+
item: vscode.TestItem,
53+
config: boolean | ValidCodeLens[]
54+
): vscode.CodeLens[] {
5155
if (!item.range) {
5256
return [];
5357
}
5458

55-
return [
56-
new vscode.CodeLens(item.range, {
59+
const lensConfigs: Array<{
60+
type: ValidCodeLens;
61+
title: string;
62+
command: string;
63+
}> = [
64+
{
65+
type: "run",
5766
title: "$(play)\u00A0Run",
5867
command: "swift.runTest",
59-
arguments: [item],
60-
}),
61-
new vscode.CodeLens(item.range, {
68+
},
69+
{
70+
type: "debug",
6271
title: "$(debug)\u00A0Debug",
6372
command: "swift.debugTest",
64-
arguments: [item],
65-
}),
66-
new vscode.CodeLens(item.range, {
73+
},
74+
{
75+
type: "coverage",
6776
title: "$(debug-coverage)\u00A0Run w/ Coverage",
6877
command: "swift.runTestWithCoverage",
69-
arguments: [item],
70-
}),
78+
},
7179
];
80+
81+
return lensConfigs
82+
.filter(
83+
lensConfig =>
84+
config === true || (Array.isArray(config) && config.includes(lensConfig.type))
85+
)
86+
.map(
87+
lensConfig =>
88+
new vscode.CodeLens(item.range!, {
89+
title: lensConfig.title,
90+
command: lensConfig.command,
91+
arguments: [item],
92+
})
93+
);
7294
}
7395
}

src/configuration.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type DiagnosticCollectionOptions =
3939
| "keepSourceKit"
4040
| "keepAll";
4141
export type DiagnosticStyle = "default" | "llvm" | "swift";
42+
export type ValidCodeLens = "run" | "debug" | "coverage";
4243

4344
/** sourcekit-lsp configuration */
4445
export interface LSPConfiguration {
@@ -291,8 +292,10 @@ const configuration = {
291292
.map(substituteVariablesInString);
292293
},
293294
/** Whether to show inline code lenses for running and debugging tests. */
294-
get showTestCodeLenses(): boolean {
295-
return vscode.workspace.getConfiguration("swift").get<boolean>("showTestCodeLenses", false);
295+
get showTestCodeLenses(): boolean | ValidCodeLens[] {
296+
return vscode.workspace
297+
.getConfiguration("swift")
298+
.get<boolean | ValidCodeLens[]>("showTestCodeLenses", true);
296299
},
297300
/** Files and directories to exclude from the Package Dependencies view. */
298301
get excludePathsFromPackageDependencies(): string[] {

0 commit comments

Comments
 (0)