Skip to content

[SwiftSyntax] Add replace method to SyntaxCollections #15331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions test/SwiftSyntax/SyntaxCollections.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: OS=macosx
// REQUIRES: objc_interop

import Foundation
import StdlibUnittest
import SwiftSyntax

func integerLiteralElement(_ int: Int) -> ArrayElementSyntax {
let literal = SyntaxFactory.makeIntegerLiteral("\(int)")
return SyntaxFactory.makeArrayElement(
expression: SyntaxFactory.makeIntegerLiteralExpr(digits: literal),
trailingComma: nil)
}

var SyntaxCollectionsAPI = TestSuite("SyntaxCollectionsAPI")

SyntaxCollectionsAPI.test("AppendingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(0)
])

let newArrayElementList = arrayElementList.appending(integerLiteralElement(1))

expectEqual(newArrayElementList.count, 2)
expectNotNil(newArrayElementList.child(at: 1))
expectEqual("\(newArrayElementList.child(at: 1)!)", "1")
}

SyntaxCollectionsAPI.test("InsertingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(1)
])

var newArrayElementList = arrayElementList.inserting(integerLiteralElement(0), at: 0)

expectEqual(newArrayElementList.count, 2)
expectNotNil(newArrayElementList.child(at: 0))
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")

newArrayElementList = newArrayElementList.inserting(integerLiteralElement(2), at: 2)

expectEqual(newArrayElementList.count, 3)
expectNotNil(newArrayElementList.child(at: 2))
expectEqual("\(newArrayElementList.child(at: 2)!)", "2")
}

SyntaxCollectionsAPI.test("PrependingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(1)
])

let newArrayElementList = arrayElementList.prepending(integerLiteralElement(0))

expectEqual(newArrayElementList.count, 2)
expectNotNil(newArrayElementList.child(at: 0))
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")
}

SyntaxCollectionsAPI.test("RemovingFirstElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(0),
integerLiteralElement(1)
])

let newArrayElementList = arrayElementList.removingFirst()

expectEqual(newArrayElementList.count, 1)
expectNotNil(newArrayElementList.child(at: 0))
expectEqual("\(newArrayElementList.child(at: 0)!)", "1")
}

SyntaxCollectionsAPI.test("RemovingLastElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(0),
integerLiteralElement(1)
])

let newArrayElementList = arrayElementList.removingLast()

expectEqual(newArrayElementList.count, 1)
expectNotNil(newArrayElementList.child(at: 0))
expectEqual("\(newArrayElementList.child(at: 0)!)", "0")
}

SyntaxCollectionsAPI.test("RemovingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(0)
])

let newArrayElementList = arrayElementList.removing(childAt: 0)

expectEqual(newArrayElementList.count, 0)
expectNil(newArrayElementList.child(at: 0))
}

SyntaxCollectionsAPI.test("ReplacingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
integerLiteralElement(0),
integerLiteralElement(1),
integerLiteralElement(2)
])

let newArrayElementList = arrayElementList.replacing(childAt: 2,
with: integerLiteralElement(3))

expectNotNil(newArrayElementList.child(at: 2))
expectEqual("\(newArrayElementList.child(at: 2)!)", "3")
}

runAllTests()
18 changes: 18 additions & 0 deletions tools/SwiftSyntax/SyntaxCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ public struct ${node.name}: _SyntaxBase {
return replacingLayout(newLayout)
}

/// Creates a new `${node.name}` by replacing the syntax element
/// at the provided index.
///
/// - Parameters:
/// - index: The index at which to replace the element in the collection.
/// - syntax: The element to replace with.
///
/// - Returns: A new `${node.name}` with the new element at the provided index.
public func replacing(childAt index: Int,
with syntax: ${node.collection_element_type}) -> ${node.name} {
var newLayout = data.raw.layout
/// Make sure the index is a valid index for replacing
precondition((newLayout.startIndex..<newLayout.endIndex).contains(index),
"replacing node at invalid index \(index)")
newLayout[index] = syntax.raw
return replacingLayout(newLayout)
}

/// Creates a new `${node.name}` by removing the syntax element at the
/// provided index.
///
Expand Down