Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 467cf38

Browse files
Merge pull request #221 from Lukas-Stuehrk/DefaultImplementations
Also document default implementations of protocols.
2 parents d4b268d + 879e4e6 commit 467cf38

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
#199 by @MaxDesiatov and @mattt.
1414
- Added `--minimum-access-level` option to `generate` and `coverage` commands.
1515
#219 by @Lukas-Stuehrk.
16+
- Added support for documenting default implementations.
17+
#221 by @Lukas-Stuehrk.
1618

1719
### Fixed
1820

Sources/SwiftDoc/Interface.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,8 @@ public final class Interface {
155155
public func conditionalCounterparts(of symbol: Symbol) -> [Symbol] {
156156
return symbolsGroupedByIdentifier[symbol.id]?.filter { $0 != symbol }.sorted() ?? []
157157
}
158+
159+
public func defaultImplementations(of symbol: Symbol) -> [Symbol] {
160+
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .defaultImplementationOf }.map { $0.subject }.sorted() ?? []
161+
}
158162
}

Sources/swift-doc/Supporting Types/Components/Members.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct Members: Component {
1717
var properties: [Symbol]
1818
var methods: [Symbol]
1919
var genericallyConstrainedMembers: [[GenericRequirement] : [Symbol]]
20+
let defaultImplementations: [Symbol]
2021

2122
init(of symbol: Symbol, in module: Module, baseURL: String, symbolFilter: (Symbol) -> Bool) {
2223
self.symbol = symbol
@@ -33,6 +34,7 @@ struct Members: Component {
3334
self.properties = members.filter { $0.api is Variable }
3435
self.methods = members.filter { $0.api is Function }
3536
self.genericallyConstrainedMembers = Dictionary(grouping: members) { $0.`extension`?.genericRequirements ?? [] }.filter { !$0.key.isEmpty }
37+
self.defaultImplementations = module.interface.defaultImplementations(of: symbol).filter(symbolFilter)
3638
}
3739

3840
var sections: [(title: String, members: [Symbol])] {
@@ -41,14 +43,15 @@ struct Members: Component {
4143
("Initializers", initializers),
4244
("Enumeration Cases", cases),
4345
("Properties", properties),
44-
("Methods", methods)
46+
("Methods", methods),
47+
("Default Implementations", defaultImplementations),
4548
].filter { !$0.members.isEmpty }
4649
}
4750

4851
// MARK: - Component
4952

5053
var fragment: Fragment {
51-
guard !members.isEmpty else { return Fragment { "" } }
54+
guard !members.isEmpty || !defaultImplementations.isEmpty else { return Fragment { "" } }
5255

5356
return Fragment {
5457
ForEach(in: sections) { section -> BlockConvertible in

Tests/SwiftDocTests/InterfaceTypeTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,30 @@ final class InterfaceTypeTests: XCTestCase {
240240
XCTAssertEqual(module.interface.symbols[6].name, "InternalProperties")
241241
XCTAssertEqual(module.interface.symbols[7].name, "internal_prop")
242242
}
243+
244+
func testDefaultImplementationsOfProtocols() throws {
245+
let source = #"""
246+
public protocol SomeProtocol {
247+
func someMethod()
248+
}
249+
250+
public extension SomeProtocol {
251+
func someMethod() { }
252+
253+
func someOtherMethod() { }
254+
}
255+
"""#
256+
257+
258+
let url = try temporaryFile(contents: source)
259+
let sourceFile = try SourceFile(file: url, relativeTo: url.deletingLastPathComponent())
260+
let module = Module(name: "Module", sourceFiles: [sourceFile])
261+
262+
let protocolSymbol = module.interface.symbols[0]
263+
XCTAssertEqual(protocolSymbol.name, "SomeProtocol")
264+
let defaultImplementations = module.interface.defaultImplementations(of: protocolSymbol)
265+
XCTAssertEqual(defaultImplementations.count, 2)
266+
XCTAssertEqual(defaultImplementations[0].name, "someMethod()")
267+
XCTAssertEqual(defaultImplementations[1].name, "someOtherMethod()")
268+
}
243269
}

0 commit comments

Comments
 (0)