Skip to content

Show Swift test explorer context menu items only for Swift tests #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,30 +616,31 @@
{
"command": "swift.runTestsMultipleTimes",
"group": "testExtras",
"when": "testId"
"when": "testId in swift.tests"
},
{
"command": "swift.runTestsUntilFailure",
"group": "testExtras",
"when": "testId"
"when": "testId in swift.tests"
}
],
"testing/item/context": [
{
"command": "swift.runTestsMultipleTimes",
"group": "testExtras",
"when": "testId"
"when": "testId in swift.tests"
},
{
"command": "swift.runTestsUntilFailure",
"group": "testExtras",
"when": "testId"
"when": "testId in swift.tests"
}
],
"file/newFile": [
{
"command": "swift.newFile",
"group": "file"
"group": "file",
"when": "swift.isActivated"
}
],
"explorer/context": [
Expand Down
17 changes: 14 additions & 3 deletions src/TestExplorer/TestExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as TestDiscovery from "./TestDiscovery";
import { TargetType } from "../SwiftPackage";
import { parseTestsFromSwiftTestListOutput } from "./SPMTestDiscovery";
import { parseTestsFromDocumentSymbols } from "./DocumentSymbolTestDiscovery";
import { flattenTestItemCollection } from "./TestUtils";

/** Build test explorer UI */
export class TestExplorer {
Expand Down Expand Up @@ -56,8 +57,6 @@ export class TestExplorer {
this.controller.resolveHandler = async item => {
if (!item) {
await this.discoverTestsInWorkspace();
} else {
//
}
};

Expand Down Expand Up @@ -106,6 +105,7 @@ export class TestExplorer {
this.onTestItemsDidChangeEmitter,
this.onDidCreateTestRunEmitter,
...this.testRunProfiles,
this.onTestItemsDidChange(() => this.updateSwiftTestContext()),
];
}

Expand Down Expand Up @@ -161,7 +161,18 @@ export class TestExplorer {
});
}

/** Called whenever we have new document symbols */
/**
* Sets the `swift.tests` context variable which is used by commands
* to determine if the test item belongs to the Swift extension.
*/
private updateSwiftTestContext() {
const items = flattenTestItemCollection(this.controller.items).map(({ id }) => id);
vscode.commands.executeCommand("setContext", "swift.tests", items);
}

/**
* Called whenever we have new document symbols
*/
private static onDocumentSymbols(
folder: FolderContext,
document: vscode.TextDocument,
Expand Down
12 changes: 12 additions & 0 deletions src/TestExplorer/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ export function reduceTestItemChildren<U>(
});
return accumulator;
}

/**
* Returns a flattented, iterable collection of test items in the collection.
*/
export function flattenTestItemCollection(coll: vscode.TestItemCollection): vscode.TestItem[] {
const items: vscode.TestItem[] = [];
coll.forEach(item => {
items.push(item);
items.push(...flattenTestItemCollection(item.children));
});
return items;
}