Skip to content

Commit 2866f0e

Browse files
morozkinharlanhaskins
authored andcommitted
[SwiftSyntax] Add replace method to SyntaxCollections (#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 61edd25 commit 2866f0e

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// RUN: %target-run-simple-swift
2+
// REQUIRES: executable_test
3+
// REQUIRES: OS=macosx
4+
// REQUIRES: objc_interop
5+
6+
import Foundation
7+
import StdlibUnittest
8+
import SwiftSyntax
9+
10+
func integerLiteralElement(_ int: Int) -> ArrayElementSyntax {
11+
let literal = SyntaxFactory.makeIntegerLiteral("\(int)")
12+
return SyntaxFactory.makeArrayElement(
13+
expression: SyntaxFactory.makeIntegerLiteralExpr(digits: literal),
14+
trailingComma: nil)
15+
}
16+
17+
var SyntaxCollectionsAPI = TestSuite("SyntaxCollectionsAPI")
18+
19+
SyntaxCollectionsAPI.test("AppendingElement") {
20+
let arrayElementList = SyntaxFactory.makeArrayElementList([
21+
integerLiteralElement(0)
22+
])
23+
24+
let newArrayElementList = arrayElementList.appending(integerLiteralElement(1))
25+
26+
expectEqual(newArrayElementList.count, 2)
27+
expectNotNil(newArrayElementList.child(at: 1))
28+
expectEqual("\(newArrayElementList.child(at: 1)!)", "1")
29+
}
30+
31+
SyntaxCollectionsAPI.test("InsertingElement") {
32+
let arrayElementList = SyntaxFactory.makeArrayElementList([
33+
integerLiteralElement(1)
34+
])
35+
36+
var newArrayElementList = arrayElementList.inserting(integerLiteralElement(0), at: 0)
37+
38+
expectEqual(newArrayElementList.count, 2)
39+
expectNotNil(newArrayElementList.child(at: 0))
40+
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")
41+
42+
newArrayElementList = newArrayElementList.inserting(integerLiteralElement(2), at: 2)
43+
44+
expectEqual(newArrayElementList.count, 3)
45+
expectNotNil(newArrayElementList.child(at: 2))
46+
expectEqual("\(newArrayElementList.child(at: 2)!)", "2")
47+
}
48+
49+
SyntaxCollectionsAPI.test("PrependingElement") {
50+
let arrayElementList = SyntaxFactory.makeArrayElementList([
51+
integerLiteralElement(1)
52+
])
53+
54+
let newArrayElementList = arrayElementList.prepending(integerLiteralElement(0))
55+
56+
expectEqual(newArrayElementList.count, 2)
57+
expectNotNil(newArrayElementList.child(at: 0))
58+
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")
59+
}
60+
61+
SyntaxCollectionsAPI.test("RemovingFirstElement") {
62+
let arrayElementList = SyntaxFactory.makeArrayElementList([
63+
integerLiteralElement(0),
64+
integerLiteralElement(1)
65+
])
66+
67+
let newArrayElementList = arrayElementList.removingFirst()
68+
69+
expectEqual(newArrayElementList.count, 1)
70+
expectNotNil(newArrayElementList.child(at: 0))
71+
expectEqual("\(newArrayElementList.child(at: 0)!)", "1")
72+
}
73+
74+
SyntaxCollectionsAPI.test("RemovingLastElement") {
75+
let arrayElementList = SyntaxFactory.makeArrayElementList([
76+
integerLiteralElement(0),
77+
integerLiteralElement(1)
78+
])
79+
80+
let newArrayElementList = arrayElementList.removingLast()
81+
82+
expectEqual(newArrayElementList.count, 1)
83+
expectNotNil(newArrayElementList.child(at: 0))
84+
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")
85+
}
86+
87+
SyntaxCollectionsAPI.test("RemovingElement") {
88+
let arrayElementList = SyntaxFactory.makeArrayElementList([
89+
integerLiteralElement(0)
90+
])
91+
92+
let newArrayElementList = arrayElementList.removing(childAt: 0)
93+
94+
expectEqual(newArrayElementList.count, 0)
95+
expectNil(newArrayElementList.child(at: 0))
96+
}
97+
98+
SyntaxCollectionsAPI.test("ReplacingElement") {
99+
let arrayElementList = SyntaxFactory.makeArrayElementList([
100+
integerLiteralElement(0),
101+
integerLiteralElement(1),
102+
integerLiteralElement(2)
103+
])
104+
105+
let newArrayElementList = arrayElementList.replacing(childAt: 2,
106+
with: integerLiteralElement(3))
107+
108+
expectNotNil(newArrayElementList.child(at: 2))
109+
expectEqual("\(newArrayElementList.child(at: 2)!)", "3")
110+
}
111+
112+
runAllTests()

tools/SwiftSyntax/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)