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

Conversation

morozkin
Copy link
Contributor

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)

@morozkin morozkin changed the title [SwiftSyntax] Add replace method SyntaxCollections [SwiftSyntax] Add replace method to SyntaxCollections Mar 18, 2018
@nkcsgexi nkcsgexi requested a review from harlanhaskins March 19, 2018 03:43
Copy link
Contributor

@harlanhaskins harlanhaskins left a 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])
Copy link
Contributor

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?

@morozkin
Copy link
Contributor Author

@harlanhaskins I added tests for all types of mutation methods of SyntaxCollections


SyntaxCollectionsAPI.test("AppendingElement") {
let arrayElementList = SyntaxFactory.makeArrayElementList([
SyntaxFactory.makeArrayElement(expression: SyntaxFactory.makeIntegerLiteral("0"), trailingComma: nil)
Copy link
Contributor

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.

@harlanhaskins
Copy link
Contributor

Structurally, the tests look great!

Copy link
Contributor

@harlanhaskins harlanhaskins left a 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!

@harlanhaskins
Copy link
Contributor

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor

@swift-ci please smoke test

@morozkin
Copy link
Contributor Author

@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

@harlanhaskins
Copy link
Contributor

No need to apologize 😊

I’ll re-run the tests now and we can hopefully get this merged!

@swift-ci please smoke test

@nkcsgexi
Copy link
Contributor

@morozkin there's no need to feel bad. People rely on CI to find build errors all the time 😄

@harlanhaskins
Copy link
Contributor

I certainly do!

@harlanhaskins
Copy link
Contributor

@harlanhaskins harlanhaskins merged commit 2866f0e into swiftlang:master Mar 20, 2018
@harlanhaskins
Copy link
Contributor

Thank you, @morozkin!

maldahleh pushed a commit to maldahleh/swift that referenced this pull request Oct 26, 2020
* [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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants