Skip to content

Factor out all leadingTrivia logic into Format #645

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 5 commits into from
Aug 28, 2022

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Aug 27, 2022

This PR moves the entire leadingTrivia logic into Format, thereby allowing for a cleaner separation of concerns: The build... methods in BuildableNodes/BuildableCollectionNodes only generate the syntax, the format._format methods then deal with indentation.

In particular, the leadingTrivia parameter that was passed alongside format: Format is no longer needed. Since many test cases, however, rely on this ad-hoc leading trivia generation, I have migrated as many tests as possible to the leadingTrivia-as-field. For smaller nodes, such as literals, which are heavily sugared through their respective convenience initializers, this would become too verbose, therefore I have removed the unexpected trivia node in those cases. Feel free to discuss this though!

Some notes:

  • The use of @_spi(Testing) for currently public indentation methods/properties on Format will come in a future PR since it depends on Add attributes to VariableDecl convenience initializers #644 and is mostly orthogonal to the changes in this PR.
  • The rationale for not passing the leading trivia back to the build... methods is that this becomes complicated for children that may or may not require leading newlines and due to some other complications such as not being able to switch over the parent's SyntaxEnum before constructing its syntax (using SyntaxKind would have been an option, but would have required us to use the low-level SPI). In general, I think the overloaded _format methods are the more elegant solution here, also since we don't add a lot of unneeded empty trivias, but I would be curious to hear your thoughts on this!
  • Performance is pretty much exactly as before, I measure 7-8 seconds of generation time both on this branch and main on debug builds of generate-swift-syntax-builder.
  • The most interesting diff in this PR is probably Sources/SwiftSyntaxBuilder/generated/Format.swift.

fwcd added 5 commits August 27, 2022 03:23
- Use format method in buildable nodes
- Introduce a new variable for the built syntax for readability
- Attach stored leadingTrivia separately in buildable nodes
- Generate format methods for collection nodes
- Move leading trivia fully into Format
Since we no longer pass a separate leadingTrivia through the syntax
build, we attach the leadingTrivia directly to the buildable node where
possible and omit it elsewhere.
@fwcd fwcd requested a review from ahoppen as a code owner August 27, 2022 02:41
@fwcd
Copy link
Member Author

fwcd commented Aug 27, 2022

@swift-ci please test

@fwcd
Copy link
Member Author

fwcd commented Aug 27, 2022

@swift-ci please test macOS

@@ -7,9 +7,12 @@ final class ImportTests: XCTestCase {
let leadingTrivia = Trivia.unexpectedText("␣")
let identifier = TokenSyntax.identifier("SwiftSyntax")

let importDecl = ImportDecl(path: AccessPath([AccessPathComponent(name: identifier)]))
let importDecl = ImportDecl(
leadingTrivia: leadingTrivia,
Copy link
Member

Choose a reason for hiding this comment

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

Actually, I don’t know why we always add the as leading trivia here. I think it’s something that was done in one of the first test cases and then just carried over to all the other tests. I would be totally fine to remove from all test cases because it’s rather weird anyway. I’m fine to keep it as-is in this PR though.

@fwcd fwcd merged commit e8f596c into swiftlang:main Aug 28, 2022
@fwcd fwcd deleted the fwcd/format-leading-trivia branch August 28, 2022 10:00
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