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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
let ${child.name()}: ${child.type().buildable()}
% end

/// The leading trivia attached to this syntax node once built.
/// This is typically used to add comments (e.g. for documentation).
let leadingTrivia: Trivia

/// Creates a `${type.buildable()}` using the provided parameters.
/// - Parameters:
% for child in children:
/// - ${child.name()}: ${child.documentation()}
% end
public init(
leadingTrivia: Trivia = [],
${',\n '.join(['%s: %s%s' % (
child.name(),
child.type().expressible_as(),
child.type().default_initialization()
) for child in children])}
) {
self.leadingTrivia = leadingTrivia
% for child in children:
% assert_stmt = child.generate_assert_stmt_text_choices(child.name())
self.${child.name()} = ${child.type().generate_expr_convert_param_type_to_storage_type(child.name())}
Expand Down Expand Up @@ -95,28 +101,31 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
/// - Initializing syntax collections using result builders
/// - Initializing tokens without default text using strings
public init(
leadingTrivia: Trivia = [],
${',\n '.join(convenience_init_normal_parameters + convenience_init_result_builder_parameters)}
) {
self.init(
leadingTrivia: leadingTrivia,
${',\n '.join(delegated_init_args)}
)
}
% end

func build${type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${type.syntax()} {
/// Builds a `${type.syntax()}`.
/// - Parameter format: The `Format` to use.
/// - Parameter leadingTrivia: Additional leading trivia to attach, typically used for indentation.
/// - Returns: The built `${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.

let result = SyntaxFactory.make${type.base_name()}(
${',\n '.join(['%s: %s' % (child.name(), child.generate_expr_build_syntax_node(child.name(), 'format')) for child in children])}
)
if let leadingTrivia = leadingTrivia {
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
} else {
return result
}
let combinedLeadingTrivia = leadingTrivia + (additionalLeadingTrivia ?? []) + (result.leadingTrivia ?? [])
return result.withLeadingTrivia(combinedLeadingTrivia)
}

/// Conformance to `${base_type.buildable()}`.
public func build${base_type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${base_type.syntax()} {
let result = build${type.base_name()}(format: format, leadingTrivia: leadingTrivia)
public func build${base_type.base_name()}(format: Format, leadingTrivia additionalLeadingTrivia: Trivia? = nil) -> ${base_type.syntax()} {
let result = build${type.base_name()}(format: format, leadingTrivia: additionalLeadingTrivia)
return ${base_type.syntax()}(result)
}

Expand Down
Loading