Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍒 Make NavigatorIndex.Builder ignore language variants when requested #1078

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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