Skip to content

Commit 0bd2a33

Browse files
committed
Move children accessors from RawSyntax to RawSyntaxLayoutView
1 parent c88ee9b commit 0bd2a33

20 files changed

+2950
-1864
lines changed

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -170,31 +170,6 @@ extension RawSyntax {
170170
}
171171
}
172172

173-
/// Child nodes.
174-
var children: RawSyntaxBuffer {
175-
switch rawData.payload {
176-
case .parsedToken(_),
177-
.materializedToken(_):
178-
return .init(start: nil, count: 0)
179-
case .layout(let dat):
180-
return dat.layout
181-
}
182-
}
183-
184-
func child(at index: Int) -> RawSyntax? {
185-
guard hasChild(at: index) else { return nil }
186-
return children[index]
187-
}
188-
189-
func hasChild(at index: Int) -> Bool {
190-
children[index] != nil
191-
}
192-
193-
/// The number of children, `present` or `missing`, in this node.
194-
var numberOfChildren: Int {
195-
return children.count
196-
}
197-
198173
/// Total number of nodes in this sub-tree, including `self` node.
199174
var totalNodes: Int {
200175
switch rawData.payload {
@@ -254,7 +229,7 @@ extension RawSyntax {
254229
trailingTrivia: tokenView.formTrailingTrivia(),
255230
arena: arena)
256231
case .layout(let layoutView):
257-
for (index, child) in children.enumerated() {
232+
for (index, child) in layoutView.children.enumerated() {
258233
if let replaced = child?.withLeadingTrivia(leadingTrivia) {
259234
return layoutView.replacingChild(at: index, with: replaced, arena: arena)
260235
}
@@ -276,22 +251,14 @@ extension RawSyntax {
276251
trailingTrivia: trailingTrivia,
277252
arena: arena)
278253
case .layout(let layoutView):
279-
for (index, child) in children.enumerated().reversed() {
254+
for (index, child) in layoutView.children.enumerated().reversed() {
280255
if let replaced = child?.withTrailingTrivia(trailingTrivia) {
281256
return layoutView.replacingChild(at: index, with: replaced, arena: arena)
282257
}
283258
}
284259
return nil
285260
}
286261
}
287-
288-
/// Returns the child at the provided cursor in the layout.
289-
/// - Parameter index: The index of the child you're accessing.
290-
/// - Returns: The child at the provided index.
291-
subscript<CursorType: RawRepresentable>(_ index: CursorType) -> RawSyntax?
292-
where CursorType.RawValue == Int {
293-
return child(at: index.rawValue)
294-
}
295262
}
296263

297264
extension RawSyntax {
@@ -341,8 +308,8 @@ extension RawSyntax {
341308
switch view {
342309
case .token(let tokenView):
343310
return tokenView
344-
case .layout:
345-
for child in children {
311+
case .layout(let layoutView):
312+
for child in layoutView.children {
346313
if let token = child?.firstToken(viewMode: viewMode) {
347314
return token
348315
}
@@ -357,8 +324,8 @@ extension RawSyntax {
357324
switch view {
358325
case .token(let tokenView):
359326
return tokenView
360-
case .layout:
361-
for child in children.reversed() {
327+
case .layout(let layoutView):
328+
for child in layoutView.children.reversed() {
362329
if let token = child?.lastToken(viewMode: viewMode) {
363330
return token
364331
}
@@ -676,8 +643,15 @@ extension RawSyntax: CustomDebugStringConvertible {
676643

677644
extension RawSyntax: CustomReflectable {
678645
public var customMirror: Mirror {
679-
let mirrorChildren: [Any] = children.map {
680-
child in child ?? (nil as Any?) as Any
646+
647+
let mirrorChildren: [Any]
648+
switch view {
649+
case .token:
650+
mirrorChildren = []
651+
case .layout(let layoutView):
652+
mirrorChildren = layoutView.children.map {
653+
child in child ?? (nil as Any?) as Any
654+
}
681655
}
682656
return Mirror(self, unlabeledChildren: mirrorChildren)
683657
}

Sources/SwiftSyntax/Raw/RawSyntaxLayoutView.swift

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ struct RawSyntaxLayoutView {
5555
at index: Int,
5656
arena: SyntaxArena
5757
) -> RawSyntax {
58-
precondition(raw.children.count >= index)
58+
precondition(children.count >= index)
5959
return .makeLayout(kind: raw.kind,
60-
uninitializedCount: raw.children.count + 1,
60+
uninitializedCount: children.count + 1,
6161
arena: arena) { buffer in
62-
var childIterator = raw.children.makeIterator()
62+
var childIterator = children.makeIterator()
6363
let base = buffer.baseAddress!
6464
for i in 0..<buffer.count {
6565
base.advanced(by: i)
@@ -72,27 +72,27 @@ struct RawSyntaxLayoutView {
7272
at index: Int,
7373
arena: SyntaxArena
7474
) -> 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
7777
return .makeLayout(kind: raw.kind,
7878
uninitializedCount: count,
7979
arena: arena) { buffer in
8080
if buffer.isEmpty { return }
8181
let newBase = buffer.baseAddress!
82-
let oldBase = raw.children.baseAddress!
82+
let oldBase = children.baseAddress!
8383

8484
// Copy elements up to the index.
8585
newBase.initialize(from: oldBase, count: index)
8686

8787
// Copy elements from the index + 1.
8888
newBase.advanced(by: index)
8989
.initialize(from: oldBase.advanced(by: index + 1),
90-
count: raw.children.count - index - 1)
90+
count: children.count - index - 1)
9191
}
9292
}
9393

9494
func appending(_ newChild: RawSyntax?, arena: SyntaxArena) -> RawSyntax {
95-
insertingChild(newChild, at: raw.children.count, arena: arena)
95+
insertingChild(newChild, at: children.count, arena: arena)
9696
}
9797

9898
func replacingChildSubrange<C: Collection>(
@@ -101,22 +101,22 @@ struct RawSyntaxLayoutView {
101101
arena: SyntaxArena
102102
) -> RawSyntax where C.Element == RawSyntax? {
103103
precondition(!raw.isToken)
104-
let newCount = raw.children.count - range.count + elements.count
104+
let newCount = children.count - range.count + elements.count
105105
return .makeLayout(kind: raw.kind,
106106
uninitializedCount: newCount,
107107
arena: arena) { buffer in
108108
if buffer.isEmpty { return }
109109
var current = buffer.baseAddress!
110110

111111
// Intialize
112-
current.initialize(from: raw.children.baseAddress!, count: range.lowerBound)
112+
current.initialize(from: children.baseAddress!, count: range.lowerBound)
113113
current = current.advanced(by: range.lowerBound)
114114
for elem in elements {
115115
current.initialize(to: elem)
116116
current += 1
117117
}
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)
120120
}
121121
}
122122

@@ -125,17 +125,50 @@ struct RawSyntaxLayoutView {
125125
with newChild: RawSyntax?,
126126
arena: SyntaxArena
127127
) -> RawSyntax {
128-
precondition(!raw.isToken && raw.children.count > index)
128+
precondition(!raw.isToken && children.count > index)
129129
return .makeLayout(kind: raw.kind,
130-
uninitializedCount: raw.children.count,
130+
uninitializedCount: children.count,
131131
arena: arena) { buffer in
132-
_ = buffer.initialize(from: raw.children)
132+
_ = buffer.initialize(from: children)
133133
buffer[index] = newChild
134134
}
135135
}
136136

137137
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
139172
}
140173
}
141174

Sources/SwiftSyntax/Raw/RawSyntaxNodes.swift.gyb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public protocol Raw${node.name}NodeProtocol: Raw${node.base_type}NodeProtocol {}
2929

3030
@_spi(RawSyntax)
3131
public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_type}NodeProtocol {
32+
var layoutView: RawSyntaxLayoutView {
33+
return raw.layoutView!
34+
}
35+
3236
public static func isKindOf(_ raw: RawSyntax) -> Bool {
3337
% if node.is_base():
3438
% sub_kinds = ['.' + n.swift_syntax_kind for n in SYNTAX_NODES if n.base_kind == node.syntax_kind]
@@ -72,7 +76,7 @@ public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_t
7276
}
7377

7478
public var elements: [Raw${node.collection_element_type}] {
75-
raw.children.map { Raw${node.collection_element_type}(raw: $0!) }
79+
layoutView.children.map { Raw${node.collection_element_type}(raw: $0!) }
7680
}
7781
% end
7882
%
@@ -107,9 +111,9 @@ public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_t
107111
% iuo_mark = "!" if not child.is_optional else ""
108112
public var ${child.swift_name}: Raw${child.type_name + optional_mark} {
109113
% if child.type_name == "Syntax":
110-
raw.children[${idx}]${iuo_mark}
114+
layoutView.children[${idx}]${iuo_mark}
111115
% else:
112-
raw.children[${idx}].map(Raw${child.type_name}.init(raw:))${iuo_mark}
116+
layoutView.children[${idx}].map(Raw${child.type_name}.init(raw:))${iuo_mark}
113117
% end
114118
}
115119
% end

0 commit comments

Comments
 (0)