Skip to content

Commit 3f5fc46

Browse files
committed
Only store indexInTree in SyntaxChildrenIndex
1 parent e62ea71 commit 3f5fc46

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@
1313
public struct SyntaxChildrenIndex: Comparable {
1414
fileprivate let offset: UInt32
1515
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
1917

2018
public static func <(lhs: SyntaxChildrenIndex, rhs: SyntaxChildrenIndex)
2119
-> Bool {
2220
return lhs.indexInParent < rhs.indexInParent
2321
}
2422

2523
fileprivate init(offset: UInt32, indexInParent: UInt32,
26-
identifier: SyntaxIdentifier) {
24+
indexInTree: SyntaxIndexInTree) {
2725
self.offset = offset
2826
self.indexInParent = indexInParent
29-
self.identifier = identifier
27+
self.indexInTree = indexInTree
3028
}
3129

3230
init(_ absoluteSyntaxInfo: AbsoluteSyntaxInfo) {
3331
self.offset = absoluteSyntaxInfo.offset
3432
self.indexInParent = absoluteSyntaxInfo.indexInParent
35-
self.identifier = absoluteSyntaxInfo.nodeId
33+
self.indexInTree = absoluteSyntaxInfo.nodeId.indexInTree
3634
}
3735
}
3836

@@ -44,6 +42,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
4442

4543
/// The node whose children shall be accessed
4644
private let parent: RawSyntax
45+
private let rootId: UInt32
4746

4847
let startIndex: SyntaxChildrenIndex
4948
let endIndex: SyntaxChildrenIndex
@@ -53,28 +52,31 @@ struct RawSyntaxChildren: BidirectionalCollection {
5352
let nodeLength = UInt32(node?.totalLength.utf8Length ?? 0)
5453
return SyntaxChildrenIndex(offset: index.offset + nodeLength,
5554
indexInParent: index.indexInParent + 1,
56-
identifier: index.identifier.advancedBySibling(node))
55+
indexInTree: index.indexInTree.advancedBySibling(node))
5756
}
5857

5958
func index(before index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
6059
let previousNode = parent.child(at: Int(index.indexInParent - 1))
6160
let previousNodeLength = UInt32(previousNode?.totalLength.utf8Length ?? 0)
6261
return SyntaxChildrenIndex(offset: index.offset - previousNodeLength,
6362
indexInParent: index.indexInParent - 1,
64-
identifier: index.identifier.reversedBySibling(previousNode))
63+
indexInTree: index.indexInTree.reversedBySibling(previousNode))
6564
}
6665

6766
subscript(index: SyntaxChildrenIndex)
6867
-> (node: RawSyntax?, info: AbsoluteSyntaxInfo) {
6968
let child = parent.child(at: Int(index.indexInParent))
7069
let position = AbsoluteSyntaxPosition(offset: index.offset,
7170
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)
7374
return (child, info)
7475
}
7576

7677
init(_ parent: AbsoluteRawSyntax) {
7778
self.parent = parent.raw
79+
self.rootId = parent.info.nodeId.rootId
7880

7981
let startPosition = parent.info.advancedToFirstChild()
8082
self.startIndex = SyntaxChildrenIndex(startPosition)

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,66 @@ struct AbsoluteSyntaxInfo {
8181
}
8282
}
8383

84+
/// Represents a unique value for a node within its own tree.
85+
struct SyntaxIndexInTree: Hashable {
86+
let indexInTree: UInt32
87+
88+
static var zero: SyntaxIndexInTree = SyntaxIndexInTree(indexInTree: 0)
89+
90+
func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIndexInTree {
91+
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
92+
return .init(indexInTree: newIndexInTree)
93+
}
94+
95+
func reversedBySibling(_ raw: RawSyntax?) -> SyntaxIndexInTree {
96+
let newIndexInTree = self.indexInTree - UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
97+
return .init(indexInTree: newIndexInTree)
98+
}
99+
100+
func advancedToFirstChild() -> SyntaxIndexInTree {
101+
let newIndexInTree = self.indexInTree + 1
102+
return .init(indexInTree: newIndexInTree)
103+
}
104+
105+
func advancedToEndOfChildren(_ raw: RawSyntax) -> SyntaxIndexInTree {
106+
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw.totalNodes)
107+
return .init(indexInTree: newIndexInTree)
108+
}
109+
110+
private init(indexInTree: UInt32) {
111+
self.indexInTree = indexInTree
112+
}
113+
}
114+
84115
/// Provides a stable and unique identity for `Syntax` nodes.
85116
public struct SyntaxIdentifier: Hashable {
86117
/// Unique value for each root node created.
87118
let rootId: UInt32
88119
/// Unique value for a node within its own tree.
89-
let indexInTree: UInt32
120+
let indexInTree: SyntaxIndexInTree
90121

91122
func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
92-
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
123+
let newIndexInTree = indexInTree.advancedBySibling(raw)
93124
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
94125
}
95126

96127
func reversedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
97-
let newIndexInTree = self.indexInTree - UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
128+
let newIndexInTree = self.indexInTree.reversedBySibling(raw)
98129
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
99130
}
100131

101132
func advancedToFirstChild() -> SyntaxIdentifier {
102-
let newIndexInTree = self.indexInTree + 1
133+
let newIndexInTree = self.indexInTree.advancedToFirstChild()
103134
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
104135
}
105136

106137
func advancedToEndOfChildren(_ raw: RawSyntax) -> SyntaxIdentifier {
107-
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw.totalNodes)
138+
let newIndexInTree = self.indexInTree.advancedToEndOfChildren(raw)
108139
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
109140
}
110141

111142
static func newRoot() -> SyntaxIdentifier {
112-
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()), indexInTree: 0)
143+
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()), indexInTree: .zero)
113144
}
114145
}
115146

0 commit comments

Comments
 (0)