Skip to content

Add ability to attach leading trivia directly to buildable nodes #475

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
Jun 28, 2022

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Jun 25, 2022

Based on #474.

This branch adds the ability to attach leading trivia directly to BuildableNodes as a parameter/attribute (currently this can only be done by passing it as a parameter directly to the build syntax method), thereby addressing the points outlined here. This is particularly convenient in TokensFile, where we can now attach doc comments directly to the nodes:

ExtensionDecl(
  leadingTrivia: .newlines(1) + .docLineComment("/// Namespace for commonly used tokens with default trivia.") + .newlines(1),
  modifiers: [TokenSyntax.public],
  extendedType: "TokenSyntax"
)

rather than having to modify the first token (as done currently):

ExtensionDecl(
  modifiers: [TokenSyntax.public.withLeadingTrivia(.newlines(1) + .docLineComment("/// Namespace for commonly used tokens with default trivia.") + .newlines(1))],
  extendedType: "TokenSyntax"
)

thereby making the DSL more readable and more aligned with the actual generated code.

cc @ahoppen @kimdv

@fwcd
Copy link
Member Author

fwcd commented Jun 25, 2022

@swift-ci please test

@kimdv
Copy link
Contributor

kimdv commented Jun 25, 2022

I like the API. Clean and easy to understand 👏

Was wondering if it would make sense to add a single test that tests if we combine the leading leadingTrivia with a leading trivia on a modifier etc?
Just to ensure that works and will not break 😁

VariableDecl(
       leadingTrivia: .newlines(1) + .docLineComment("/// The `open` contextual token") + .newlines(1),
       modifiers: [TokenSyntax.static.withLeadingTrivia(.newlines(1) )],
       letOrVarKeyword: .var
     )

@fwcd
Copy link
Member Author

fwcd commented Jun 26, 2022

@swift-ci please test

@fwcd fwcd marked this pull request as ready for review June 26, 2022 03:06
@fwcd fwcd requested a review from ahoppen as a code owner June 26, 2022 03:06
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Yes, this really improves readability of TokensFile.swift 👍

${',\n '.join(delegated_init_args)}
)
}
% end

func build${type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${type.syntax()} {
func build${type.base_name()}(format: Format, leadingTrivia additionalLeadingTrivia: Trivia? = nil) -> ${type.syntax()} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are defaulting to ?? [] should we not add it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think that's a good idea. I kept it optional to be consistent with the other build-methods, should those be refactored too?

Copy link
Member Author

@fwcd fwcd Jun 27, 2022

Choose a reason for hiding this comment

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

I think the SyntaxBuildable protocol (and its related subprotocols) currently require this to be optional, so we'd have to refactor that (which might be a larger change).

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest that we keep it as-is for now and could consider refactoring it to be non-optional in a follow-up PR.

fwcd added 2 commits June 27, 2022 16:02
- Simplify leading trivia attachment in syntax-building method
- Add some doc comments regarding leading trivia on buildable nodes
- Add test case for leading trivia to verify that it is combined
  correctly
@fwcd fwcd force-pushed the buildable-nodes-leading-trivia branch from a2e5bc9 to 70f46d2 Compare June 27, 2022 14:03
@fwcd
Copy link
Member Author

fwcd commented Jun 27, 2022

@swift-ci please test

@fwcd fwcd merged commit ead746b into swiftlang:main Jun 28, 2022
@fwcd fwcd deleted the buildable-nodes-leading-trivia branch June 28, 2022 13:02
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.

3 participants