@@ -20,7 +20,7 @@ public enum SyntaxNodeStructure {
20
20
/// The node contains a fixed number of children which can be accessed by these key paths.
21
21
case layout( [ AnyKeyPath ] )
22
22
23
- /// The node is a `SyntaxCollection` of the given type.
23
+ /// The node is a `SyntaxCollection` with elements of the given type.
24
24
case collection( SyntaxProtocol . Type )
25
25
26
26
/// The node can contain a single node with one of the listed types.
@@ -299,10 +299,14 @@ public extension SyntaxProtocol {
299
299
guard let parent = self . parent else {
300
300
return nil
301
301
}
302
- guard case . layout( let childrenKeyPaths) = parent. kind. syntaxNodeType. structure else {
303
- return nil
302
+ switch parent. kind. syntaxNodeType. structure {
303
+ case . layout( let childrenKeyPaths) :
304
+ return childrenKeyPaths [ data. indexInParent]
305
+ case . collection( _) :
306
+ return ( parent. asProtocol ( SyntaxProtocol . self) as! any SyntaxCollection ) . keyPath ( for: self . indexInParent)
307
+ case . choices:
308
+ preconditionFailure ( " The parent of a syntax node should always be a concrete node and not one with choices " )
304
309
}
305
- return childrenKeyPaths [ data. indexInParent]
306
310
}
307
311
308
312
@available ( * , deprecated, message: " Use previousToken(viewMode:) instead " )
@@ -823,3 +827,12 @@ extension ReversedTokenSequence: CustomReflectable {
823
827
/// replaced by the ``Syntax`` type.
824
828
@available ( * , unavailable, message: " use 'Syntax' instead " )
825
829
public struct SyntaxNode { }
830
+
831
+ extension SyntaxCollection {
832
+ /// Implementation detail of ``SyntaxProtocol/keyPathInParent``.
833
+ ///
834
+ /// I couldn't find a way to express this without an extension on ``SyntaxCollection``.
835
+ fileprivate func keyPath( for index: SyntaxChildrenIndex ) -> AnyKeyPath {
836
+ return \Self [ index]
837
+ }
838
+ }
0 commit comments