-
Notifications
You must be signed in to change notification settings - Fork 440
List the token kinds in child documentation in SyntaxNodes #2168
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
Changes from all commits
e379aee
3f5dbd6
4fd2ade
11582df
8862f9a
b788135
b4fbbd9
4b76a66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,8 +87,25 @@ public class Child { | |
/// This is used to e.g. describe the child if all of its tokens are missing in the source file. | ||
public let nameForDiagnostics: String? | ||
|
||
/// A doc comment describing the child. | ||
public let documentation: SwiftSyntax.Trivia | ||
/// A summary of a doc comment describing the child. Full docc comment for the | ||
/// child is available in ``Child/documentation``, and includes detailed list | ||
/// of possible choices for the child if it's a token kind. | ||
public let documentationSummary: SwiftSyntax.Trivia | ||
|
||
/// A docc comment describing the child, including the trivia provided when | ||
/// initializing the ``Child``, and the list of possible token choices inferred automatically. | ||
public var documentation: SwiftSyntax.Trivia { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The part that I don't quite like about this is that we're essentially templating the doc comment for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have quite a bit of logic in the templates (I’m not even sure if templates really is the right term here but it’s what we have right now)… My take on this has always been that it’s not too bad because we control the entire pipeline (no other clients of the model and we even control the inputs) and thus we can get away with a slightly more sloppy separation of concerns. We could make this part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think perhaps this is a little bit bigger than one occurrence. The pattern is as follows:
That feels slightly unorthodox, but not too bad:
I'm uncertain if my opinion is valid — I might not have enough exposure and experience. But so far, my idea is something like:
@ahoppen want me to start a discussion / post on forums on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we aren’t super strict on the distinction between the pure data layer and the view layer in CodeGeneration and you probably notice this a lot more than I do because I haven’t been doing much view-based / UI development in a few years, where you think a lot more about that separation. But then also, the distinction is a little bit muddy IMO. Just taking the documentation as an example. I think we both agree that the documentation should be a property of a node/child but should that documentation already contain the generated Token section? I think you can argue in either direction. That being said, if you have ideas how to make that distinction clearer, I’m all in favor for them. One thing that’s important to me, though, is that we don’t introduce too many new abstraction layers that make the code less readable. Since CodeGeneration is still fairly reasonable in size and I don’t expect it to grow too much faster in the future, I would prefer an unorthodox decision here and there to an overengineered system of abstraction layers. |
||
if case .token(let choices, _, _) = kind { | ||
let tokenChoicesTrivia = SwiftSyntax.Trivia.docCommentTrivia( | ||
from: GrammarGenerator.childTokenChoices(for: choices) | ||
) | ||
|
||
return SwiftSyntax.Trivia(joining: [documentationSummary, tokenChoicesTrivia]) | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// If this child is not a token kind, return documentation summary without the choices list. | ||
return documentationSummary | ||
} | ||
|
||
/// The first line of the child's documentation | ||
public let documentationAbstract: String | ||
|
@@ -220,7 +237,7 @@ public class Child { | |
self.deprecatedName = deprecatedName | ||
self.kind = kind | ||
self.nameForDiagnostics = nameForDiagnostics | ||
self.documentation = docCommentTrivia(from: documentation) | ||
self.documentationSummary = SwiftSyntax.Trivia.docCommentTrivia(from: documentation) | ||
self.documentationAbstract = String(documentation?.split(whereSeparator: \.isNewline).first ?? "") | ||
self.isOptional = isOptional | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,24 +22,12 @@ import Utils | |
func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { | ||
SourceFileSyntax(leadingTrivia: copyrightHeader) { | ||
for node in SYNTAX_NODES.compactMap(\.layoutNode) where nodesStartingWith.contains(node.kind.syntaxType.description.first!) { | ||
let documentationSections = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this is clean, there's a way to make this even nicer — we can rename I'll push that change in a bit to this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. In particular, it would be nice to be consistent with how we stitch together the comment sections. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we agree on this, then perhaps I should include that cleanup in this PR. I'm hesitant to bring new abstractions, or even move things into |
||
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( | ||
""" | ||
// MARK: - \(node.kind.syntaxType) | ||
|
||
\(documentation) | ||
\(SwiftSyntax.Trivia(joining: [node.documentation, node.grammar, node.containedIn])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ahoppen I think you told me there's this code in three spots, but I only caught two: here, and in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s another one in SyntaxCollectionsFile.swift:21 |
||
\(node.node.apiAttributes())\ | ||
public struct \(node.kind.syntaxType): \(node.baseType.syntaxBaseName)Protocol, SyntaxHashable, \(node.base.leafProtocolType) | ||
""" | ||
|
Uh oh!
There was an error while loading. Please reload this page.