Skip to content
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

Return a keyPathInParent for syntax collections #1921

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxNodeStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum SyntaxNodeStructure: Sendable {
/// The node contains a fixed number of children which can be accessed by these key paths.
case layout([AnyKeyPath & Sendable])

/// The node is a `SyntaxCollection` of the given type.
/// The node is a `SyntaxCollection` with elements of the given type.
case collection(SyntaxProtocol.Type)

/// The node can contain a single node with one of the listed types.
Expand Down
20 changes: 17 additions & 3 deletions Sources/SwiftSyntax/SyntaxProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ extension SyntaxProtocol {
guard let parent = self.parent else {
return nil
}
guard case .layout(let childrenKeyPaths) = parent.kind.syntaxNodeType.structure else {
return nil

switch parent.kind.syntaxNodeType.structure {
case .layout(let childrenKeyPaths):
return childrenKeyPaths[Syntax(self).indexInParent]
case .collection(_):
return (parent.asProtocol(SyntaxProtocol.self) as! any SyntaxCollection).keyPath(for: self.index)
case .choices:
preconditionFailure("The parent of a syntax node should always be a concrete node and not one with choices")
}
return childrenKeyPaths[Syntax(self).indexInParent]
}

@available(*, deprecated, message: "Use previousToken(viewMode:) instead")
Expand Down Expand Up @@ -786,3 +791,12 @@ extension SyntaxChildChoices {
return self.as(S.self)!
}
}

extension SyntaxCollection {
/// Implementation detail of ``SyntaxProtocol/keyPathInParent``.
///
/// I couldn't find a way to express this without an extension on ``SyntaxCollection``.
fileprivate func keyPath(for index: SyntaxChildrenIndex) -> AnyKeyPath {
return \Self[index]
}
}
13 changes: 13 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxCollectionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,17 @@ class SyntaxCollectionsTests: XCTestCase {
// The filtered sequence should have the `ArrayExprSyntax` as a parent
XCTAssert(filteredElements.parent?.is(ArrayExprSyntax.self) ?? false)
}

public func testKeyPathInParent() throws {
let arrayElementList = ArrayElementListSyntax([
integerLiteralElement(0),
integerLiteralElement(1),
integerLiteralElement(2),
])

let element = arrayElementList[1]
let keyPath = try XCTUnwrap(element.keyPathInParent)
XCTAssert(type(of: keyPath).rootType == ArrayElementListSyntax.self)
XCTAssert(type(of: keyPath).valueType == ArrayElementSyntax.self)
}
}