@@ -55,11 +55,11 @@ struct RawSyntaxLayoutView {
55
55
at index: Int ,
56
56
arena: SyntaxArena
57
57
) -> RawSyntax {
58
- precondition ( raw . children. count >= index)
58
+ precondition ( children. count >= index)
59
59
return . makeLayout( kind: raw. kind,
60
- uninitializedCount: raw . children. count + 1 ,
60
+ uninitializedCount: children. count + 1 ,
61
61
arena: arena) { buffer in
62
- var childIterator = raw . children. makeIterator ( )
62
+ var childIterator = children. makeIterator ( )
63
63
let base = buffer. baseAddress!
64
64
for i in 0 ..< buffer. count {
65
65
base. advanced ( by: i)
@@ -72,27 +72,27 @@ struct RawSyntaxLayoutView {
72
72
at index: Int ,
73
73
arena: SyntaxArena
74
74
) -> RawSyntax {
75
- precondition ( raw . children. count > index)
76
- let count = raw . children. count - 1
75
+ precondition ( children. count > index)
76
+ let count = children. count - 1
77
77
return . makeLayout( kind: raw. kind,
78
78
uninitializedCount: count,
79
79
arena: arena) { buffer in
80
80
if buffer. isEmpty { return }
81
81
let newBase = buffer. baseAddress!
82
- let oldBase = raw . children. baseAddress!
82
+ let oldBase = children. baseAddress!
83
83
84
84
// Copy elements up to the index.
85
85
newBase. initialize ( from: oldBase, count: index)
86
86
87
87
// Copy elements from the index + 1.
88
88
newBase. advanced ( by: index)
89
89
. initialize ( from: oldBase. advanced ( by: index + 1 ) ,
90
- count: raw . children. count - index - 1 )
90
+ count: children. count - index - 1 )
91
91
}
92
92
}
93
93
94
94
func appending( _ newChild: RawSyntax ? , arena: SyntaxArena ) -> RawSyntax {
95
- insertingChild ( newChild, at: raw . children. count, arena: arena)
95
+ insertingChild ( newChild, at: children. count, arena: arena)
96
96
}
97
97
98
98
func replacingChildSubrange< C: Collection > (
@@ -101,22 +101,22 @@ struct RawSyntaxLayoutView {
101
101
arena: SyntaxArena
102
102
) -> RawSyntax where C. Element == RawSyntax ? {
103
103
precondition ( !raw. isToken)
104
- let newCount = raw . children. count - range. count + elements. count
104
+ let newCount = children. count - range. count + elements. count
105
105
return . makeLayout( kind: raw. kind,
106
106
uninitializedCount: newCount,
107
107
arena: arena) { buffer in
108
108
if buffer. isEmpty { return }
109
109
var current = buffer. baseAddress!
110
110
111
111
// Intialize
112
- current. initialize ( from: raw . children. baseAddress!, count: range. lowerBound)
112
+ current. initialize ( from: children. baseAddress!, count: range. lowerBound)
113
113
current = current. advanced ( by: range. lowerBound)
114
114
for elem in elements {
115
115
current. initialize ( to: elem)
116
116
current += 1
117
117
}
118
- current. initialize ( from: raw . children. baseAddress!. advanced ( by: range. upperBound) ,
119
- count: raw . children. count - range. upperBound)
118
+ current. initialize ( from: children. baseAddress!. advanced ( by: range. upperBound) ,
119
+ count: children. count - range. upperBound)
120
120
}
121
121
}
122
122
@@ -125,17 +125,50 @@ struct RawSyntaxLayoutView {
125
125
with newChild: RawSyntax ? ,
126
126
arena: SyntaxArena
127
127
) -> RawSyntax {
128
- precondition ( !raw. isToken && raw . children. count > index)
128
+ precondition ( !raw. isToken && children. count > index)
129
129
return . makeLayout( kind: raw. kind,
130
- uninitializedCount: raw . children. count,
130
+ uninitializedCount: children. count,
131
131
arena: arena) { buffer in
132
- _ = buffer. initialize ( from: raw . children)
132
+ _ = buffer. initialize ( from: children)
133
133
buffer [ index] = newChild
134
134
}
135
135
}
136
136
137
137
func formLayoutArray( ) -> [ RawSyntax ? ] {
138
- Array ( raw. children)
138
+ Array ( children)
139
+ }
140
+
141
+ /// Child nodes.
142
+ var children : RawSyntaxBuffer {
143
+ switch raw. rawData. payload {
144
+ case . parsedToken( _) ,
145
+ . materializedToken( _) :
146
+ preconditionFailure ( " RawSyntax must be a layout " )
147
+ case . layout( let dat) :
148
+ return dat. layout
149
+ }
150
+ }
151
+
152
+ func child( at index: Int ) -> RawSyntax ? {
153
+ guard hasChild ( at: index) else { return nil }
154
+ return children [ index]
155
+ }
156
+
157
+ func hasChild( at index: Int ) -> Bool {
158
+ children [ index] != nil
159
+ }
160
+
161
+ /// Returns the child at the provided cursor in the layout.
162
+ /// - Parameter index: The index of the child you're accessing.
163
+ /// - Returns: The child at the provided index.
164
+ subscript< CursorType: RawRepresentable > ( _ index: CursorType ) -> RawSyntax ?
165
+ where CursorType. RawValue == Int {
166
+ return child ( at: index. rawValue)
167
+ }
168
+
169
+ /// The number of children, `present` or `missing`, in this node.
170
+ var numberOfChildren : Int {
171
+ return children. count
139
172
}
140
173
}
141
174
0 commit comments