-
Notifications
You must be signed in to change notification settings - Fork 412
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
indexInParent
replacement for use with replacing(childAt
#1872
Comments
Tracked in Apple’s issue tracker as rdar://111591397 |
You should be able to get the index of the element you want to replace when finding the element that contains “Foo”. This is similar to what you would do in a normal Swift array as well. override func visit(_ node: ImportDeclSyntax) -> DeclSyntax {
guard let (index, importAccessPathSyntax) = node.path.enumerated().first(where: { $0.element.name.text.contains("Foo") }) else {
return super.visit(node)
}
let newToken = importAccessPathSyntax.name.with(\.tokenKind, .stringSegment("Bar"))
let newPath = node.path.replacing(childAt: index, with: importAccessPathSyntax.with(\.name, newToken))
let newNode = node.with(\.path, newPath)
correctionPositions.append(importAccessPathSyntax.positionAfterSkippingLeadingTrivia)
return super.visit(newNode)
} Thinking about this a little bit more, though, |
I'm wondering if the issue was completely resolved in #1880? |
I still have two small small items on my to-do list to resolve this. I think I should get to them this week. So: no, not quite resolved yet. |
Description
I had some SwiftSyntax code that did:
In order to replace imports of
Foo
withBar
. I was able to do this without affecting any trivia using this approach, but it seems like with recent changes on the release/5.9 branch there is no way to get the equivalent ofimportAccessPathSyntax.indexInParent
anymore. I attempted to useimportAccessPathSyntax.keyPathInParent
as a replacement here but in this case that results innil
. Should that be the new option? Shouldreplacing(childAt:
takeSyntaxChildrenIndex
now instead of anInt
? In the meantime I worked around this case by assuming that0
was the right index always:The text was updated successfully, but these errors were encountered: