Skip to content
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

Generate doc comments for property setter funcs and non-throwing property extensions #369

Merged
merged 1 commit into from
Oct 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public struct SwiftProtocolBodyWriter: SwiftSyntaxWriter {
}

public func writeFunc(
groupAsProperty: Bool = false,
documentation: SwiftDocumentationComment? = nil,
isPropertySetter: Bool = false,
attributes: [SwiftAttribute] = [],
static: Bool = false,
mutating: Bool = false,
Expand All @@ -82,7 +82,7 @@ public struct SwiftProtocolBodyWriter: SwiftSyntaxWriter {
returnType: SwiftType? = nil) {

if let documentation { writeDocumentationComment(documentation) }
output.beginLine(group: .named(isPropertySetter ? "protocolProperty" : "protocolFunc"))
output.beginLine(group: .named(groupAsProperty ? "protocolProperty" : "protocolFunc"))
writeFuncHeader(
attributes: attributes,
visibility: .implicit,
Expand Down
39 changes: 31 additions & 8 deletions Generator/Sources/ProjectionModel/Projection+documentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ extension Projection {
}

public func getDocumentationComment(_ member: Member, classFactoryKind: ClassFactoryKind? = nil, classDefinition: ClassDefinition? = nil) throws -> SwiftDocumentationComment? {
// Prefer the documentation comment from the class member over the abi member.
if let classDefinition,
member.definingType != classDefinition,
let classMember = try Self.findClassMember(classDefinition: classDefinition, abiMember: member, classFactoryKind: classFactoryKind),
let documentationComment = try getDocumentationComment(classMember) {
return documentationComment
try getMemberDocumentation(member, classFactoryKind: classFactoryKind, classDefinition: classDefinition)
.map(toDocumentationComment)
}

public enum PropertyAccessor {
case getter
case setter
}

public func getDocumentationComment(_ property: Property, accessor: PropertyAccessor, classDefinition: ClassDefinition? = nil) throws -> SwiftDocumentationComment? {
guard var propertyDocumentation = try getMemberDocumentation(property, classDefinition: classDefinition) else { return nil }

switch accessor {
case .getter:
propertyDocumentation.params = []
case .setter:
propertyDocumentation.returns = nil
}

guard let assemblyDocumentation = assembliesToModules[member.assembly]?.documentation else { return nil }
return try? assemblyDocumentation.lookup(member: member).map(toDocumentationComment)
return toDocumentationComment(propertyDocumentation)
}

public func getDocumentationComment(_ genericParam: GenericParam, typeDefinition: TypeDefinition) -> SwiftDocumentationComment? {
Expand All @@ -34,6 +44,19 @@ extension Projection {
}
}

private func getMemberDocumentation(_ member: Member, classFactoryKind: ClassFactoryKind? = nil, classDefinition: ClassDefinition? = nil) throws -> MemberDocumentation? {
// Prefer the documentation comment from the class member over the abi member.
if let classDefinition,
member.definingType != classDefinition,
let classMember = try Self.findClassMember(classDefinition: classDefinition, abiMember: member, classFactoryKind: classFactoryKind),
let documentation = try getMemberDocumentation(classMember) {
return documentation
}

guard let assemblyDocumentation = assembliesToModules[member.assembly]?.documentation else { return nil }
return try assemblyDocumentation.lookup(member: member)
}

private static func findClassMember(classDefinition: ClassDefinition, abiMember: Member, classFactoryKind: ClassFactoryKind? = nil) throws -> Member? {
switch abiMember {
case let method as Method:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ internal func writeExtensionProperties(
for property in interface.properties {
try writeNonthrowingPropertyImplementation(
property: property, static: `static`,
classDefinition: typeDefinition as? ClassDefinition,
projection: projection, to: writer)
}
}
}
}

internal func writeNonthrowingPropertyImplementation(
property: Property, static: Bool,
property: Property, static: Bool, classDefinition: ClassDefinition?,
projection: Projection, to writer: SwiftTypeDefinitionWriter) throws {
guard let getter = try property.getter else { return }

Expand All @@ -51,7 +52,7 @@ internal func writeNonthrowingPropertyImplementation(
}

try writer.writeComputedProperty(
documentation: projection.getDocumentationComment(property),
documentation: projection.getDocumentationComment(property, classDefinition: classDefinition),
visibility: .public,
static: `static`,
name: Projection.toMemberName(property),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje
for property in interfaceDefinition.properties {
if let getter = try property.getter {
try writer.writeFunc(
documentation: projection.getDocumentationComment(property),
documentation: projection.getDocumentationComment(property, accessor: .getter),
name: Projection.toMemberName(getter),
throws: true,
returnType: projection.toReturnType(property.type))
}

if let setter = try property.setter {
try writer.writeFunc(
isPropertySetter: true,
groupAsProperty: true,
documentation: projection.getDocumentationComment(property, accessor: .setter),
name: Projection.toMemberName(setter),
params: setter.params.map { try projection.toParameter($0) },
throws: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ fileprivate func writeInterfacePropertyImplementation(
// from an interface that would an extension property.
// public static var myProperty: MyPropertyType { ... }
try writeNonthrowingPropertyImplementation(
property: property, static: `static`, projection: projection, to: writer)
property: property, static: `static`, classDefinition: classDefinition,
projection: projection, to: writer)
}

// public [static] func _myProperty() throws -> MyPropertyType { ... }
if let getter = try property.getter, try getter.hasReturnValue {
let returnParamBinding = try projection.getParamBinding(getter.returnParam, genericTypeArgs: typeGenericArgs)
try writer.writeFunc(
documentation: documentation ? projection.getDocumentationComment(property, classDefinition: classDefinition) : nil,
documentation: documentation ? projection.getDocumentationComment(property, accessor: .getter, classDefinition: classDefinition) : nil,
visibility: overridable ? .open : .public,
static: `static`,
name: Projection.toMemberName(getter),
Expand All @@ -75,7 +76,7 @@ fileprivate func writeInterfacePropertyImplementation(
guard let newValueParam = try setter.params.first else { fatalError() }
let newValueParamBinding = try projection.getParamBinding(newValueParam, genericTypeArgs: typeGenericArgs)
try writer.writeFunc(
documentation: documentation ? projection.getDocumentationComment(property, classDefinition: classDefinition) : nil,
documentation: documentation ? projection.getDocumentationComment(property, accessor: .setter, classDefinition: classDefinition) : nil,
visibility: .public,
static: `static`,
name: Projection.toMemberName(setter),
Expand Down