Skip to content

Commit 065a609

Browse files
Make NavigatorIndex.Builder ignore language variants when requested
Clients might want the Navigator Index to not preemptively add an entry for nodes that have a language variant trait. For example, a client might not have a renderer cable of applying language variants, in this instance it doesn't make sense to generate a navigator hierarchy for other languages. rdar://138183564
1 parent ed492be commit 065a609

File tree

2 files changed

+94
-61
lines changed

2 files changed

+94
-61
lines changed

Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ extension NavigatorIndex {
473473
A `Builder` is a utility class to build a navigator index.
474474

475475
The builder generates an index for content navigation, but also maps important information to filter content based on availability, symbol type, platform and some others.
476-
477-
- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.
476+
477+
- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.
478478
*/
479479
open class Builder {
480480

@@ -613,12 +613,13 @@ extension NavigatorIndex {
613613

614614
/// Index a single render `RenderNode`.
615615
/// - Parameter renderNode: The render node to be indexed.
616-
public func index(renderNode: RenderNode) throws {
616+
/// - Parameter ignoringLanguage: Whether language variants should be ignored when indexing this render node.
617+
public func index(renderNode: RenderNode, ignoringLanguage: Bool = false) throws {
617618
// Always index the main render node representation
618619
let language = try index(renderNode, traits: nil)
619620

620621
// Additionally, for Swift want to also index the Objective-C variant, if there is any.
621-
guard language == .swift else {
622+
guard !ignoringLanguage && language == .swift else {
622623
return
623624
}
624625

Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,14 @@ Root
703703
}
704704

705705
func testNavigatorIndexGenerationVariantsPayload() throws {
706+
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: false)
707+
}
708+
709+
func testNavigatorIndexGenerationVariantsPayloadIgnoringLanguage() throws {
710+
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: true)
711+
}
712+
713+
private func testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: Bool) throws {
706714
let jsonFile = Bundle.module.url(forResource: "Variant-render-node", withExtension: "json", subdirectory: "Test Resources")!
707715
let jsonData = try Data(contentsOf: jsonFile)
708716

@@ -711,88 +719,112 @@ Root
711719
builder.setup()
712720

713721
let renderNode = try XCTUnwrap(RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: jsonData))
714-
try builder.index(renderNode: renderNode)
722+
try builder.index(renderNode: renderNode, ignoringLanguage: ignoringLanguage)
715723

716724
builder.finalize()
717725

718726
let navigatorIndex = builder.navigatorIndex!
719727

720728
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
721-
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), """
729+
var expectedDump = """
722730
[Root]
731+
732+
"""
733+
734+
if !ignoringLanguage {
735+
expectedDump += """
723736
┣╸Objective-C
724737
┃ ┗╸My Article in Objective-C
725738
┃ ┣╸Task Group 1
726739
┃ ┣╸Task Group 2
727740
┃ ┗╸Task Group 3
741+
742+
"""
743+
}
744+
745+
expectedDump += """
728746
┗╸Swift
729747
┗╸My Article
730748
┣╸Task Group 1
731749
┣╸Task Group 2
732750
┗╸Task Group 3
733-
""")
734-
735-
try XCTAssertEqual(
736-
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
737-
RenderIndex.fromString(#"""
738-
{
739-
"interfaceLanguages": {
740-
"occ": [
751+
"""
752+
753+
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), expectedDump)
754+
755+
var expectedRenderIndexString = """
756+
{
757+
"interfaceLanguages": {
758+
"""
759+
760+
if !ignoringLanguage {
761+
expectedRenderIndexString += #"""
762+
"occ": [
763+
{
764+
"children": [
765+
{
766+
"title": "Task Group 1",
767+
"type": "groupMarker"
768+
},
769+
{
770+
"title": "Task Group 2",
771+
"type": "groupMarker"
772+
},
741773
{
742-
"children": [
743-
{
744-
"title": "Task Group 1",
745-
"type": "groupMarker"
746-
},
747-
{
748-
"title": "Task Group 2",
749-
"type": "groupMarker"
750-
},
751-
{
752-
"title": "Task Group 3",
753-
"type": "groupMarker"
754-
}
755-
],
756-
"path": "\/documentation\/mykit\/my-article",
757-
"title": "My Article in Objective-C",
758-
"type": "article"
774+
"title": "Task Group 3",
775+
"type": "groupMarker"
759776
}
760777
],
761-
"swift": [
778+
"path": "\/documentation\/mykit\/my-article",
779+
"title": "My Article in Objective-C",
780+
"type": "article"
781+
}
782+
],
783+
"""#
784+
}
785+
786+
expectedRenderIndexString += #"""
787+
"swift": [
788+
{
789+
"children": [
762790
{
763-
"children": [
764-
{
765-
"title": "Task Group 1",
766-
"type": "groupMarker"
767-
},
768-
{
769-
"title": "Task Group 2",
770-
"type": "groupMarker"
771-
},
772-
{
773-
"title": "Task Group 3",
774-
"type": "groupMarker"
775-
}
776-
],
777-
"path": "\/documentation\/mykit\/my-article",
778-
"title": "My Article",
779-
"type": "article"
791+
"title": "Task Group 1",
792+
"type": "groupMarker"
793+
},
794+
{
795+
"title": "Task Group 2",
796+
"type": "groupMarker"
797+
},
798+
{
799+
"title": "Task Group 3",
800+
"type": "groupMarker"
780801
}
781-
]
782-
},
783-
"includedArchiveIdentifiers": [
784-
"org.swift.docc.example"
785-
],
786-
"schemaVersion": {
787-
"major": 0,
788-
"minor": 1,
789-
"patch": 2
802+
],
803+
"path": "\/documentation\/mykit\/my-article",
804+
"title": "My Article",
805+
"type": "article"
790806
}
791-
}
807+
]
792808
"""#
793-
)
809+
810+
expectedRenderIndexString += #"""
811+
},
812+
"includedArchiveIdentifiers": [
813+
"org.swift.docc.example"
814+
],
815+
"schemaVersion": {
816+
"major": 0,
817+
"minor": 1,
818+
"patch": 2
819+
}
820+
}
821+
"""#
822+
823+
try XCTAssertEqual(
824+
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
825+
RenderIndex.fromString(expectedRenderIndexString)
794826
)
795-
827+
796828
try FileManager.default.removeItem(at: targetURL)
797829
}
798830

0 commit comments

Comments
 (0)