Skip to content

Move Format from SwiftSyntaxBuilder into a separate module #830

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 4 commits into from
Sep 26, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Sep 22, 2022

This way we can use Format to determine whether spaces should be inserted after tokens when creating Fix-Its in the parser.

import SwiftSyntax
import SwiftSyntaxBuilder

/// SwiftSyntaxBuilder sources to be generated
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// SwiftSyntaxBuilder sources to be generated
/// SwiftBasicFormat sources to be generated

]

@main
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
struct GenerateSwiftBasicFormat: ParsableCommand {

attributes: nil
)

for (sourceFile, name) in sourceTemplates {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we could move all of this logic from main into a function taking the generation path/url and a [(SourceFile, String)] and share it with generate-swiftsyntaxbuilder?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I wanted to do that but forgot about it while I was working on this PR. Moved it to Utils now. Thanks!

@ahoppen ahoppen changed the title Move Format from SwiftSyntaxBuilder into a separate package Move Format from SwiftSyntaxBuilder into a separate module Sep 22, 2022
@ahoppen ahoppen force-pushed the ahoppen/basic-format-package branch from b0a1e40 to 86c555c Compare September 23, 2022 12:54
@ahoppen
Copy link
Member Author

ahoppen commented Sep 23, 2022

@swift-ci Please test

@ahoppen ahoppen marked this pull request as ready for review September 23, 2022 12:55
var result = syntax
let leadingTrivia = result.leadingTrivia ?? []
if !leadingTrivia.isEmpty {
result = result.withLeadingTrivia(leadingTrivia.addingSpacingAfterNewlinesIfNeeded())
}
return result
}
func format(syntax: DeclNameArgumentSyntax) -> DeclNameArgumentSyntax {
public func format(syntax: DeclNameArgumentSyntax) -> DeclNameArgumentSyntax {
var result = syntax
Copy link
Member

Choose a reason for hiding this comment

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

This code is so repetitive. Can we not put it into an extension of SyntaxProtocol somehow instead of stamping out a hundred copies?

Copy link
Member Author

Choose a reason for hiding this comment

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

Could definitely be something we can investigate in a follow-up PR.

build-script.py Outdated
env = dict(os.environ)
env["SWIFT_BUILD_SCRIPT_ENVIRONMENT"] = "1"
# Tell other projects in the unified build to use local dependencies
env["SWIFT_SYNTAX_PARSER_LIB_SEARCH_PATH"] = \
Copy link
Member

Choose a reason for hiding this comment

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

Can we use SwiftParser now for the code generation?

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by using SwiftParser for code generation? We are using SwiftSyntaxBuilder and I don’t see a reason why SwiftSyntaxBuilder shouldn’t be able to use SwiftParser as well.

Copy link
Member

Choose a reason for hiding this comment

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

I should have been more specific: can we remove the env["SWIFT_SYNTAX_PARSER_LIB_SEARCH_PATH"] bit?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good catch. We can absolutely do that because CodeGeneration doesn’t depend on neither the old nor the new parser.

This way we can use `Format` to format Fix-Its in the parser

Share generate command between generate-swiftbasicformat and generate-swiftsyntaxbuilder
@ahoppen ahoppen force-pushed the ahoppen/basic-format-package branch from 5ab399a to 9965cbb Compare September 26, 2022 07:59
@ahoppen
Copy link
Member Author

ahoppen commented Sep 26, 2022

@swift-ci Please test

@ahoppen ahoppen merged commit 79e979f into swiftlang:main Sep 26, 2022
@ahoppen ahoppen deleted the ahoppen/basic-format-package branch September 26, 2022 13:26
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