Skip to content

Commit 303d4d2

Browse files
authored
Show Swift test explorer context menu items only for Swift tests (#1048)
Keep a flattened list of swift Test Explorer test IDs in the context and use a `when` clause on the context menu items to only show them on Swift tests in the Text Explorer. Issue: #1047
1 parent 2519726 commit 303d4d2

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

Diff for: package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -616,30 +616,31 @@
616616
{
617617
"command": "swift.runTestsMultipleTimes",
618618
"group": "testExtras",
619-
"when": "testId"
619+
"when": "testId in swift.tests"
620620
},
621621
{
622622
"command": "swift.runTestsUntilFailure",
623623
"group": "testExtras",
624-
"when": "testId"
624+
"when": "testId in swift.tests"
625625
}
626626
],
627627
"testing/item/context": [
628628
{
629629
"command": "swift.runTestsMultipleTimes",
630630
"group": "testExtras",
631-
"when": "testId"
631+
"when": "testId in swift.tests"
632632
},
633633
{
634634
"command": "swift.runTestsUntilFailure",
635635
"group": "testExtras",
636-
"when": "testId"
636+
"when": "testId in swift.tests"
637637
}
638638
],
639639
"file/newFile": [
640640
{
641641
"command": "swift.newFile",
642-
"group": "file"
642+
"group": "file",
643+
"when": "swift.isActivated"
643644
}
644645
],
645646
"explorer/context": [

Diff for: src/TestExplorer/TestExplorer.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as TestDiscovery from "./TestDiscovery";
2727
import { TargetType } from "../SwiftPackage";
2828
import { parseTestsFromSwiftTestListOutput } from "./SPMTestDiscovery";
2929
import { parseTestsFromDocumentSymbols } from "./DocumentSymbolTestDiscovery";
30+
import { flattenTestItemCollection } from "./TestUtils";
3031

3132
/** Build test explorer UI */
3233
export class TestExplorer {
@@ -56,8 +57,6 @@ export class TestExplorer {
5657
this.controller.resolveHandler = async item => {
5758
if (!item) {
5859
await this.discoverTestsInWorkspace();
59-
} else {
60-
//
6160
}
6261
};
6362

@@ -106,6 +105,7 @@ export class TestExplorer {
106105
this.onTestItemsDidChangeEmitter,
107106
this.onDidCreateTestRunEmitter,
108107
...this.testRunProfiles,
108+
this.onTestItemsDidChange(() => this.updateSwiftTestContext()),
109109
];
110110
}
111111

@@ -161,7 +161,18 @@ export class TestExplorer {
161161
});
162162
}
163163

164-
/** Called whenever we have new document symbols */
164+
/**
165+
* Sets the `swift.tests` context variable which is used by commands
166+
* to determine if the test item belongs to the Swift extension.
167+
*/
168+
private updateSwiftTestContext() {
169+
const items = flattenTestItemCollection(this.controller.items).map(({ id }) => id);
170+
vscode.commands.executeCommand("setContext", "swift.tests", items);
171+
}
172+
173+
/**
174+
* Called whenever we have new document symbols
175+
*/
165176
private static onDocumentSymbols(
166177
folder: FolderContext,
167178
document: vscode.TextDocument,

Diff for: src/TestExplorer/TestUtils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ export function reduceTestItemChildren<U>(
2929
});
3030
return accumulator;
3131
}
32+
33+
/**
34+
* Returns a flattented, iterable collection of test items in the collection.
35+
*/
36+
export function flattenTestItemCollection(coll: vscode.TestItemCollection): vscode.TestItem[] {
37+
const items: vscode.TestItem[] = [];
38+
coll.forEach(item => {
39+
items.push(item);
40+
items.push(...flattenTestItemCollection(item.children));
41+
});
42+
return items;
43+
}

0 commit comments

Comments
 (0)