-
Notifications
You must be signed in to change notification settings - Fork 87
Add Playgrounds to the project panel #1967
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6c4603
Add Playgrounds to the project panel
award999 bef05f9
Better error handling and only fetch if workspace/playground is exper…
award999 c14261e
Context command to run playground
award999 185302f
Add test
award999 9580a4c
Fix failing unit test
award999 c9494a4
Fix review comments
award999 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
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,60 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the VS Code Swift open source project | ||
| // | ||
| // Copyright (c) 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 { FolderContext } from "../FolderContext"; | ||
| import { checkExperimentalCapability } from "../sourcekit-lsp/LanguageClientManager"; | ||
| import { LanguageClientManager } from "../sourcekit-lsp/LanguageClientManager"; | ||
| import { Playground, WorkspacePlaygroundsRequest } from "../sourcekit-lsp/extensions"; | ||
| import { Version } from "../utilities/version"; | ||
|
|
||
| export { Playground }; | ||
|
|
||
| /** | ||
| * Uses document symbol request to keep a running copy of all the test methods | ||
| * in a file. When a file is saved it checks to see if any new methods have been | ||
| * added, or if any methods have been removed and edits the test items based on | ||
| * these results. | ||
| */ | ||
| export class LSPPlaygroundsDiscovery { | ||
| private languageClient: LanguageClientManager; | ||
| private toolchainVersion: Version; | ||
|
|
||
| constructor(folderContext: FolderContext) { | ||
| this.languageClient = folderContext.languageClientManager; | ||
| this.toolchainVersion = folderContext.toolchain.swiftVersion; | ||
| } | ||
|
|
||
| /** | ||
| * Return list of workspace playgrounds | ||
| */ | ||
| async getWorkspacePlaygrounds(): Promise<Playground[]> { | ||
| return await this.languageClient.useLanguageClient(async (client, token) => { | ||
| // Only use the lsp for this request if it supports the | ||
| // workspace/playgrounds method. | ||
| if (checkExperimentalCapability(client, WorkspacePlaygroundsRequest.method, 1)) { | ||
| return await client.sendRequest(WorkspacePlaygroundsRequest.type, token); | ||
| } else { | ||
| throw new Error(`${WorkspacePlaygroundsRequest.method} requests not supported`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| async supportsPlaygrounds(): Promise<boolean> { | ||
| if (this.toolchainVersion.isLessThan(new Version(6, 3, 0))) { | ||
| return false; | ||
| } | ||
| return await this.languageClient.useLanguageClient(async client => { | ||
| return checkExperimentalCapability(client, WorkspacePlaygroundsRequest.method, 1); | ||
| }); | ||
| } | ||
| } |
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,153 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the VS Code Swift open source project | ||
| // | ||
| // Copyright (c) 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 { FolderContext } from "../FolderContext"; | ||
| import { FolderOperation, WorkspaceContext } from "../WorkspaceContext"; | ||
| import { SwiftLogger } from "../logging/SwiftLogger"; | ||
| import { LSPPlaygroundsDiscovery, Playground } from "./LSPPlaygroundsDiscovery"; | ||
|
|
||
| export { Playground }; | ||
|
|
||
| export interface PlaygroundChangeEvent { | ||
| uri: string; | ||
| playgrounds: Playground[]; | ||
| } | ||
|
|
||
| /** | ||
| * Uses document symbol request to keep a running copy of all the test methods | ||
| * in a file. When a file is saved it checks to see if any new methods have been | ||
| * added, or if any methods have been removed and edits the test items based on | ||
| * these results. | ||
| */ | ||
| export class PlaygroundProvider implements vscode.Disposable { | ||
| private hasFetched: boolean = false; | ||
| private fetchPromise: Promise<Playground[]> | undefined; | ||
| private documentPlaygrounds: Map<string, Playground[]> = new Map(); | ||
| private didChangePlaygroundsEmitter: vscode.EventEmitter<PlaygroundChangeEvent> = | ||
| new vscode.EventEmitter(); | ||
|
|
||
| constructor(private folderContext: FolderContext) {} | ||
|
|
||
| private get lspPlaygroundDiscovery(): LSPPlaygroundsDiscovery { | ||
| return new LSPPlaygroundsDiscovery(this.folderContext); | ||
| } | ||
|
|
||
| private get logger(): SwiftLogger { | ||
| return this.folderContext.workspaceContext.logger; | ||
| } | ||
|
|
||
| /** | ||
| * Create folder observer that creates a PlaygroundProvider when a folder is added and | ||
| * discovers available playgrounds when the folder is in focus | ||
| * @param workspaceContext Workspace context for extension | ||
| * @returns Observer disposable | ||
| */ | ||
| public static observeFolders(workspaceContext: WorkspaceContext): vscode.Disposable { | ||
| return workspaceContext.onDidChangeFolders(({ folder, operation }) => { | ||
| switch (operation) { | ||
| case FolderOperation.add: | ||
| case FolderOperation.packageUpdated: | ||
| if (folder) { | ||
| void this.setupPlaygroundProviderForFolder(folder); | ||
| } | ||
| break; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static async setupPlaygroundProviderForFolder(folder: FolderContext) { | ||
| if (!folder.hasPlaygroundProvider()) { | ||
| folder.addPlaygroundProvider(); | ||
| } | ||
| await folder.refreshPlaygroundProvider(); | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the full list of playgrounds | ||
| */ | ||
| async getWorkspacePlaygrounds(): Promise<Playground[]> { | ||
| if (this.fetchPromise) { | ||
| return await this.fetchPromise; | ||
| } else if (!this.hasFetched) { | ||
| await this.fetch(); | ||
| } | ||
| return Array.from(this.documentPlaygrounds.values()).flatMap(v => v); | ||
| } | ||
|
|
||
| onDocumentCodeLens( | ||
| document: vscode.TextDocument, | ||
| codeLens: vscode.CodeLens[] | null | undefined | ||
| ) { | ||
| const playgrounds: Playground[] = ( | ||
| codeLens?.map(c => (c.command?.arguments ?? [])[0]) ?? [] | ||
| ) | ||
| .filter(p => !!p) | ||
| // Convert from LSP TextDocumentPlayground to Playground | ||
| .map(p => ({ | ||
| ...p, | ||
| range: undefined, | ||
| location: new vscode.Location(document.uri, p.range), | ||
| })); | ||
| const uri = document.uri.toString(); | ||
| if (playgrounds.length > 0) { | ||
| this.documentPlaygrounds.set(uri, playgrounds); | ||
| this.didChangePlaygroundsEmitter.fire({ uri, playgrounds }); | ||
| } else { | ||
| if (this.documentPlaygrounds.delete(uri)) { | ||
| this.didChangePlaygroundsEmitter.fire({ uri, playgrounds: [] }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| onDidChangePlaygrounds: vscode.Event<PlaygroundChangeEvent> = | ||
| this.didChangePlaygroundsEmitter.event; | ||
|
|
||
| async fetch() { | ||
| this.hasFetched = true; | ||
| if (this.fetchPromise) { | ||
| await this.fetchPromise; | ||
| return; | ||
| } | ||
| if (!(await this.lspPlaygroundDiscovery.supportsPlaygrounds())) { | ||
award999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.logger.debug( | ||
| `Fetching playgrounds not supported by the language server`, | ||
| this.folderContext.name | ||
| ); | ||
| return; | ||
| } | ||
| this.fetchPromise = this.lspPlaygroundDiscovery.getWorkspacePlaygrounds(); | ||
| try { | ||
| const playgrounds = await this.fetchPromise; | ||
| this.documentPlaygrounds.clear(); | ||
| for (const playground of playgrounds) { | ||
| const uri = playground.location.uri; | ||
| this.documentPlaygrounds.set( | ||
| uri, | ||
| (this.documentPlaygrounds.get(uri) ?? []).concat(playground) | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| this.logger.error( | ||
| `Failed to fetch workspace playgrounds: ${error}`, | ||
| this.folderContext.name | ||
| ); | ||
| } | ||
| this.fetchPromise = undefined; | ||
| } | ||
|
|
||
| dispose() { | ||
| this.documentPlaygrounds.clear(); | ||
| } | ||
| } | ||
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
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.