|
| 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 | +import Foundation |
| 14 | +package import LanguageServerProtocol |
| 15 | +import SKLogging |
| 16 | +package import SourceKitLSP |
| 17 | +import SwiftSyntax |
| 18 | +import Playgrounds |
| 19 | +import SwiftParser |
| 20 | +internal import BuildServerIntegration |
| 21 | + |
| 22 | +extension SwiftLanguageService { |
| 23 | + package func syntacticDocumentPlaygrounds(for uri: DocumentURI, in workspace: Workspace) async throws -> [PlaygroundItem] { |
| 24 | + let snapshot = try self.documentManager.latestSnapshot(uri) |
| 25 | + |
| 26 | + let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot) |
| 27 | + |
| 28 | + try Task.checkCancellation() |
| 29 | + return |
| 30 | + await PlaygroundMacroFinder.find( |
| 31 | + in: [Syntax(syntaxTree)], |
| 32 | + workspace: workspace, |
| 33 | + snapshot: snapshot, |
| 34 | + range: syntaxTree.position..<syntaxTree.endPosition |
| 35 | + ) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// MARK: - PlaygroundMacroFinder |
| 40 | + |
| 41 | +final class PlaygroundMacroFinder: SyntaxAnyVisitor { |
| 42 | + /// The base ID to use to generate IDs for any playgrounds found in this file. |
| 43 | + private let baseID: String |
| 44 | + |
| 45 | + /// The snapshot of the document for which we are getting playgrounds. |
| 46 | + private let snapshot: DocumentSnapshot |
| 47 | + |
| 48 | + /// Only playgrounds that intersect with this range get reported. |
| 49 | + private let range: Range<AbsolutePosition> |
| 50 | + |
| 51 | + /// Accumulating the result in here. |
| 52 | + private var result: [PlaygroundItem] = [] |
| 53 | + |
| 54 | + /// Keep track of if "Playgrounds" has been imported |
| 55 | + private var isPlaygroundImported: Bool = false |
| 56 | + |
| 57 | + private init(baseID: String, snapshot: DocumentSnapshot, range: Range<AbsolutePosition>) { |
| 58 | + self.baseID = baseID |
| 59 | + self.snapshot = snapshot |
| 60 | + self.range = range |
| 61 | + super.init(viewMode: .sourceAccurate) |
| 62 | + } |
| 63 | + |
| 64 | + /// Designated entry point for `PlaygroundMacroFinder`. |
| 65 | + static func find( |
| 66 | + in nodes: some Sequence<Syntax>, |
| 67 | + workspace: Workspace, |
| 68 | + snapshot: DocumentSnapshot, |
| 69 | + range: Range<AbsolutePosition> |
| 70 | + ) async -> [PlaygroundItem] { |
| 71 | + guard let canonicalTarget = await workspace.buildServerManager.canonicalTarget(for: snapshot.uri), |
| 72 | + let moduleName = await workspace.buildServerManager.moduleName(for: snapshot.uri, in: canonicalTarget), |
| 73 | + let baseName = snapshot.uri.fileURL?.lastPathComponent |
| 74 | + else { |
| 75 | + return [] |
| 76 | + } |
| 77 | + let visitor = PlaygroundMacroFinder(baseID: "\(moduleName)/\(baseName)", snapshot: snapshot, range: range) |
| 78 | + for node in nodes { |
| 79 | + visitor.walk(node) |
| 80 | + } |
| 81 | + return visitor.result |
| 82 | + } |
| 83 | + |
| 84 | + /// Add a playground location with the given parameters to the `result` array. |
| 85 | + private func record( |
| 86 | + id: String, |
| 87 | + label: String?, |
| 88 | + range: Range<AbsolutePosition> |
| 89 | + ) { |
| 90 | + if !self.range.overlaps(range) { |
| 91 | + return |
| 92 | + } |
| 93 | + let positionRange = snapshot.absolutePositionRange(of: range) |
| 94 | + let location = Location(uri: snapshot.uri, range: positionRange) |
| 95 | + |
| 96 | + result.append( |
| 97 | + PlaygroundItem( |
| 98 | + id: id, |
| 99 | + label: label, |
| 100 | + location: location, |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + override func visit(_ node: ImportPathComponentSyntax) -> SyntaxVisitorContinueKind { |
| 106 | + if node.name.text == "Playgrounds" { |
| 107 | + isPlaygroundImported = true |
| 108 | + } |
| 109 | + return .skipChildren |
| 110 | + } |
| 111 | + |
| 112 | + override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind { |
| 113 | + guard isPlaygroundImported, node.macroName.text == "Playground" else { |
| 114 | + return .skipChildren |
| 115 | + } |
| 116 | + |
| 117 | + let startPosition = snapshot.position(of: node.positionAfterSkippingLeadingTrivia) |
| 118 | + let stringLiteral = node.arguments.first?.expression.as(StringLiteralExprSyntax.self) |
| 119 | + let playgroundLabel = stringLiteral?.representedLiteralValue |
| 120 | + let playgroundID = "\(baseID):\(startPosition.line + 1)" |
| 121 | + |
| 122 | + record( |
| 123 | + id: playgroundID, |
| 124 | + label: playgroundLabel, |
| 125 | + range: node.positionAfterSkippingLeadingTrivia..<node.endPositionBeforeTrailingTrivia |
| 126 | + ) |
| 127 | + |
| 128 | + return .skipChildren |
| 129 | + } |
| 130 | +} |
0 commit comments