-
Notifications
You must be signed in to change notification settings - Fork 364
feat: add inlay hints for trailing closure labels #2464
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
DPrakashhh
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
DPrakashhh:feature/inlay-hints-trailing-closures-2381
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.
Open
Changes from 1 commit
Commits
Show all changes
2 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
229 changes: 229 additions & 0 deletions
229
Sources/SwiftLanguageService/TrailingClosureInlayHints.swift
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,229 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2026 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| import Foundation | ||
| @_spi(SourceKitLSP) package import LanguageServerProtocol | ||
| package import SKOptions | ||
| import SourceKitD | ||
| import SourceKitLSP | ||
| import SwiftSyntax | ||
|
|
||
| /// Collects trailing closure inlay hints for function calls. | ||
| private class TrailingClosureHintCollector: SyntaxVisitor { | ||
| private var hints: [TrailingClosureHintInfo] = [] | ||
|
|
||
| override func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind { | ||
| if let trailingClosure = node.trailingClosure { | ||
| let hintInfo = TrailingClosureHintInfo( | ||
| trailingClosure: trailingClosure, | ||
| functionCall: node | ||
| ) | ||
| hints.append(hintInfo) | ||
| } | ||
| return .visitChildren | ||
| } | ||
|
|
||
| static func collectTrailingClosures(in tree: some SyntaxProtocol) -> [TrailingClosureHintInfo] { | ||
| let visitor = TrailingClosureHintCollector(viewMode: .sourceAccurate) | ||
| visitor.walk(tree) | ||
| return visitor.hints | ||
| } | ||
| } | ||
|
|
||
| /// Information about a trailing closure that may need an inlay hint. | ||
| struct TrailingClosureHintInfo { | ||
| let trailingClosure: ClosureExprSyntax | ||
| let functionCall: FunctionCallExprSyntax | ||
|
|
||
| /// The opening brace of the trailing closure. | ||
| var openingBrace: TokenSyntax { | ||
| trailingClosure.leftBrace | ||
| } | ||
| } | ||
|
|
||
| extension SwiftLanguageService { | ||
| /// Generates inlay hints for trailing closures in the given range. | ||
| /// | ||
| /// Trailing closure hints display the parameter name immediately before the opening brace | ||
| /// of a trailing closure, helping identify which parameter the closure satisfies. | ||
| /// | ||
| /// - Parameters: | ||
| /// - uri: The document URI. | ||
| /// - range: Optional range to filter hints. If nil, hints are generated for the entire document. | ||
| /// - options: Server configuration options. | ||
| /// | ||
| /// - Returns: An array of inlay hints for trailing closures. | ||
| package func trailingClosureInlayHints( | ||
| uri: DocumentURI, | ||
| range: Range<Position>?, | ||
| options: SourceKitLSPOptions | ||
| ) async -> [InlayHint] { | ||
| // Return early if feature is disabled | ||
| guard options.inlayHintsOrDefault.trailingClosureLabelsOrDefault else { | ||
| return [] | ||
| } | ||
|
|
||
| do { | ||
| let snapshot = try await self.latestSnapshot(for: uri) | ||
| let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot) | ||
|
|
||
| let closureInfos = TrailingClosureHintCollector.collectTrailingClosures(in: syntaxTree) | ||
|
|
||
| var hints: [InlayHint] = [] | ||
|
|
||
| for closureInfo in closureInfos { | ||
| // Check if the closure is within the requested range | ||
| if let range { | ||
| let openingBracePosition = snapshot.position( | ||
| of: closureInfo.openingBrace.endPositionBeforeTrailingTrivia | ||
| ) | ||
| guard openingBracePosition >= range.lowerBound && openingBracePosition < range.upperBound else { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Try to get the parameter label from the function signature | ||
| if let parameterLabel = await getTrailingClosureParameterLabel( | ||
| for: closureInfo.functionCall, | ||
| in: snapshot | ||
| ) { | ||
| let hintPosition = snapshot.position(of: closureInfo.openingBrace.endPositionBeforeTrailingTrivia) | ||
| let label = ": \(parameterLabel)" | ||
|
|
||
| let hint = InlayHint( | ||
| position: hintPosition, | ||
| label: .string(label), | ||
| kind: .parameter, | ||
| paddingLeft: false, | ||
| paddingRight: false | ||
| ) | ||
| hints.append(hint) | ||
| } | ||
| } | ||
|
|
||
| return hints | ||
| } catch { | ||
| // If any error occurs during hint generation, return empty array | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| /// Retrieves the parameter label for a trailing closure in a function call. | ||
| /// | ||
| /// This queries sourcekitd to determine the function's signature and identifies | ||
| /// the parameter that the trailing closure satisfies. | ||
| /// | ||
| /// - Parameters: | ||
| /// - functionCall: The function call expression containing the trailing closure. | ||
| /// - snapshot: The document snapshot. | ||
| /// | ||
| /// - Returns: The parameter label if it can be determined, or nil if the information is unavailable. | ||
| private func getTrailingClosureParameterLabel( | ||
| for functionCall: FunctionCallExprSyntax, | ||
| in snapshot: DocumentSnapshot | ||
| ) async -> String? { | ||
| let compileCommand = await self.compileCommand(for: snapshot.uri, fallbackAfterTimeout: false) | ||
|
|
||
| // Query sourcekitd at the position of the function call to get information about the called function | ||
| do { | ||
| let calleePosition = snapshot.position(of: functionCall.calledExpression.endPositionBeforeTrailingTrivia) | ||
| let calleeOffset = snapshot.utf8Offset(of: calleePosition) | ||
| let skreq = sourcekitd.dictionary([ | ||
| keys.cancelOnSubsequentRequest: 0, | ||
| keys.offset: calleeOffset, | ||
| keys.sourceFile: snapshot.uri.sourcekitdSourceFile, | ||
| keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath, | ||
| keys.compilerArgs: compileCommand?.compilerArgs as [any SKDRequestValue]?, | ||
| ]) | ||
|
|
||
| let dict = try await send(sourcekitdRequest: \.cursorInfo, skreq, snapshot: snapshot) | ||
|
|
||
| // Get the function signature and identify the trailing closure parameter | ||
| // Try docFullAsXML first, then fallback to annotatedDecl | ||
| var signature: String? | ||
| if let xmlSig: String = dict[keys.docFullAsXML] { | ||
| signature = xmlSig | ||
| } else if let annotDecl: String = dict[keys.annotatedDecl] { | ||
| signature = annotDecl | ||
| } | ||
|
|
||
| if let signature { | ||
| return extractTrailingClosureParameterName(from: signature) | ||
| } | ||
|
|
||
| return nil | ||
| } catch { | ||
| // If sourcekitd query fails, we can't determine the parameter label | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| /// Extracts the trailing closure parameter name from a function signature. | ||
| /// | ||
| /// - Parameter signature: The function signature string (may be XML or plain text). | ||
| /// - Returns: The parameter name if it can be determined. | ||
| private func extractTrailingClosureParameterName(from signature: String) -> String? { | ||
| // Common trailing closure parameter names in order of likelihood | ||
| let commonNames = [ | ||
| "content", // SwiftUI views | ||
| "label", // SwiftUI controls | ||
| "body", // View bodies | ||
| "completion", // Async operations | ||
| "handler", // Event handlers | ||
| "onComplete", // Callbacks | ||
| "onSuccess", // Async results | ||
| "onFailure", // Error handlers | ||
| ] | ||
|
|
||
| // Check for these common names in the signature | ||
| for name in commonNames { | ||
| // Look for parameter pattern: name: @escaping? (args) -> ReturnType | ||
| let patterns = [ | ||
| "\\b\(name)\\s*:\\s*@escaping\\s*\\(", // @escaping version | ||
| "\\b\(name)\\s*:\\s*\\([^)]*\\)\\s*->", // non-escaping version | ||
| "\\b\(name)\\s*:\\s*@\\w+\\s*\\(\\)", // simple closure | ||
| ] | ||
|
|
||
| for pattern in patterns { | ||
| if let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) { | ||
| let range = NSRange(signature.startIndex..., in: signature) | ||
| if regex.firstMatch(in: signature, options: [], range: range) != nil { | ||
| return name | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Try to extract any closure parameter name using a more generic pattern | ||
| // Look for: word: (something) -> or word: @escaping (something) -> | ||
| let genericPattern = "\\b([a-zA-Z_]\\w*)\\s*:\\s*(?:@escaping\\s+)?\\([^)]*\\)\\s*->" | ||
| if let regex = try? NSRegularExpression(pattern: genericPattern, options: []) { | ||
| let range = NSRange(signature.startIndex..., in: signature) | ||
| if let match = regex.firstMatch(in: signature, options: [], range: range), | ||
| match.numberOfRanges > 1, | ||
| let paramRange = Range(match.range(at: 1), in: signature) | ||
| { | ||
| let paramName = String(signature[paramRange]) | ||
| return paramName | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
DPrakashhh marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| func testHint() { | ||
| let numbers = [1, 2] | ||
| numbers.forEach { number in | ||
| print(number) | ||
| } | ||
| } | ||
|
DPrakashhh marked this conversation as resolved.
Outdated
|
||
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.