This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build documentation for extensions on external types. (#230)
* Build documentation for external types. Implements #122. * Display extensions in definition list Remove unnecessary style rules for extensions * Fix false positives for external types. * Better check for external symbols. * Refactor isExternalSymbol to perform more general symbol resolution * Remove unnecessary parameter * Add resolution for nested types through typealiases Refactor implementation of ID * Use typealias resolution when creating relationships * Add tests for extensions on typealiases. * Add changelog entry for #230 Co-authored-by: Mattt <mattt@me.com>
- Loading branch information
1 parent
1b2baaf
commit b8a0cd9
Showing
9 changed files
with
318 additions
and
24 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
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 |
---|---|---|
@@ -1,16 +1,41 @@ | ||
public struct Identifier: Hashable { | ||
public let pathComponents: [String] | ||
public let context: [String] | ||
public let name: String | ||
public let pathComponents: [String] | ||
|
||
public init(context: [String], name: String) { | ||
self.context = context | ||
self.name = name | ||
self.pathComponents = context + CollectionOfOne(name) | ||
} | ||
|
||
public func matches(_ string: String) -> Bool { | ||
(pathComponents + CollectionOfOne(name)).reversed().starts(with: string.split(separator: ".").map { String($0) }.reversed()) | ||
return matches(string.split(separator: ".")) | ||
} | ||
|
||
public func matches(_ pathComponents: [Substring]) -> Bool { | ||
return matches(pathComponents.map(String.init)) | ||
} | ||
|
||
public func matches(_ pathComponents: [String]) -> Bool { | ||
return self.pathComponents.ends(with: pathComponents) | ||
} | ||
} | ||
|
||
// MARK: - CustomStringConvertible | ||
|
||
extension Identifier: CustomStringConvertible { | ||
public var description: String { | ||
(pathComponents + CollectionOfOne(name)).joined(separator: ".") | ||
pathComponents.joined(separator: ".") | ||
} | ||
} | ||
|
||
fileprivate extension Array { | ||
func ends<PossibleSuffix>(with possibleSuffix: PossibleSuffix) -> Bool | ||
where PossibleSuffix : Sequence, | ||
Self.Element == PossibleSuffix.Element, | ||
Self.Element: Equatable | ||
{ | ||
reversed().starts(with: possibleSuffix) | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
Sources/swift-doc/Supporting Types/Pages/ExternalTypePage.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,87 @@ | ||
import CommonMarkBuilder | ||
import SwiftDoc | ||
import HypertextLiteral | ||
import SwiftMarkup | ||
import SwiftSemantics | ||
|
||
struct ExternalTypePage: Page { | ||
|
||
let module: Module | ||
let externalType: String | ||
let baseURL: String | ||
|
||
let typealiases: [Symbol] | ||
let initializers: [Symbol] | ||
let properties: [Symbol] | ||
let methods: [Symbol] | ||
|
||
init(module: Module, externalType: String, symbols: [Symbol], baseURL: String) { | ||
self.module = module | ||
self.externalType = externalType | ||
self.baseURL = baseURL | ||
|
||
self.typealiases = symbols.filter { $0.api is Typealias } | ||
self.initializers = symbols.filter { $0.api is Initializer } | ||
self.properties = symbols.filter { $0.api is Variable } | ||
self.methods = symbols.filter { $0.api is Function } | ||
} | ||
|
||
var title: String { externalType } | ||
|
||
var sections: [(title: String, members: [Symbol])] { | ||
return [ | ||
("Nested Type Aliases", typealiases), | ||
("Initializers", initializers), | ||
("Properties", properties), | ||
("Methods", methods), | ||
].filter { !$0.members.isEmpty } | ||
} | ||
|
||
var document: CommonMark.Document { | ||
Document { | ||
Heading { "Extensions on \(externalType)" } | ||
ForEach(in: sections) { section -> BlockConvertible in | ||
Section { | ||
Heading { section.title } | ||
|
||
Section { | ||
ForEach(in: section.members) { member in | ||
Heading { | ||
Code { member.name } | ||
} | ||
Documentation(for: member, in: module, baseURL: baseURL) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
var html: HypertextLiteral.HTML { | ||
#""" | ||
<h1> | ||
<small>Extensions on</small> | ||
<code class="name">\#(externalType)</code> | ||
</h1> | ||
\#(sections.map { section -> HypertextLiteral.HTML in | ||
#""" | ||
<section id=\#(section.title.lowercased())> | ||
<h2>\#(section.title)</h2> | ||
\#(section.members.map { member -> HypertextLiteral.HTML in | ||
let descriptor = String(describing: type(of: member.api)).lowercased() | ||
|
||
return #""" | ||
<div role="article" class="\#(descriptor)" id=\#(member.id.description.lowercased().replacingOccurrences(of: " ", with: "-"))> | ||
<h3> | ||
<code>\#(softbreak(member.name))</code> | ||
</h3> | ||
\#(Documentation(for: member, in: module, baseURL: baseURL).html) | ||
</div> | ||
"""# | ||
}) | ||
</section> | ||
"""# | ||
}) | ||
"""# | ||
} | ||
} |
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
Oops, something went wrong.