Skip to content

Commit 37ed32d

Browse files
Collect external links from various symbol section types (#855)
* Collect external links from _symbol_ deprecation summary content rdar://120374614 * ExternalReferenceWalker#visitSymbol visits all types of Symbol sections that might contain external links. Also, ReferenceResolver#visitSymbol visits HTTPBodyParameters. * ExternalReferenceWalker visits all variants of symbols. Refactor unit tests to use realistic symbol graph files for HTTP/REST API symbols. --------- Co-authored-by: David Rönnqvist <[email protected]>
1 parent 8f04a33 commit 37ed32d

File tree

4 files changed

+351
-14
lines changed

4 files changed

+351
-14
lines changed

Sources/SwiftDocC/Semantics/ExternalLinks/ExternalReferenceWalker.swift

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,58 @@ struct ExternalReferenceWalker: SemanticVisitor {
174174
}
175175

176176
mutating func visitComment(_ comment: Comment) { }
177-
177+
178+
mutating func visitSection(_ section: Section) {
179+
for markup in section.content { visitMarkup(markup) }
180+
}
181+
182+
mutating func visitSectionVariants(_ variants: DocumentationDataVariants<some Section>) {
183+
for variant in variants.allValues.map(\.variant) {
184+
visitSection(variant)
185+
}
186+
}
187+
178188
mutating func visitSymbol(_ symbol: Symbol) {
179-
symbol.abstractSection.unwrap { visitMarkup($0.paragraph) }
180-
symbol.discussion.unwrap { $0.content.forEach { visitMarkup($0) }}
181-
symbol.topics.unwrap { $0.content.forEach { visitMarkup($0) }}
182-
symbol.seeAlso.unwrap { $0.content.forEach { visitMarkup($0) }}
183-
symbol.returnsSection.unwrap { $0.content.forEach { visitMarkup($0) }}
184-
symbol.deprecatedSummary.unwrap { $0.content.forEach { visitMarkup($0) }}
185-
186-
symbol.parametersSection.unwrap {
187-
$0.parameters.forEach {
188-
$0.contents.forEach { visitMarkup($0) }
189+
190+
visitSectionVariants(symbol.abstractSectionVariants)
191+
visitSectionVariants(symbol.discussionVariants)
192+
visitSectionVariants(symbol.topicsVariants)
193+
visitSectionVariants(symbol.seeAlsoVariants)
194+
visitSectionVariants(symbol.returnsSectionVariants)
195+
visitSectionVariants(symbol.deprecatedSummaryVariants)
196+
197+
if let parametersSection = symbol.parametersSection {
198+
for parameter in parametersSection.parameters {
199+
for markup in parameter.contents { visitMarkup(markup) }
200+
}
201+
}
202+
203+
for dictionaryKeysSection in symbol.dictionaryKeysSectionVariants.allValues.map(\.variant) {
204+
for dictionaryKeys in dictionaryKeysSection.dictionaryKeys {
205+
for markup in dictionaryKeys.contents { visitMarkup(markup) }
206+
}
207+
}
208+
209+
for httpParametersSection in symbol.httpParametersSectionVariants.allValues.map(\.variant) {
210+
for param in httpParametersSection.parameters {
211+
for markup in param.contents { visitMarkup(markup) }
212+
}
213+
}
214+
215+
for httpResponsesSection in symbol.httpResponsesSectionVariants.allValues.map(\.variant) {
216+
for param in httpResponsesSection.responses {
217+
for markup in param.contents { visitMarkup(markup) }
218+
}
219+
}
220+
221+
for httpBodySection in symbol.httpBodySectionVariants.allValues.map(\.variant) {
222+
for markup in httpBodySection.body.contents { visitMarkup(markup) }
223+
for parameter in httpBodySection.body.parameters {
224+
for markup in parameter.contents { visitMarkup(markup) }
189225
}
190226
}
191227
}
192-
228+
193229
mutating func visitDeprecationSummary(_ summary: DeprecationSummary) {
194230
visit(summary.content)
195231
}

Sources/SwiftDocC/Semantics/ReferenceResolver.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,10 @@ struct ReferenceResolver: SemanticVisitor {
456456
}
457457
let newHTTPBodyVariants = symbol.httpBodySectionVariants.map { httpBodySection -> HTTPBodySection in
458458
let oldBody = httpBodySection.body
459-
let newBody = HTTPBody(mediaType: oldBody.mediaType, contents: oldBody.contents.map { visitMarkup($0) }, parameters: oldBody.parameters, symbol: oldBody.symbol)
459+
let newBodyParameters = oldBody.parameters.map {
460+
HTTPParameter(name: $0.name, source: $0.source, contents: $0.contents.map { visitMarkup($0) }, symbol: $0.symbol, required: $0.required)
461+
}
462+
let newBody = HTTPBody(mediaType: oldBody.mediaType, contents: oldBody.contents.map { visitMarkup($0) }, parameters: newBodyParameters, symbol: oldBody.symbol)
460463
return HTTPBodySection(body: newBody)
461464
}
462465
let newHTTPParametersVariants = symbol.httpParametersSectionVariants.map { httpParametersSection -> HTTPParametersSection in

Sources/SwiftDocC/Semantics/Symbol/Symbol.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ extension [String: Mixin] {
536536

537537
// MARK: Accessors for the first variant of symbol properties.
538538

539+
// Extend the Symbol class to account for legacy code that didn't account for symbols having multiple
540+
// language representations. New code should be written to work with the variants so that it supports
541+
// language specific content.
542+
539543
extension Symbol {
540544
/// The kind of the first variant of this symbol, such as protocol or variable.
541545
public var kind: SymbolGraph.Symbol.Kind { kindVariants.firstValue! }
@@ -686,10 +690,12 @@ extension Symbol {
686690
get { mixinsVariants.firstValue }
687691
set { mixinsVariants.firstValue = newValue }
688692
}
689-
693+
690694
/// Any automatically created task groups of the first variant of the symbol.
691695
var automaticTaskGroups: [AutomaticTaskGroupSection] {
692696
get { automaticTaskGroupsVariants.firstValue! }
693697
set { automaticTaskGroupsVariants.firstValue = newValue }
694698
}
699+
700+
// Don't add additional functions here. See the comment above about legacy code.
695701
}

0 commit comments

Comments
 (0)