-
Notifications
You must be signed in to change notification settings - Fork 80
Code Lenses for Tests #1698
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
plemarquand
merged 10 commits into
swiftlang:main
from
plemarquand:code-lenses-for-tests
Jul 15, 2025
Merged
Code Lenses for Tests #1698
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e9b1ad7
Code Lenses for Tests
plemarquand 19ccde1
Add missing licence header
plemarquand 1ce7274
Update CHANGELOG
plemarquand 8a77ecc
Set setting scope to 'application'
plemarquand 2779e6c
Enable lenses by default
plemarquand 2825c20
Make lenses configurable
plemarquand 0f87700
Update changelog
plemarquand 6965bd2
Fixup tests and changelog formatting
plemarquand b9167b3
Merge branch 'main' into code-lenses-for-tests
plemarquand cb63b60
Apply suggestion from @plemarquand
plemarquand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2021-2025 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import * as vscode from "vscode"; | ||
import { TestExplorer } from "./TestExplorer"; | ||
import { flattenTestItemCollection } from "./TestUtils"; | ||
import configuration, { ValidCodeLens } from "../configuration"; | ||
|
||
export class TestCodeLensProvider implements vscode.CodeLensProvider, vscode.Disposable { | ||
private onDidChangeCodeLensesEmitter = new vscode.EventEmitter<void>(); | ||
public onDidChangeCodeLenses = this.onDidChangeCodeLensesEmitter.event; | ||
private disposables: vscode.Disposable[] = []; | ||
|
||
constructor(private testExplorer: TestExplorer) { | ||
this.disposables = [ | ||
testExplorer.onTestItemsDidChange(() => this.onDidChangeCodeLensesEmitter.fire()), | ||
vscode.languages.registerCodeLensProvider({ language: "swift", scheme: "file" }, this), | ||
]; | ||
} | ||
|
||
dispose() { | ||
this.disposables.forEach(disposable => disposable.dispose()); | ||
} | ||
|
||
public provideCodeLenses( | ||
document: vscode.TextDocument, | ||
_token: vscode.CancellationToken | ||
): vscode.ProviderResult<vscode.CodeLens[]> { | ||
const config = configuration.showTestCodeLenses; | ||
if (config === false || (Array.isArray(config) && config.length === 0)) { | ||
return []; | ||
} | ||
|
||
const items = flattenTestItemCollection(this.testExplorer.controller.items); | ||
return items | ||
.filter(item => item.uri?.fsPath === document.uri.fsPath) | ||
.flatMap(item => this.codeLensesForTestItem(item, config)); | ||
} | ||
|
||
private codeLensesForTestItem( | ||
item: vscode.TestItem, | ||
config: boolean | ValidCodeLens[] | ||
): vscode.CodeLens[] { | ||
if (!item.range) { | ||
return []; | ||
} | ||
|
||
const lensConfigs: Array<{ | ||
type: ValidCodeLens; | ||
title: string; | ||
command: string; | ||
}> = [ | ||
{ | ||
type: "run", | ||
title: "$(play)\u00A0Run", | ||
command: "swift.runTest", | ||
}, | ||
{ | ||
type: "debug", | ||
title: "$(debug)\u00A0Debug", | ||
command: "swift.debugTest", | ||
}, | ||
{ | ||
type: "coverage", | ||
title: "$(debug-coverage)\u00A0Run w/ Coverage", | ||
command: "swift.runTestWithCoverage", | ||
}, | ||
]; | ||
|
||
return lensConfigs | ||
.filter( | ||
lensConfig => | ||
config === true || (Array.isArray(config) && config.includes(lensConfig.type)) | ||
) | ||
.map( | ||
lensConfig => | ||
new vscode.CodeLens(item.range!, { | ||
title: lensConfig.title, | ||
command: lensConfig.command, | ||
arguments: [item], | ||
}) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.