Skip to content

Generate Format using SwiftSyntaxBuilder #611

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 2 commits into from
Aug 21, 2022

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Aug 19, 2022

In preparation for some a small refactor to the formatting/leading trivia generation, this PR ports Format.swift to a file generated by SwiftSyntaxBuilder's DSL.

fwcd added 2 commits August 19, 2022 19:43
- Add FormatFile template
- Add Format extension
- Bootstrap Format
@fwcd fwcd requested a review from ahoppen as a code owner August 19, 2022 18:35
@fwcd
Copy link
Member Author

fwcd commented Aug 19, 2022

@swift-ci please test

@fwcd
Copy link
Member Author

fwcd commented Aug 19, 2022

Some observations I've made while tinkering on FormatFile that aren't directly related to this PR, but to the future plan of making Format responsible for generating leading trivia based on a node rather than the generated build... methods:

  • We can't determine requiresLeadingNewline solely based on a passed node, since it is a property of Child rather than Node
  • Even in collection nodes, the newline trivia attached to the elements crucially depends on the fact that the parent is a collection
  • _makeIndent() has to be public or at least internal to be testable (so we cannot fully abstract these internals away, but that shouldn't be much of an issue AFAICT)

My current experiments can be found here and here.

Since the node-child relationship determines most the newline-related formatting anyway, I am not entirely sure whether this approach would really be feasible. Any thoughts?

cc @ahoppen

@ahoppen
Copy link
Member

ahoppen commented Aug 21, 2022

  • We can't determine requiresLeadingNewline solely based on a passed node, since it is a property of Child rather than Node
  • Even in collection nodes, the newline trivia attached to the elements crucially depends on the fact that the parent is a collection

Since we SwiftSyntaxBuilder builds the tree top-down, I think we would pass the parent’s SyntaxKind to _leadingTrivia. That way you should be able to determine all the child properties, I believe.

  • _makeIndent() has to be public or at least internal to be testable (so we cannot fully abstract these internals away, but that shouldn't be much of an issue AFAICT)

You could use @_spi(Testing) like we do here: https://github.com/apple/swift-syntax/blob/6ce8c4871484da39be1e92b04ca885461c1995b0/Sources/SwiftSyntax/gyb_generated/TokenKind.swift#L315

@fwcd fwcd merged commit 1313c36 into swiftlang:main Aug 21, 2022
@fwcd fwcd deleted the generate-format-file branch August 21, 2022 15:43
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.

2 participants