Skip to content

Commit eeff165

Browse files
committed
Deprecate SyntaxCollection.replacing(childAt:with:) in favor of a writable subscript
1 parent 7b07fcf commit eeff165

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/// The data for an index in a syntax children collection that is not the end
1616
/// index. See ``SyntaxChildrenIndex`` for the representation of the end index.
17-
struct SyntaxChildrenIndexData: Comparable {
17+
struct SyntaxChildrenIndexData: Hashable, Comparable {
1818
/// The UTF-8 offset of the item at this index in the source file
1919
/// See `AbsoluteSyntaxPosition.offset`
2020
let offset: UInt32
@@ -50,7 +50,7 @@ struct SyntaxChildrenIndexData: Comparable {
5050
}
5151

5252
/// An index in a syntax children collection.
53-
public struct SyntaxChildrenIndex: Comparable, ExpressibleByNilLiteral {
53+
public struct SyntaxChildrenIndex: Hashable, Comparable, ExpressibleByNilLiteral {
5454
/// Construct the `endIndex` of a ``SyntaxChildren`` collection.
5555
public init(nilLiteral: ()) {
5656
self.data = nil

Sources/SwiftSyntax/SyntaxCollection.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ extension SyntaxCollection {
113113
/// - syntax: The element to replace with.
114114
///
115115
/// - Returns: A new collection with the new element at the provided index.
116+
@available(*, deprecated, message: "Use .with(\\.[index], newValue) instead")
116117
public func replacing(childAt index: Int, with syntax: Element) -> Self {
117118
var newLayout = layoutView.formLayoutArray()
118119
/// Make sure the index is a valid index for replacing
@@ -211,9 +212,24 @@ extension SyntaxCollection {
211212
}
212213

213214
public subscript(position: SyntaxChildrenIndex) -> Element {
214-
let (raw, info) = rawChildren[position]
215-
let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)
216-
let data = SyntaxData(absoluteRaw, parent: Syntax(self))
217-
return Syntax(data).cast(Element.self)
215+
get {
216+
let (raw, info) = rawChildren[position]
217+
let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)
218+
let data = SyntaxData(absoluteRaw, parent: Syntax(self))
219+
return Syntax(data).cast(Element.self)
220+
}
221+
set {
222+
guard let indexToReplace = (position.data?.indexInParent).map(Int.init) else {
223+
preconditionFailure("Cannot replace element at the end index")
224+
}
225+
var newLayout = layoutView.formLayoutArray()
226+
/// Make sure the index is a valid index for replacing
227+
precondition(
228+
(newLayout.startIndex..<newLayout.endIndex).contains(indexToReplace),
229+
"replacing node at invalid index \(index)"
230+
)
231+
newLayout[indexToReplace] = newValue.raw
232+
self = replacingLayout(newLayout)
233+
}
218234
}
219235
}

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
340340
return DeclSyntax(
341341
visitedVarDecl.with(
342342
\.bindings,
343-
visitedVarDecl.bindings.replacing(
344-
childAt: 0,
345-
with: binding.with(
343+
visitedVarDecl.bindings.with(
344+
\.[visitedVarDecl.bindings.startIndex],
345+
binding.with(
346346
\.accessor,
347347
.accessors(
348348
.init(

Sources/SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
339339
return DeclSyntax(
340340
visitedVarDecl.with(
341341
\.bindings,
342-
visitedVarDecl.bindings.replacing(
343-
childAt: 0,
344-
with: binding.with(
342+
visitedVarDecl.bindings.with(
343+
\.[visitedVarDecl.bindings.startIndex],
344+
binding.with(
345345
\.accessor,
346346
.accessors(
347347
.init(

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ private func replaceFirstLabel(
5757
return tuple
5858
}
5959

60-
return tuple.replacing(
61-
childAt: 0,
62-
with: firstElement.with(\.label, .identifier(newLabel))
60+
return tuple.with(
61+
\.[tuple.startIndex],
62+
firstElement.with(\.label, .identifier(newLabel))
6363
)
6464
}
6565

Tests/SwiftSyntaxTest/SyntaxCollectionsTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ public class SyntaxCollectionsTests: XCTestCase {
110110
integerLiteralElement(2),
111111
])
112112

113-
let newArrayElementList = arrayElementList.replacing(
114-
childAt: 2,
115-
with: integerLiteralElement(3)
113+
let lastElementIndex = arrayElementList.index(arrayElementList.startIndex, offsetBy: 2)
114+
115+
let newArrayElementList = arrayElementList.with(
116+
\.[lastElementIndex],
117+
integerLiteralElement(3)
116118
)
117119

118120
XCTAssertNotNil(newArrayElementList.child(at: 2))

0 commit comments

Comments
 (0)