Skip to content

Change trivia attribution rule to make comments trailing trivia unless separated by a newline #985

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
Oct 20, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 19, 2022

This changes the trivia attribution rule in the new parser to consider all trivia up to the next newline as trailing trivia. Previously, comments were always considered trailing trivia.

This was discussed last year in https://forums.swift.org/t/changing-comment-trivia-attribution-from-trailing-trivia-to-leading-trivia/50773.

My idea is that right now is probably one of the best times to change this because switching from the old to the new parser is an active source change for all clients.

Resolves rdar://68234477
Resolves rdar://95639215
Fixes #438

CC @allevato @jpsim

…s separated by a newline

This changes the trivia attribution rule in the new parser to consider all trivia up to the next newline as trailing trivia. Previously, comments were always considered trailing trivia.

This was discussed last year in https://forums.swift.org/t/changing-comment-trivia-attribution-from-trailing-trivia-to-leading-trivia/50773.

My idea is that right now is probably one of the best times to change this because switching from the old to the new parser is an active source change for all clients.

Resolves rdar://68234477
Resolves rdar://95639215
Fixes swiftlang#438
@ahoppen ahoppen force-pushed the ahoppen/change-trivia-rule branch from f479a9e to 60c7037 Compare October 19, 2022 19:24
@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

@swift-ci Please test

Copy link
Contributor

@CodaFi CodaFi left a comment

Choose a reason for hiding this comment

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

We met to discuss the implications of this change and settled on accepting it. The forum thread contains a number of oddities that these rules still don't quite handle as well as one would hope, but the balance between "smarter" trivia attribution and an efficient trivia lexing scheme struck by this solution is better than the status quo in enough places that it's worth it.

This is also a behavior change that swiftlint and swift-format may have to adapt to.

@CodaFi
Copy link
Contributor

CodaFi commented Oct 20, 2022

CC @jpsim and @allevato you may want to check this out and see if your respective projects behaviors change under this patch.

@ahoppen ahoppen merged commit 0663a53 into swiftlang:main Oct 20, 2022
@ahoppen ahoppen deleted the ahoppen/change-trivia-rule branch October 20, 2022 07:11
allevato added a commit to allevato/swift-format that referenced this pull request Oct 21, 2022
As of swiftlang/swift-syntax#985, the parser treats
all trivia following a token up to the next line break as trailing trivia,
including comments. This is a change from the former logic, which treated
such comments as leading trivia of the following token.

A great deal of the logic in swift-format was written in terms of that
behavior, and attempts to rewrite it to use the new behavior instead have
been difficult to get right in all cases. In the interest of expediency,
I'm merging this workaround that shifts the trivia around to restore the
old tree that we expected.

This shouldn't be considered a long-term fix; we should eventually fix
the misbehavior with the new layout and drop the extra mutation.
jpsim added a commit to realm/SwiftLint that referenced this pull request Oct 23, 2022
This has changes to how comments are associated to nodes.

See swiftlang/swift-syntax#985
jpsim added a commit to realm/SwiftLint that referenced this pull request Oct 23, 2022
This has changes to how comments are associated to nodes.

See swiftlang/swift-syntax#985
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.

[SR-9616] SwiftSyntax is not following trivia rule
2 participants