Skip to content

Add a doc comment section mentioning which nodes a node occurs as a child in #1928

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 1 commit into from
Jul 25, 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
63 changes: 63 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ public class Node {
self.data = .layout(children: childrenWithUnexpected, traits: traits)
}

/// A doc comment that lists all the nodes in which this node occurs as a child in.
public var containedIn: SwiftSyntax.Trivia {
var childIn: [(node: SyntaxNodeKind, child: Child?)] = []
for node in SYNTAX_NODES {
if let layout = node.layoutNode {
for child in layout.children {
if child.kinds.contains(self.kind) {
childIn.append((node.kind, child))
}
}
} else if let collection = node.collectionNode {
if collection.elementChoices.contains(self.kind) {
childIn.append((node.kind, nil))
}
}
}

guard !childIn.isEmpty else {
return []
}

let list =
childIn
.map {
if let childName = $0.child?.varOrCaseName {
// This will repeat the syntax type before and after the dot, which is
// a little unfortunate, but it's the only way I found to get docc to
// generate a fully-qualified type + member.
return " - ``\($0.node.syntaxType)``.``\($0.node.syntaxType)/\(childName)``"
} else {
return " - ``\($0.node.syntaxType)``"
}
}
.joined(separator: "\n")

return docCommentTrivia(
from: """
### Contained in

\(list)
"""
)
}

/// Construct the specification for a collection syntax node.
///
/// `base` must be `.syntaxCollection`.
Expand Down Expand Up @@ -288,3 +332,22 @@ public struct CollectionNode {
)
}
}

fileprivate extension Child {
var kinds: [SyntaxNodeKind] {
switch kind {
case .node(let kind):
return [kind]
case .nodeChoices(let choices):
return choices.flatMap(\.kinds)
case .collection(let kind, _, _):
return [kind]
case .token:
return [.token]
}
}
}

fileprivate extension Node {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ import Utils

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

let documentation =
documentationSections
.filter { !$0.isEmpty }
.map { [$0] }
.joined(separator: [Trivia.newline, Trivia.docLineComment("///"), Trivia.newline])
.reduce(Trivia(), +)

try! StructDeclSyntax(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ import Utils
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
let documentationSections = [
node.documentation,
node.grammar,
node.containedIn,
]
let documentation =
documentationSections
.filter { !$0.isEmpty }
.map { [$0] }
.joined(separator: [Trivia.newline, Trivia.docLineComment("///"), Trivia.newline])
.reduce(Trivia(), +)

// We are actually handling this node now
try! StructDeclSyntax(
Expand Down
Loading