-
Notifications
You must be signed in to change notification settings - Fork 324
Parse #Playground macro expansions and include them in CodeLens request #2340
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
Open
award999
wants to merge
4
commits into
swiftlang:main
Choose a base branch
from
award999:document-playgrounds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+618
−24
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
101 changes: 101 additions & 0 deletions
101
Sources/SwiftLanguageService/SwiftPlaygroundsScanner.swift
award999 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,101 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| internal import BuildServerIntegration | ||
| import Foundation | ||
| @_spi(SourceKitLSP) import LanguageServerProtocol | ||
| import SKLogging | ||
| import SourceKitLSP | ||
| import SwiftParser | ||
| import SwiftSyntax | ||
|
|
||
| // MARK: - SwiftPlaygroundsScanner | ||
|
|
||
| final class SwiftPlaygroundsScanner: SyntaxVisitor { | ||
| /// The base ID to use to generate IDs for any playgrounds found in this file. | ||
| private let baseID: String | ||
|
|
||
| /// The snapshot of the document for which we are getting playgrounds. | ||
| private let snapshot: DocumentSnapshot | ||
|
|
||
| /// Accumulating the result in here. | ||
| private var result: [TextDocumentPlayground] = [] | ||
|
|
||
| /// Keep track of if "Playgrounds" has been imported | ||
| private var isPlaygroundImported: Bool = false | ||
|
|
||
| private init(baseID: String, snapshot: DocumentSnapshot) { | ||
| self.baseID = baseID | ||
| self.snapshot = snapshot | ||
| super.init(viewMode: .sourceAccurate) | ||
| } | ||
|
|
||
award999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Designated entry point for `SwiftPlaygroundsScanner`. | ||
| static func findDocumentPlaygrounds( | ||
| in node: some SyntaxProtocol, | ||
| workspace: Workspace, | ||
| snapshot: DocumentSnapshot | ||
| ) async -> [TextDocumentPlayground] { | ||
| guard let canonicalTarget = await workspace.buildServerManager.canonicalTarget(for: snapshot.uri), | ||
| let moduleName = await workspace.buildServerManager.moduleName(for: snapshot.uri, in: canonicalTarget), | ||
| let baseName = snapshot.uri.fileURL?.lastPathComponent | ||
| else { | ||
| return [] | ||
| } | ||
| let visitor = SwiftPlaygroundsScanner(baseID: "\(moduleName)/\(baseName)", snapshot: snapshot) | ||
| visitor.walk(node) | ||
| return visitor.isPlaygroundImported ? visitor.result : [] | ||
| } | ||
|
|
||
| /// Add a playground location with the given parameters to the `result` array. | ||
| private func record( | ||
| id: String, | ||
| label: String?, | ||
| range: Range<AbsolutePosition> | ||
| ) { | ||
| let positionRange = snapshot.absolutePositionRange(of: range) | ||
|
|
||
| result.append( | ||
| TextDocumentPlayground( | ||
| id: id, | ||
| label: label, | ||
| range: positionRange, | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| override func visit(_ node: ImportPathComponentSyntax) -> SyntaxVisitorContinueKind { | ||
| if node.name.text == "Playgrounds" { | ||
| isPlaygroundImported = true | ||
| } | ||
| return .skipChildren | ||
| } | ||
|
|
||
| override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind { | ||
award999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| guard node.macroName.text == "Playground" else { | ||
| return .skipChildren | ||
| } | ||
|
|
||
| let startPosition = snapshot.sourcekitdPosition(of: snapshot.position(of: node.positionAfterSkippingLeadingTrivia)) | ||
| let stringLiteral = node.arguments.first?.expression.as(StringLiteralExprSyntax.self) | ||
| let playgroundLabel = stringLiteral?.representedLiteralValue | ||
| let playgroundID = "\(baseID):\(startPosition.line):\(startPosition.utf8Column)" | ||
|
|
||
| record( | ||
| id: playgroundID, | ||
| label: playgroundLabel, | ||
| range: node.trimmedRange | ||
| ) | ||
|
|
||
| return .skipChildren | ||
| } | ||
| } | ||
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.