Skip to content

Commit 86b1ff8

Browse files
committed
Use the subheading fragments to init the external entity
`LinkDestinationSummary` now provides information for both the full and abbreviated declaration fragments. This commit adds the abbreviated declaration fragments to its counterpart type `OutOfProcessReferenceResolver.ResolvedInformation` [1], and uses them to initialise the external entity's `TopicRenderReference`. This is because the `TopicRenderReference` expects the abbreviated declaration fragments [2]. This results in the abbreviated declaration fragments correctly being used in the Topics and navigation index, rather than the full declaration fragments. Fixes rdar://156488052. [1]: https://github.com/swiftlang/swift-docc/blob/1b4a1850dd2785a8ebabded139ae0af3551bb029/Sources/SwiftDocC/Infrastructure/External%20Data/OutOfProcessReferenceResolver.swift#L559-L562 [2]: https://github.com/swiftlang/swift-docc/blob/1b4a1850dd2785a8ebabded139ae0af3551bb029/Sources/SwiftDocC/Model/Rendering/References/TopicRenderReference.swift#L50-L53
1 parent 832f1eb commit 86b1ff8

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

Sources/SwiftDocC/Infrastructure/External Data/OutOfProcessReferenceResolver.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ public class OutOfProcessReferenceResolver: ExternalDocumentationSource, GlobalE
166166
url: resolvedInformation.url.path,
167167
kind: kind,
168168
role: role,
169-
fragments: resolvedInformation.declarationFragments?.declarationFragments.map { DeclarationRenderSection.Token(fragment: $0, identifier: nil) },
169+
fragments: resolvedInformation.subHeadingDeclarationFragments?.declarationFragments
170+
.map { DeclarationRenderSection.Token(fragment: $0, identifier: nil) },
170171
isBeta: resolvedInformation.isBeta,
171172
isDeprecated: (resolvedInformation.platforms ?? []).contains(where: { $0.deprecated != nil }),
172173
images: resolvedInformation.topicImages ?? []
@@ -182,7 +183,7 @@ public class OutOfProcessReferenceResolver: ExternalDocumentationSource, GlobalE
182183
.init(traits: variant.traits, patch: [.replace(value: [.text(abstract)])])
183184
)
184185
}
185-
if let declarationFragments = variant.declarationFragments {
186+
if let declarationFragments = variant.subHeadingDeclarationFragments {
186187
renderReference.fragmentsVariants.variants.append(
187188
.init(traits: variant.traits, patch: [.replace(value: declarationFragments?.declarationFragments.map { DeclarationRenderSection.Token(fragment: $0, identifier: nil) })])
188189
)
@@ -577,7 +578,11 @@ extension OutOfProcessReferenceResolver {
577578
public let platforms: [PlatformAvailability]?
578579
/// Information about the resolved declaration fragments, if any.
579580
public let declarationFragments: DeclarationFragments?
580-
581+
/// Information about the resolved abbreviated declaration fragments, if any.
582+
///
583+
/// They are used for displaying in contexts where the full declaration fragments would be too verbose, like in the Topics section or the navigation index.
584+
public let subHeadingDeclarationFragments: DeclarationFragments?
585+
581586
// We use the real types here because they're Codable and don't have public member-wise initializers.
582587

583588
/// Platform availability for a resolved symbol reference.
@@ -620,6 +625,7 @@ extension OutOfProcessReferenceResolver {
620625
/// - availableLanguages: The languages where the resolved node is available.
621626
/// - platforms: The platforms and their versions where the resolved node is available, if any.
622627
/// - declarationFragments: The resolved declaration fragments, if any.
628+
/// - subHeadingDeclarationFragments: The abbreviated resolved declaration fragments, if any.
623629
/// - topicImages: Images that are used to represent the summarized element.
624630
/// - references: References used in the content of the summarized element.
625631
/// - variants: The variants of content for this resolver information.
@@ -632,6 +638,7 @@ extension OutOfProcessReferenceResolver {
632638
availableLanguages: Set<SourceLanguage>,
633639
platforms: [PlatformAvailability]? = nil,
634640
declarationFragments: DeclarationFragments? = nil,
641+
subHeadingDeclarationFragments: DeclarationFragments? = nil,
635642
topicImages: [TopicImage]? = nil,
636643
references: [any RenderReference]? = nil,
637644
variants: [Variant]? = nil
@@ -644,6 +651,7 @@ extension OutOfProcessReferenceResolver {
644651
self.availableLanguages = availableLanguages
645652
self.platforms = platforms
646653
self.declarationFragments = declarationFragments
654+
self.subHeadingDeclarationFragments = subHeadingDeclarationFragments
647655
self.topicImages = topicImages
648656
self.references = references
649657
self.variants = variants
@@ -675,7 +683,12 @@ extension OutOfProcessReferenceResolver {
675683
///
676684
/// If the resolver information has a declaration but the variant doesn't, this property will be `Optional.some(nil)`.
677685
public let declarationFragments: VariantValue<DeclarationFragments?>
678-
686+
/// The abbreviated declaration fragments of the variant or `nil` if the declaration is the same as the resolved information.
687+
///
688+
/// They are used for displaying in contexts where the full declaration fragments would be too verbose, like in the Topics section or the navigation index.
689+
/// If the resolver information has a declaration but the variant doesn't, this property will be `Optional.some(nil)`.
690+
public let subHeadingDeclarationFragments: VariantValue<DeclarationFragments?>
691+
679692
/// Creates a new resolved information variant with the values that are different from the resolved information values.
680693
///
681694
/// - Parameters:
@@ -686,14 +699,16 @@ extension OutOfProcessReferenceResolver {
686699
/// - abstract: The resolved (plain text) abstract.
687700
/// - language: The resolved language.
688701
/// - declarationFragments: The resolved declaration fragments, if any.
702+
/// - subHeadingDeclarationFragments: The resolved abbreviated declaration fragments, if any.
689703
public init(
690704
traits: [RenderNode.Variant.Trait],
691705
kind: VariantValue<DocumentationNode.Kind> = nil,
692706
url: VariantValue<URL> = nil,
693707
title: VariantValue<String> = nil,
694708
abstract: VariantValue<String> = nil,
695709
language: VariantValue<SourceLanguage> = nil,
696-
declarationFragments: VariantValue<DeclarationFragments?> = nil
710+
declarationFragments: VariantValue<DeclarationFragments?> = nil,
711+
subHeadingDeclarationFragments: VariantValue<DeclarationFragments?> = nil
697712
) {
698713
self.traits = traits
699714
self.kind = kind
@@ -702,6 +717,7 @@ extension OutOfProcessReferenceResolver {
702717
self.abstract = abstract
703718
self.language = language
704719
self.declarationFragments = declarationFragments
720+
self.subHeadingDeclarationFragments = subHeadingDeclarationFragments
705721
}
706722
}
707723
}
@@ -717,6 +733,7 @@ extension OutOfProcessReferenceResolver.ResolvedInformation {
717733
case availableLanguages
718734
case platforms
719735
case declarationFragments
736+
case subHeadingDeclarationFragments
720737
case topicImages
721738
case references
722739
case variants
@@ -733,6 +750,8 @@ extension OutOfProcessReferenceResolver.ResolvedInformation {
733750
availableLanguages = try container.decode(Set<SourceLanguage>.self, forKey: .availableLanguages)
734751
platforms = try container.decodeIfPresent([OutOfProcessReferenceResolver.ResolvedInformation.PlatformAvailability].self, forKey: .platforms)
735752
declarationFragments = try container.decodeIfPresent(OutOfProcessReferenceResolver.ResolvedInformation.DeclarationFragments.self, forKey: .declarationFragments)
753+
subHeadingDeclarationFragments = try container
754+
.decodeIfPresent(OutOfProcessReferenceResolver.ResolvedInformation.DeclarationFragments.self, forKey: .subHeadingDeclarationFragments)
736755
topicImages = try container.decodeIfPresent([TopicImage].self, forKey: .topicImages)
737756
references = try container.decodeIfPresent([CodableRenderReference].self, forKey: .references).map { decodedReferences in
738757
decodedReferences.map(\.reference)
@@ -752,6 +771,7 @@ extension OutOfProcessReferenceResolver.ResolvedInformation {
752771
try container.encode(self.availableLanguages, forKey: .availableLanguages)
753772
try container.encodeIfPresent(self.platforms, forKey: .platforms)
754773
try container.encodeIfPresent(self.declarationFragments, forKey: .declarationFragments)
774+
try container.encodeIfPresent(self.subHeadingDeclarationFragments, forKey: .subHeadingDeclarationFragments)
755775
try container.encodeIfPresent(self.topicImages, forKey: .topicImages)
756776
try container.encodeIfPresent(references?.map { CodableRenderReference($0) }, forKey: .references)
757777
try container.encodeIfPresent(self.variants, forKey: .variants)

Tests/SwiftDocCUtilitiesTests/OutOfProcessReferenceResolverTests.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
7070
declarationFragments: .init(declarationFragments: [
7171
.init(kind: .text, spelling: "declaration fragment", preciseIdentifier: nil)
7272
]),
73+
subHeadingDeclarationFragments: .init(declarationFragments: [
74+
.init(kind: .text, spelling: "subheading declaration fragment", preciseIdentifier: nil)
75+
]),
7376
topicImages: nil,
7477
references: nil,
7578
variants: [
@@ -82,7 +85,10 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
8285
language: .init(name: "Language Name 2", id: "com.test.another-language.id"),
8386
declarationFragments: .init(declarationFragments: [
8487
.init(kind: .text, spelling: "variant declaration fragment", preciseIdentifier: nil)
85-
])
88+
]),
89+
subHeadingDeclarationFragments: .init(declarationFragments: [
90+
.init(kind: .text, spelling: "variant subheading declaration fragment", preciseIdentifier: nil)
91+
]),
8692
)
8793
]
8894
)
@@ -120,7 +126,7 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
120126
XCTAssertEqual(availableSourceLanguages[1], expectedLanguages[1])
121127
XCTAssertEqual(availableSourceLanguages[2], expectedLanguages[2])
122128

123-
XCTAssertEqual(entity.topicRenderReference.fragments, [.init(text: "declaration fragment", kind: .text, preciseIdentifier: nil)])
129+
XCTAssertEqual(entity.topicRenderReference.fragments, [.init(text: "subheading declaration fragment", kind: .text, preciseIdentifier: nil)])
124130

125131
let variantTraits = [RenderNode.Variant.Trait.interfaceLanguage("com.test.another-language.id")]
126132
XCTAssertEqual(entity.topicRenderReference.titleVariants.value(for: variantTraits), "Resolved Variant Title")
@@ -129,7 +135,7 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
129135
let fragmentVariant = try XCTUnwrap(entity.topicRenderReference.fragmentsVariants.variants.first(where: { $0.traits == variantTraits }))
130136
XCTAssertEqual(fragmentVariant.patch.map(\.operation), [.replace])
131137
if case .replace(let variantFragment) = fragmentVariant.patch.first {
132-
XCTAssertEqual(variantFragment, [.init(text: "variant declaration fragment", kind: .text, preciseIdentifier: nil)])
138+
XCTAssertEqual(variantFragment, [.init(text: "variant subheading declaration fragment", kind: .text, preciseIdentifier: nil)])
133139
} else {
134140
XCTFail("Unexpected fragments variant patch")
135141
}
@@ -226,6 +232,9 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
226232
declarationFragments: .init(declarationFragments: [
227233
.init(kind: .text, spelling: "declaration fragment", preciseIdentifier: nil)
228234
]),
235+
subHeadingDeclarationFragments: .init(declarationFragments: [
236+
.init(kind: .text, spelling: "subheading declaration fragment", preciseIdentifier: nil)
237+
]),
229238
topicImages: [
230239
TopicImage(
231240
type: .card,
@@ -260,6 +269,9 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
260269
language: .init(name: "Language Name 2", id: "com.test.another-language.id"),
261270
declarationFragments: .init(declarationFragments: [
262271
.init(kind: .text, spelling: "variant declaration fragment", preciseIdentifier: nil)
272+
]),
273+
subHeadingDeclarationFragments: .init(declarationFragments: [
274+
.init(kind: .text, spelling: "variant subheading declaration fragment", preciseIdentifier: nil)
263275
])
264276
)
265277
]
@@ -288,7 +300,7 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
288300
XCTAssertEqual(availableSourceLanguages[1], expectedLanguages[1])
289301
XCTAssertEqual(availableSourceLanguages[2], expectedLanguages[2])
290302

291-
XCTAssertEqual(entity.topicRenderReference.fragments, [.init(text: "declaration fragment", kind: .text, preciseIdentifier: nil)])
303+
XCTAssertEqual(entity.topicRenderReference.fragments, [.init(text: "subheading declaration fragment", kind: .text, preciseIdentifier: nil)])
292304

293305
let variantTraits = [RenderNode.Variant.Trait.interfaceLanguage("com.test.another-language.id")]
294306
XCTAssertEqual(entity.topicRenderReference.titleVariants.value(for: variantTraits), "Resolved Variant Title")
@@ -297,7 +309,7 @@ class OutOfProcessReferenceResolverTests: XCTestCase {
297309
let fragmentVariant = try XCTUnwrap(entity.topicRenderReference.fragmentsVariants.variants.first(where: { $0.traits == variantTraits }))
298310
XCTAssertEqual(fragmentVariant.patch.map(\.operation), [.replace])
299311
if case .replace(let variantFragment) = fragmentVariant.patch.first {
300-
XCTAssertEqual(variantFragment, [.init(text: "variant declaration fragment", kind: .text, preciseIdentifier: nil)])
312+
XCTAssertEqual(variantFragment, [.init(text: "variant subheading declaration fragment", kind: .text, preciseIdentifier: nil)])
301313
} else {
302314
XCTFail("Unexpected fragments variant patch")
303315
}

0 commit comments

Comments
 (0)