Skip to content

Commit 60ebde3

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 61f67ae commit 60ebde3

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
@@ -477,8 +477,8 @@ extension NavigatorIndex {
477477
A `Builder` is a utility class to build a navigator index.
478478

479479
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.
480-
481-
- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.
480+
481+
- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.
482482
*/
483483
open class Builder {
484484

@@ -617,12 +617,13 @@ extension NavigatorIndex {
617617

618618
/// Index a single render `RenderNode`.
619619
/// - Parameter renderNode: The render node to be indexed.
620-
public func index(renderNode: RenderNode) throws {
620+
/// - Parameter ignoringLanguage: Whether language variants should be ignored when indexing this render node.
621+
public func index(renderNode: RenderNode, ignoringLanguage: Bool = false) throws {
621622
// Always index the main render node representation
622623
let language = try index(renderNode, traits: nil)
623624

624625
// Additionally, for Swift want to also index the Objective-C variant, if there is any.
625-
guard language == .swift else {
626+
guard !ignoringLanguage && language == .swift else {
626627
return
627628
}
628629

Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ Root
709709
}
710710

711711
func testNavigatorIndexGenerationVariantsPayload() throws {
712+
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: false)
713+
}
714+
715+
func testNavigatorIndexGenerationVariantsPayloadIgnoringLanguage() throws {
716+
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: true)
717+
}
718+
719+
private func testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: Bool) throws {
712720
let jsonFile = Bundle.module.url(forResource: "Variant-render-node", withExtension: "json", subdirectory: "Test Resources")!
713721
let jsonData = try Data(contentsOf: jsonFile)
714722

@@ -717,88 +725,112 @@ Root
717725
builder.setup()
718726

719727
let renderNode = try XCTUnwrap(RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: jsonData))
720-
try builder.index(renderNode: renderNode)
728+
try builder.index(renderNode: renderNode, ignoringLanguage: ignoringLanguage)
721729

722730
builder.finalize()
723731

724732
let navigatorIndex = builder.navigatorIndex!
725733

726734
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
727-
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), """
735+
var expectedDump = """
728736
[Root]
737+
738+
"""
739+
740+
if !ignoringLanguage {
741+
expectedDump += """
729742
┣╸Objective-C
730743
┃ ┗╸My Article in Objective-C
731744
┃ ┣╸Task Group 1
732745
┃ ┣╸Task Group 2
733746
┃ ┗╸Task Group 3
747+
748+
"""
749+
}
750+
751+
expectedDump += """
734752
┗╸Swift
735753
┗╸My Article
736754
┣╸Task Group 1
737755
┣╸Task Group 2
738756
┗╸Task Group 3
739-
""")
740-
741-
try XCTAssertEqual(
742-
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
743-
RenderIndex.fromString(#"""
744-
{
745-
"interfaceLanguages": {
746-
"occ": [
757+
"""
758+
759+
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), expectedDump)
760+
761+
var expectedRenderIndexString = """
762+
{
763+
"interfaceLanguages": {
764+
"""
765+
766+
if !ignoringLanguage {
767+
expectedRenderIndexString += #"""
768+
"occ": [
769+
{
770+
"children": [
771+
{
772+
"title": "Task Group 1",
773+
"type": "groupMarker"
774+
},
775+
{
776+
"title": "Task Group 2",
777+
"type": "groupMarker"
778+
},
747779
{
748-
"children": [
749-
{
750-
"title": "Task Group 1",
751-
"type": "groupMarker"
752-
},
753-
{
754-
"title": "Task Group 2",
755-
"type": "groupMarker"
756-
},
757-
{
758-
"title": "Task Group 3",
759-
"type": "groupMarker"
760-
}
761-
],
762-
"path": "\/documentation\/mykit\/my-article",
763-
"title": "My Article in Objective-C",
764-
"type": "article"
780+
"title": "Task Group 3",
781+
"type": "groupMarker"
765782
}
766783
],
767-
"swift": [
784+
"path": "\/documentation\/mykit\/my-article",
785+
"title": "My Article in Objective-C",
786+
"type": "article"
787+
}
788+
],
789+
"""#
790+
}
791+
792+
expectedRenderIndexString += #"""
793+
"swift": [
794+
{
795+
"children": [
768796
{
769-
"children": [
770-
{
771-
"title": "Task Group 1",
772-
"type": "groupMarker"
773-
},
774-
{
775-
"title": "Task Group 2",
776-
"type": "groupMarker"
777-
},
778-
{
779-
"title": "Task Group 3",
780-
"type": "groupMarker"
781-
}
782-
],
783-
"path": "\/documentation\/mykit\/my-article",
784-
"title": "My Article",
785-
"type": "article"
797+
"title": "Task Group 1",
798+
"type": "groupMarker"
799+
},
800+
{
801+
"title": "Task Group 2",
802+
"type": "groupMarker"
803+
},
804+
{
805+
"title": "Task Group 3",
806+
"type": "groupMarker"
786807
}
787-
]
788-
},
789-
"includedArchiveIdentifiers": [
790-
"org.swift.docc.example"
791-
],
792-
"schemaVersion": {
793-
"major": 0,
794-
"minor": 1,
795-
"patch": 2
808+
],
809+
"path": "\/documentation\/mykit\/my-article",
810+
"title": "My Article",
811+
"type": "article"
796812
}
797-
}
813+
]
798814
"""#
799-
)
815+
816+
expectedRenderIndexString += #"""
817+
},
818+
"includedArchiveIdentifiers": [
819+
"org.swift.docc.example"
820+
],
821+
"schemaVersion": {
822+
"major": 0,
823+
"minor": 1,
824+
"patch": 2
825+
}
826+
}
827+
"""#
828+
829+
try XCTAssertEqual(
830+
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
831+
RenderIndex.fromString(expectedRenderIndexString)
800832
)
801-
833+
802834
try FileManager.default.removeItem(at: targetURL)
803835
}
804836

0 commit comments

Comments
 (0)