-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
[SwiftSyntax] Add replace method to SyntaxCollections #15331
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this! If you could add a test, that’d be great!
/// Make sure the index is a valid index for replacing | ||
precondition((newLayout.startIndex..<newLayout.endIndex).contains(index), | ||
"replacing node at invalid index \(index)") | ||
newLayout.replaceSubrange(index...index, with: [syntax]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be newLayout[index] = syntax.raw
?
@harlanhaskins I added tests for all types of mutation methods of |
|
||
SyntaxCollectionsAPI.test("AppendingElement") { | ||
let arrayElementList = SyntaxFactory.makeArrayElementList([ | ||
SyntaxFactory.makeArrayElement(expression: SyntaxFactory.makeIntegerLiteral("0"), trailingComma: nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makeIntegerLiteral
returns a TokenSyntax
, not an ExprSyntax
. Can you rewrite this to use makeIntegerLiteralExpr
?
You might want to make a helper:
func integerLiteralElement(_ int: Int) -> ArrayElementSyntax {
let literal = SyntaxFactory.makeIntegerLiteral("\(int)")
return SyntaxFactory.makeArrayElement(
expression: SyntaxFactory.makeIntegerLiteralExpr(digits: literal),
trailingComma: nil)
}
That way you always know you're constructing a full ArrayElementSyntax
.
Structurally, the tests look great! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you so much!
@swift-ci please smoke test |
@swift-ci please smoke test |
@harlanhaskins I'm sorry for the quality of my previous commits. I had errors with compilation of the swift so I didn't run and check that my code compiles and runs well. But with my latest commit I compiled the swift with my changes and checked that all tests passed successfully |
No need to apologize 😊 I’ll re-run the tests now and we can hopefully get this merged! @swift-ci please smoke test |
@morozkin there's no need to feel bad. People rely on CI to find build errors all the time 😄 |
I certainly do! |
⛵ |
Thank you, @morozkin! |
* [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
I started working closely with SwiftSyntax and realized that I very often face with the situation where I need to replace one element in some kind of SyntaxCollection and for doing such operation we have to call two different methods: one for delete operation and one for insert operation.
This is a typical scenario where you need to update one element in the
ArrayExprSyntax
:arrayExpr.elements.removing(childAt: i).inserting(newArrayElement, at: i)
Instead of this with the new method we can rewrite it in the following way:
arrayExpr.elements.replacing(childAt: i, with: newArrayElement)