Skip to content

Commit 9c27f97

Browse files
morozkinharlanhaskins
authored andcommitted
[SwiftSyntax] Add replace method to SyntaxCollections (swiftlang#15331)
* [SwiftSyntax] Add replace method SyntaxCollections * Change replaceSubrange method call to subscript setter call * [SwiftSyntax] Add test for SyntaxCollections * Fix mistakes in SyntaxCollections tests * Use syntax's raw value instead of syntax itself in replacing method * Update SyntaxCollections tests
1 parent c3522a4 commit 9c27f97

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

SyntaxCollections.swift.gyb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ public struct ${node.name}: _SyntaxBase {
9393
return replacingLayout(newLayout)
9494
}
9595

96+
/// Creates a new `${node.name}` by replacing the syntax element
97+
/// at the provided index.
98+
///
99+
/// - Parameters:
100+
/// - index: The index at which to replace the element in the collection.
101+
/// - syntax: The element to replace with.
102+
///
103+
/// - Returns: A new `${node.name}` with the new element at the provided index.
104+
public func replacing(childAt index: Int,
105+
with syntax: ${node.collection_element_type}) -> ${node.name} {
106+
var newLayout = data.raw.layout
107+
/// Make sure the index is a valid index for replacing
108+
precondition((newLayout.startIndex..<newLayout.endIndex).contains(index),
109+
"replacing node at invalid index \(index)")
110+
newLayout[index] = syntax.raw
111+
return replacingLayout(newLayout)
112+
}
113+
96114
/// Creates a new `${node.name}` by removing the syntax element at the
97115
/// provided index.
98116
///

0 commit comments

Comments
 (0)