13
13
public struct SyntaxChildrenIndex : Comparable {
14
14
fileprivate let offset : UInt32
15
15
fileprivate let indexInParent : UInt32
16
- // TODO: We can retrieve the rootId from the collection's node and could just
17
- // store indexInTree instead of the entire identifier
18
- fileprivate let identifier : SyntaxIdentifier
16
+ fileprivate let indexInTree : SyntaxIndexInTree
19
17
20
18
public static func < ( lhs: SyntaxChildrenIndex , rhs: SyntaxChildrenIndex )
21
19
-> Bool {
22
20
return lhs. indexInParent < rhs. indexInParent
23
21
}
24
22
25
23
fileprivate init ( offset: UInt32 , indexInParent: UInt32 ,
26
- identifier : SyntaxIdentifier ) {
24
+ indexInTree : SyntaxIndexInTree ) {
27
25
self . offset = offset
28
26
self . indexInParent = indexInParent
29
- self . identifier = identifier
27
+ self . indexInTree = indexInTree
30
28
}
31
29
32
30
init ( _ absoluteSyntaxInfo: AbsoluteSyntaxInfo ) {
33
31
self . offset = absoluteSyntaxInfo. offset
34
32
self . indexInParent = absoluteSyntaxInfo. indexInParent
35
- self . identifier = absoluteSyntaxInfo. nodeId
33
+ self . indexInTree = absoluteSyntaxInfo. nodeId. indexInTree
36
34
}
37
35
}
38
36
@@ -44,6 +42,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
44
42
45
43
/// The node whose children shall be accessed
46
44
private let parent : RawSyntax
45
+ private let rootId : UInt32
47
46
48
47
let startIndex : SyntaxChildrenIndex
49
48
let endIndex : SyntaxChildrenIndex
@@ -53,28 +52,31 @@ struct RawSyntaxChildren: BidirectionalCollection {
53
52
let nodeLength = UInt32 ( node? . totalLength. utf8Length ?? 0 )
54
53
return SyntaxChildrenIndex ( offset: index. offset + nodeLength,
55
54
indexInParent: index. indexInParent + 1 ,
56
- identifier : index. identifier . advancedBySibling ( node) )
55
+ indexInTree : index. indexInTree . advancedBySibling ( node) )
57
56
}
58
57
59
58
func index( before index: SyntaxChildrenIndex ) -> SyntaxChildrenIndex {
60
59
let previousNode = parent. child ( at: Int ( index. indexInParent - 1 ) )
61
60
let previousNodeLength = UInt32 ( previousNode? . totalLength. utf8Length ?? 0 )
62
61
return SyntaxChildrenIndex ( offset: index. offset - previousNodeLength,
63
62
indexInParent: index. indexInParent - 1 ,
64
- identifier : index. identifier . reversedBySibling ( previousNode) )
63
+ indexInTree : index. indexInTree . reversedBySibling ( previousNode) )
65
64
}
66
65
67
66
subscript( index: SyntaxChildrenIndex )
68
67
-> ( node: RawSyntax ? , info: AbsoluteSyntaxInfo ) {
69
68
let child = parent. child ( at: Int ( index. indexInParent) )
70
69
let position = AbsoluteSyntaxPosition ( offset: index. offset,
71
70
indexInParent: index. indexInParent)
72
- let info = AbsoluteSyntaxInfo ( position: position, nodeId: index. identifier)
71
+ let identifier = SyntaxIdentifier ( rootId: rootId,
72
+ indexInTree: index. indexInTree)
73
+ let info = AbsoluteSyntaxInfo ( position: position, nodeId: identifier)
73
74
return ( child, info)
74
75
}
75
76
76
77
init ( _ parent: AbsoluteRawSyntax ) {
77
78
self . parent = parent. raw
79
+ self . rootId = parent. info. nodeId. rootId
78
80
79
81
let startPosition = parent. info. advancedToFirstChild ( )
80
82
self . startIndex = SyntaxChildrenIndex ( startPosition)
0 commit comments