Skip to content

Add Children section to SyntaxCollection nodes #1902

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 3 commits into from
Jul 17, 2023
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
17 changes: 17 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,21 @@ public struct CollectionNode {
return choices
}
}

public var grammar: SwiftSyntax.Trivia {
let grammar: String
if let onlyElement = elementChoices.only {
grammar = "``\(onlyElement.syntaxType)`` `*`"
} else {
grammar = "(\(elementChoices.map { "``\($0.syntaxType)``" }.joined(separator: " | "))) `*`"
}

return docCommentTrivia(
from: """
### Children

\(grammar)
"""
)
}
}
11 changes: 11 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ public func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
}
return SwiftSyntax.Trivia(pieces: pieces)
}

public extension Collection {
/// If the collection contains a single element, return it, otherwise `nil`.
var only: Element? {
if !isEmpty && index(after: startIndex) == endIndex {
return self.first!
} else {
return nil
}
}
}
48 changes: 15 additions & 33 deletions CodeGeneration/Sources/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,21 @@
//
//===----------------------------------------------------------------------===//

/// Trims leading and trailing whitespace from each line.
public func dedented<Lines: Sequence>(lines: Lines) -> [String] where Lines.Element: StringProtocol {
lines.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
}

/// Trims leading and trailing whitespace from each line.
public func dedented(string: String) -> String {
dedented(lines: string.split(separator: "\n"))
.joined(separator: "\n")
}

/// Creates a single-line documentation string from indented
/// documentation as written in `CodeGeneration`.
public func flattened(indentedDocumentation: String) -> String {
dedented(string: indentedDocumentation)
.replacingOccurrences(of: "\n", with: "")
.trimmingCharacters(in: .whitespacesAndNewlines)
}

/// Removes all empty lines from a multi-line string.
public func removedEmptyLines(string: String) -> String {
string.split(whereSeparator: \.isNewline)
.filter { !$0.allSatisfy(\.isWhitespace) }
.joined(separator: "\n")
}
extension String {
/// Creates a single-line documentation string from indented
/// documentation as written in `CodeGeneration`.
public var flattened: String {
self
.replacingOccurrences(of: "\n", with: "")
.trimmingCharacters(in: .whitespacesAndNewlines)
}

public extension Collection {
/// If the collection contains a single element, return it, otherwise `nil`.
var only: Element? {
if !isEmpty && index(after: startIndex) == endIndex {
return self.first!
} else {
return nil
}
/// Removes all empty lines from a multi-line string.
public var removingEmptyLines: String {
return
self
.split(whereSeparator: \.isNewline)
.filter { !$0.allSatisfy(\.isWhitespace) }
.joined(separator: "\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ extension LayoutNode {
return " - \(child.varName): \(firstLine)"
}

let formattedParams = removedEmptyLines(
string: """
- Parameters:
- leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. \
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
\(children.compactMap(generateParamDocComment).joined(separator: "\n"))
- trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. \
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
"""
)
let formattedParams = """
- Parameters:
- leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. \
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
\(children.compactMap(generateParamDocComment).joined(separator: "\n"))
- trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. \
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
""".removingEmptyLines

return docCommentTrivia(from: formattedParams)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ import Utils

let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
for node in SYNTAX_NODES.compactMap(\.collectionNode) {
let documentation = """
\(node.documentation)
\(node.documentation.isEmpty ? "" : "///")
\(node.grammar)
""".removingEmptyLines

try! StructDeclSyntax(
"""
\(raw: node.documentation)
public struct \(raw: node.kind.syntaxType): SyntaxCollection, SyntaxHashable
\(raw: documentation)
public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable
"""
) {
if let onlyElement = node.elementChoices.only {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ extension Child {
guard let description = documentation else {
return []
}
let dedented = dedented(string: description)
let lines = dedented.split(separator: "\n", omittingEmptySubsequences: false)
let lines = description.split(separator: "\n", omittingEmptySubsequences: false)
let pieces = lines.map { SwiftSyntax.TriviaPiece.docLineComment("/// \($0)") }
return Trivia(pieces: pieces)
}
Expand All @@ -34,14 +33,18 @@ extension Child {
func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
SourceFileSyntax(leadingTrivia: copyrightHeader) {
for node in SYNTAX_NODES.compactMap(\.layoutNode) where node.base == emitKind {
let documentation = """
\(node.documentation)
\(node.documentation.isEmpty ? "" : "///")
\(node.grammar)
""".removingEmptyLines

// We are actually handling this node now
try! StructDeclSyntax(
"""
// MARK: - \(raw: node.kind.syntaxType)

\(raw: node.documentation)
\(raw: node.documentation.isEmpty ? "" : "///")
\(raw: node.grammar)
\(raw: documentation)
public struct \(raw: node.kind.syntaxType): \(raw: node.baseType.syntaxBaseName)Protocol, SyntaxHashable
"""
) {
Expand Down
Loading