|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +internal import BuildServerIntegration |
| 14 | +import Foundation |
| 15 | +@_spi(SourceKitLSP) import LanguageServerProtocol |
| 16 | +import SKLogging |
| 17 | +import SourceKitLSP |
| 18 | +import SwiftParser |
| 19 | +import SwiftSyntax |
| 20 | + |
| 21 | +// MARK: - SwiftPlaygroundsScanner |
| 22 | + |
| 23 | +final class SwiftPlaygroundsScanner: SyntaxVisitor { |
| 24 | + /// The base ID to use to generate IDs for any playgrounds found in this file. |
| 25 | + private let baseID: String |
| 26 | + |
| 27 | + /// The snapshot of the document for which we are getting playgrounds. |
| 28 | + private let snapshot: DocumentSnapshot |
| 29 | + |
| 30 | + /// Accumulating the result in here. |
| 31 | + private var result: [TextDocumentPlayground] = [] |
| 32 | + |
| 33 | + /// Keep track of if "Playgrounds" has been imported |
| 34 | + private var isPlaygroundImported: Bool = false |
| 35 | + |
| 36 | + private init(baseID: String, snapshot: DocumentSnapshot) { |
| 37 | + self.baseID = baseID |
| 38 | + self.snapshot = snapshot |
| 39 | + super.init(viewMode: .sourceAccurate) |
| 40 | + } |
| 41 | + |
| 42 | + /// Designated entry point for `SwiftPlaygroundsScanner`. |
| 43 | + static func findDocumentPlaygrounds( |
| 44 | + in node: some SyntaxProtocol, |
| 45 | + workspace: Workspace, |
| 46 | + snapshot: DocumentSnapshot |
| 47 | + ) async -> [TextDocumentPlayground] { |
| 48 | + guard let canonicalTarget = await workspace.buildServerManager.canonicalTarget(for: snapshot.uri), |
| 49 | + let moduleName = await workspace.buildServerManager.moduleName(for: snapshot.uri, in: canonicalTarget), |
| 50 | + let baseName = snapshot.uri.fileURL?.lastPathComponent |
| 51 | + else { |
| 52 | + return [] |
| 53 | + } |
| 54 | + let visitor = SwiftPlaygroundsScanner(baseID: "\(moduleName)/\(baseName)", snapshot: snapshot) |
| 55 | + visitor.walk(node) |
| 56 | + return visitor.isPlaygroundImported ? visitor.result : [] |
| 57 | + } |
| 58 | + |
| 59 | + /// Add a playground location with the given parameters to the `result` array. |
| 60 | + private func record( |
| 61 | + id: String, |
| 62 | + label: String?, |
| 63 | + range: Range<AbsolutePosition> |
| 64 | + ) { |
| 65 | + let positionRange = snapshot.absolutePositionRange(of: range) |
| 66 | + |
| 67 | + result.append( |
| 68 | + TextDocumentPlayground( |
| 69 | + id: id, |
| 70 | + label: label, |
| 71 | + range: positionRange, |
| 72 | + ) |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + override func visit(_ node: ImportPathComponentSyntax) -> SyntaxVisitorContinueKind { |
| 77 | + if node.name.text == "Playgrounds" { |
| 78 | + isPlaygroundImported = true |
| 79 | + } |
| 80 | + return .skipChildren |
| 81 | + } |
| 82 | + |
| 83 | + override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind { |
| 84 | + guard node.macroName.text == "Playground" else { |
| 85 | + return .skipChildren |
| 86 | + } |
| 87 | + |
| 88 | + let startPosition = snapshot.position(of: node.positionAfterSkippingLeadingTrivia) |
| 89 | + let stringLiteral = node.arguments.first?.expression.as(StringLiteralExprSyntax.self) |
| 90 | + let playgroundLabel = stringLiteral?.representedLiteralValue |
| 91 | + let playgroundID = "\(baseID):\(startPosition.line + 1):\(startPosition.utf16index + 1)" |
| 92 | + |
| 93 | + record( |
| 94 | + id: playgroundID, |
| 95 | + label: playgroundLabel, |
| 96 | + range: node.trimmedRange |
| 97 | + ) |
| 98 | + |
| 99 | + return .skipChildren |
| 100 | + } |
| 101 | +} |
0 commit comments