Skip to content

Helper methods to modify leading and trailing trivia on nodes #91

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 1 commit into from
Mar 11, 2019

Conversation

hartbit
Copy link
Contributor

@hartbit hartbit commented Feb 22, 2019

This is implemented recursively on RawSyntax and through SyntaxData and appropriate public methods were made available on all syntax nodes and collections.

}
set {
if let trailingTrivia = newValue {
self = withTrailingTrivia(trailingTrivia)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I set this to nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tricky one. As the getter returns nil when there is no token in the hierarchy, it doesn't make sense to set nil: it's not like it should remove all tokens form the hierarchy. The only options I thought of are:

  • Have the setter do nothing when setting nil (that's the option I chose). Unfortunately, this can be very surprising:
print(node.leadingTrivia) // [.spaces(1)]
node.leadingTrivia = nil
print(node.leadingTrivia) // [.spaces(1)]
  • Have the setter trap when setting nil. That's another good option AFAIK.
  • Don't implement a setter and only rely on with...Trivia functions. I'm not a fan of this option as we have convenience setter for all other properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making node.leadingTrivia = nil have the same semantics as setting node.leadingTrivia = []? Without looking too much into detail, it's a behaviour I would expect / would be able to explain when it occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's indeed another option, but it still has the same surprising behaviour as noop that the getter won't be returning the same value you just set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, still both values represent some kind of “nothingness”.

It also reminded me of some Cocoa pattern where setting a property to nil caused it to take on a particular value (I think like black for text colour). It's not really relevant here but it shows that having the setter set a different value than is afterwards returned by the getter is not completely unprecedented in the Apple ecosystem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fair, I'll make the change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW that pattern is codified into the language as null_resettable. I think it's perfectly valid here.

@hartbit
Copy link
Contributor Author

hartbit commented Mar 6, 2019

@ahoppen @nkcsgexi @harlanhaskins Any news?

@harlanhaskins
Copy link
Contributor

@swift-ci please test

@harlanhaskins
Copy link
Contributor

Sorry for the delay here, @hartbit!

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

@nkcsgexi nkcsgexi merged commit e7bcc57 into swiftlang:master Mar 11, 2019
@hartbit
Copy link
Contributor Author

hartbit commented Mar 12, 2019

Thanks!!

@hartbit hartbit deleted the syntax-trivia branch March 12, 2019 07:15
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
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.

4 participants