-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added infrastructure for supporting writing documentation comments.
- Loading branch information
1 parent
b4f26d6
commit a2f322b
Showing
9 changed files
with
229 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public struct SwiftDocComment { | ||
public var summary: [Block]? | ||
public var parameters = [Param]() | ||
public var returns: [Span]? | ||
|
||
public init() {} | ||
|
||
public enum Block: Hashable { | ||
case paragraph([Span]) | ||
case code(String) | ||
case list([Span]) | ||
|
||
public static func paragraph(_ span: Span) -> Block { .paragraph([span]) } | ||
public static func paragraph(_ text: String) -> Block { .paragraph(.text(text)) } | ||
} | ||
|
||
public enum Span: Hashable { | ||
case text(String) | ||
case code(String) | ||
} | ||
|
||
public struct Param { | ||
public var name: String | ||
public var description: String | ||
|
||
public init(name: String, description: String) { | ||
self.name = name | ||
self.description = description | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Generator/Sources/CodeWriters/Swift/SyntaxWriters/DocComments.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
extension SwiftSyntaxWriter { | ||
internal func writeDocComment(_ docComment: SwiftDocComment) { | ||
output.writeLine("/**") | ||
if let summary = docComment.summary { | ||
for block in summary { writeDocCommentBlock(block) } | ||
} | ||
|
||
for param in docComment.parameters { | ||
output.write("- Parameter ") | ||
output.write(param.name) | ||
output.write(": ") | ||
output.write(param.description) | ||
output.endLine() | ||
} | ||
|
||
if let returns = docComment.returns { | ||
output.write("- Returns: ") | ||
for span in returns { writeDocCommentSpan(span) } | ||
output.endLine() | ||
} | ||
|
||
output.writeLine("*/") | ||
} | ||
|
||
fileprivate func writeDocCommentBlock(_ block: SwiftDocComment.Block) { | ||
switch block { | ||
case .paragraph(let spans): | ||
for span in spans { writeDocCommentSpan(span) } | ||
output.endLine() | ||
default: | ||
return // TODO: Support more block types | ||
} | ||
} | ||
|
||
fileprivate func writeDocCommentSpan(_ span: SwiftDocComment.Span) { | ||
switch span { | ||
case .text(let string): output.write(string) | ||
default: | ||
return // TODO: Support more inline types | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Generator/Sources/ProjectionGenerator/SwiftProjection+Module.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import DotNetMetadata | ||
import DotNetXMLDocs | ||
|
||
extension SwiftProjection { | ||
public class Module { | ||
public unowned let projection: SwiftProjection | ||
public let shortName: String | ||
public private(set) var typeDefinitionsByNamespace = [String: Set<TypeDefinition>]() | ||
public private(set) var closedGenericTypesByDefinition = [TypeDefinition: [[TypeNode]]]() | ||
private(set) var references: Set<Reference> = [] | ||
|
||
internal init(projection: SwiftProjection, shortName: String) { | ||
self.projection = projection | ||
self.shortName = shortName | ||
} | ||
|
||
public var assemblyModuleName: String { shortName + "Assembly" } | ||
|
||
public func addAssembly(_ assembly: Assembly, documentation: DocumentationFile? = nil) { | ||
projection.assembliesToModules[assembly] = AssemblyEntry(module: self, documentation: documentation) | ||
} | ||
|
||
public func hasTypeDefinition(_ type: TypeDefinition) -> Bool { | ||
typeDefinitionsByNamespace[Module.getNamespaceOrEmpty(type)]?.contains(type) ?? false | ||
} | ||
|
||
public func addTypeDefinition(_ type: TypeDefinition) { | ||
precondition(projection.getModule(type.assembly) === self) | ||
typeDefinitionsByNamespace[Module.getNamespaceOrEmpty(type), default: Set()].insert(type) | ||
} | ||
|
||
public func addClosedGenericType(_ type: BoundType) { | ||
precondition(!type.genericArgs.isEmpty && !type.isParameterized) | ||
closedGenericTypesByDefinition[type.definition, default: []].append(type.genericArgs) | ||
} | ||
|
||
public func addReference(_ other: Module) { | ||
references.insert(Reference(target: other)) | ||
} | ||
|
||
internal func getName(_ typeDefinition: TypeDefinition, namespaced: Bool = true) throws -> String { | ||
// Map: Namespace.TypeName | ||
// To: Namespace_TypeName | ||
// Map: Namespace.Subnamespace.TypeName/NestedTypeName | ||
// To: NamespaceSubnamespace_TypeName_NestedTypeName | ||
var result: String = "" | ||
if let enclosingType = try typeDefinition.enclosingType { | ||
result += try getName(enclosingType, namespaced: namespaced) + "_" | ||
} | ||
else if namespaced { | ||
result += typeDefinition.namespace.flatMap { SwiftProjection.toCompactNamespace($0) + "_" } ?? "" | ||
} | ||
|
||
result += typeDefinition.nameWithoutGenericSuffix | ||
|
||
return result | ||
} | ||
|
||
private static func getNamespaceOrEmpty(_ type: TypeDefinition) -> String { | ||
var namespacedType = type | ||
while namespacedType.namespace == nil, let enclosingType = try? namespacedType.enclosingType { | ||
namespacedType = enclosingType | ||
} | ||
return namespacedType.namespace ?? "" | ||
} | ||
|
||
struct Reference: Hashable { | ||
unowned var target: Module | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(ObjectIdentifier(target)) | ||
} | ||
|
||
static func == (lhs: Module.Reference, rhs: Module.Reference) -> Bool { | ||
lhs.target === rhs.target | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.