|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import * as vscode from "vscode"; |
| 15 | + |
| 16 | +import { FolderContext } from "../FolderContext"; |
| 17 | +import { FolderOperation, WorkspaceContext } from "../WorkspaceContext"; |
| 18 | +import { SwiftLogger } from "../logging/SwiftLogger"; |
| 19 | +import { LSPPlaygroundsDiscovery, Playground } from "./LSPPlaygroundsDiscovery"; |
| 20 | + |
| 21 | +export { Playground }; |
| 22 | + |
| 23 | +export interface PlaygroundChangeEvent { |
| 24 | + uri: string; |
| 25 | + playgrounds: Playground[]; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Uses document symbol request to keep a running copy of all the test methods |
| 30 | + * in a file. When a file is saved it checks to see if any new methods have been |
| 31 | + * added, or if any methods have been removed and edits the test items based on |
| 32 | + * these results. |
| 33 | + */ |
| 34 | +export class PlaygroundProvider implements vscode.Disposable { |
| 35 | + private hasFetched: boolean = false; |
| 36 | + private fetchPromise: Promise<Playground[]> | undefined; |
| 37 | + private documentPlaygrounds: Map<string, Playground[]> = new Map(); |
| 38 | + private didChangePlaygroundsEmitter: vscode.EventEmitter<PlaygroundChangeEvent> = |
| 39 | + new vscode.EventEmitter(); |
| 40 | + |
| 41 | + constructor(private folderContext: FolderContext) {} |
| 42 | + |
| 43 | + private get lspPlaygroundDiscovery(): LSPPlaygroundsDiscovery { |
| 44 | + return new LSPPlaygroundsDiscovery(this.folderContext); |
| 45 | + } |
| 46 | + |
| 47 | + private get logger(): SwiftLogger { |
| 48 | + return this.folderContext.workspaceContext.logger; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Create folder observer that creates a PlaygroundProvider when a folder is added and |
| 53 | + * discovers available playgrounds when the folder is in focus |
| 54 | + * @param workspaceContext Workspace context for extension |
| 55 | + * @returns Observer disposable |
| 56 | + */ |
| 57 | + public static observeFolders(workspaceContext: WorkspaceContext): vscode.Disposable { |
| 58 | + return workspaceContext.onDidChangeFolders(({ folder, operation }) => { |
| 59 | + switch (operation) { |
| 60 | + case FolderOperation.add: |
| 61 | + case FolderOperation.packageUpdated: |
| 62 | + if (folder) { |
| 63 | + void this.setupPlaygroundProviderForFolder(folder); |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + private static async setupPlaygroundProviderForFolder(folder: FolderContext) { |
| 71 | + if (!folder.hasPlaygroundProvider()) { |
| 72 | + folder.addPlaygroundProvider(); |
| 73 | + } |
| 74 | + await folder.refreshPlaygroundProvider(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Fetch the full list of playgrounds |
| 79 | + */ |
| 80 | + async getWorkspacePlaygrounds(): Promise<Playground[]> { |
| 81 | + if (this.fetchPromise) { |
| 82 | + return await this.fetchPromise; |
| 83 | + } else if (!this.hasFetched) { |
| 84 | + await this.fetch(); |
| 85 | + } |
| 86 | + return Array.from(this.documentPlaygrounds.values()).flatMap(v => v); |
| 87 | + } |
| 88 | + |
| 89 | + onDocumentCodeLens( |
| 90 | + document: vscode.TextDocument, |
| 91 | + codeLens: vscode.CodeLens[] | null | undefined |
| 92 | + ) { |
| 93 | + const playgrounds: Playground[] = ( |
| 94 | + codeLens?.map(c => (c.command?.arguments ?? [])[0]) ?? [] |
| 95 | + ) |
| 96 | + .filter(p => !!p) |
| 97 | + // Convert from LSP TextDocumentPlayground to Playground |
| 98 | + .map(p => ({ |
| 99 | + ...p, |
| 100 | + range: undefined, |
| 101 | + location: new vscode.Location(document.uri, p.range), |
| 102 | + })); |
| 103 | + const uri = document.uri.toString(); |
| 104 | + if (playgrounds.length > 0) { |
| 105 | + this.documentPlaygrounds.set(uri, playgrounds); |
| 106 | + this.didChangePlaygroundsEmitter.fire({ uri, playgrounds }); |
| 107 | + } else { |
| 108 | + if (this.documentPlaygrounds.delete(uri)) { |
| 109 | + this.didChangePlaygroundsEmitter.fire({ uri, playgrounds: [] }); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + onDidChangePlaygrounds: vscode.Event<PlaygroundChangeEvent> = |
| 115 | + this.didChangePlaygroundsEmitter.event; |
| 116 | + |
| 117 | + async fetch() { |
| 118 | + this.hasFetched = true; |
| 119 | + if (this.fetchPromise) { |
| 120 | + await this.fetchPromise; |
| 121 | + return; |
| 122 | + } |
| 123 | + if (!(await this.lspPlaygroundDiscovery.supportsPlaygrounds())) { |
| 124 | + this.logger.debug( |
| 125 | + `Fetching playgrounds not supported by the language server`, |
| 126 | + this.folderContext.name |
| 127 | + ); |
| 128 | + return; |
| 129 | + } |
| 130 | + this.fetchPromise = this.lspPlaygroundDiscovery.getWorkspacePlaygrounds(); |
| 131 | + try { |
| 132 | + const playgrounds = await this.fetchPromise; |
| 133 | + this.documentPlaygrounds.clear(); |
| 134 | + for (const playground of playgrounds) { |
| 135 | + const uri = playground.location.uri; |
| 136 | + this.documentPlaygrounds.set( |
| 137 | + uri, |
| 138 | + (this.documentPlaygrounds.get(uri) ?? []).concat(playground) |
| 139 | + ); |
| 140 | + } |
| 141 | + } catch (error) { |
| 142 | + this.logger.error( |
| 143 | + `Failed to fetch workspace playgrounds: ${error}`, |
| 144 | + this.folderContext.name |
| 145 | + ); |
| 146 | + } |
| 147 | + this.fetchPromise = undefined; |
| 148 | + } |
| 149 | + |
| 150 | + dispose() { |
| 151 | + this.documentPlaygrounds.clear(); |
| 152 | + } |
| 153 | +} |
0 commit comments