Skip to content

Commit

Permalink
Make NavigatorIndex.Builder ignore language variants when requested
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniel-grumberg committed Oct 25, 2024
1 parent 61f67ae commit 60ebde3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 61 deletions.
9 changes: 5 additions & 4 deletions Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ extension NavigatorIndex {
A `Builder` is a utility class to build a navigator index.

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.
- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.

- Note: The builder is not thread safe and therefore, calling `index(renderNode:)` requires external synchronization in case the process is performed on different threads.
*/
open class Builder {

Expand Down Expand Up @@ -617,12 +617,13 @@ extension NavigatorIndex {

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

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

Expand Down
146 changes: 89 additions & 57 deletions Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ Root
}

func testNavigatorIndexGenerationVariantsPayload() throws {
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: false)
}

func testNavigatorIndexGenerationVariantsPayloadIgnoringLanguage() throws {
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: true)
}

private func testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: Bool) throws {
let jsonFile = Bundle.module.url(forResource: "Variant-render-node", withExtension: "json", subdirectory: "Test Resources")!
let jsonData = try Data(contentsOf: jsonFile)

Expand All @@ -717,88 +725,112 @@ Root
builder.setup()

let renderNode = try XCTUnwrap(RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: jsonData))
try builder.index(renderNode: renderNode)
try builder.index(renderNode: renderNode, ignoringLanguage: ignoringLanguage)

builder.finalize()

let navigatorIndex = builder.navigatorIndex!

assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), """
var expectedDump = """
[Root]
"""

if !ignoringLanguage {
expectedDump += """
┣╸Objective-C
┃ ┗╸My Article in Objective-C
┃ ┣╸Task Group 1
┃ ┣╸Task Group 2
┃ ┗╸Task Group 3
"""
}

expectedDump += """
┗╸Swift
┗╸My Article
┣╸Task Group 1
┣╸Task Group 2
┗╸Task Group 3
""")

try XCTAssertEqual(
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
RenderIndex.fromString(#"""
{
"interfaceLanguages": {
"occ": [
"""

assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), expectedDump)

var expectedRenderIndexString = """
{
"interfaceLanguages": {
"""

if !ignoringLanguage {
expectedRenderIndexString += #"""
"occ": [
{
"children": [
{
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"children": [
{
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"title": "Task Group 3",
"type": "groupMarker"
}
],
"path": "\/documentation\/mykit\/my-article",
"title": "My Article in Objective-C",
"type": "article"
"title": "Task Group 3",
"type": "groupMarker"
}
],
"swift": [
"path": "\/documentation\/mykit\/my-article",
"title": "My Article in Objective-C",
"type": "article"
}
],
"""#
}

expectedRenderIndexString += #"""
"swift": [
{
"children": [
{
"children": [
{
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"title": "Task Group 3",
"type": "groupMarker"
}
],
"path": "\/documentation\/mykit\/my-article",
"title": "My Article",
"type": "article"
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"title": "Task Group 3",
"type": "groupMarker"
}
]
},
"includedArchiveIdentifiers": [
"org.swift.docc.example"
],
"schemaVersion": {
"major": 0,
"minor": 1,
"patch": 2
],
"path": "\/documentation\/mykit\/my-article",
"title": "My Article",
"type": "article"
}
}
]
"""#
)

expectedRenderIndexString += #"""
},
"includedArchiveIdentifiers": [
"org.swift.docc.example"
],
"schemaVersion": {
"major": 0,
"minor": 1,
"patch": 2
}
}
"""#

try XCTAssertEqual(
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
RenderIndex.fromString(expectedRenderIndexString)
)

try FileManager.default.removeItem(at: targetURL)
}

Expand Down

0 comments on commit 60ebde3

Please sign in to comment.