Skip to content

Commit 145e5fb

Browse files
authored
Merge branch 'main' into ci-test-separation
2 parents 769c62c + 92f7fe1 commit 145e5fb

File tree

4 files changed

+248
-8
lines changed

4 files changed

+248
-8
lines changed

Sources/SwiftDocC/Model/Rendering/DocumentationContentRenderer.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,18 @@ public class DocumentationContentRenderer {
6767
from: symbol.subHeadingVariants,
6868
symbol.titleVariants,
6969
symbol.kindVariants
70-
) { _, subHeading, title, kind in
70+
) { trait, subHeading, title, kind in
7171
var fragments = subHeading
7272
.map({ fragment -> DeclarationRenderSection.Token in
7373
return DeclarationRenderSection.Token(fragment: fragment, identifier: nil)
7474
})
7575
if fragments.last?.text == "\n" { fragments.removeLast() }
7676

77-
// TODO: Return an Objective-C subheading for Objective-C symbols (rdar://84195588)
78-
return Swift.subHeading(for: fragments, symbolTitle: title, symbolKind: kind.identifier.identifier)
77+
if trait == .swift {
78+
return Swift.subHeading(for: fragments, symbolTitle: title, symbolKind: kind.identifier.identifier)
79+
} else {
80+
return fragments
81+
}
7982
} ?? .init(defaultValue: nil)
8083
}
8184

@@ -86,15 +89,19 @@ public class DocumentationContentRenderer {
8689
}
8790

8891
return VariantCollection<[DeclarationRenderSection.Token]?>(
89-
from: symbol.navigatorVariants
90-
) { _, navigator in
92+
from: symbol.navigatorVariants,
93+
symbol.titleVariants
94+
) { trait, navigator, title in
9195
var fragments = navigator.map { fragment -> DeclarationRenderSection.Token in
9296
return DeclarationRenderSection.Token(fragment: fragment, identifier: nil)
9397
}
9498
if fragments.last?.text == "\n" { fragments.removeLast() }
9599

96-
// TODO: Return an Objective-C navigator title for Objective-C symbols (rdar://84195588)
97-
return Swift.navigatorTitle(for: fragments, symbolTitle: symbol.title)
100+
if trait == .swift {
101+
return Swift.navigatorTitle(for: fragments, symbolTitle: title)
102+
} else {
103+
return fragments
104+
}
98105
} ?? .init(defaultValue: nil)
99106
}
100107

Sources/SwiftDocC/Model/Rendering/Variants/VariantCollection+Symbol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private func zipPairsByKey<Key, Value1, Value2>(
342342
)
343343
}
344344

345-
/// Creates a dictionary out of three sequences of pairs of the same key type
345+
/// Creates a dictionary out of three sequences of pairs of the same key type.
346346
///
347347
/// ```swift
348348
/// let words = [("a", "one"), ("b", "two")]
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
import SymbolKit
13+
import Markdown
14+
@testable import SwiftDocC
15+
16+
class DocumentationContentRendererTests: XCTestCase {
17+
func testReplacesTypeIdentifierSubHeadingFragmentWithIdentifierForSwift() throws {
18+
let subHeadingFragments = documentationContentRenderer
19+
.subHeadingFragments(for: nodeWithSubheadingAndNavigatorVariants)
20+
21+
XCTAssertEqual(
22+
subHeadingFragments.defaultValue,
23+
[
24+
DeclarationRenderSection.Token(
25+
text: "class",
26+
kind: .keyword,
27+
identifier: nil,
28+
preciseIdentifier: nil
29+
),
30+
DeclarationRenderSection.Token(
31+
text: " ",
32+
kind: .text,
33+
identifier: nil,
34+
preciseIdentifier: nil
35+
),
36+
DeclarationRenderSection.Token(
37+
text: "ClassInSwift",
38+
39+
// The 'typeIdentifier' value of the symbol's declaration is replaced with an 'identifier'.
40+
kind: .identifier,
41+
identifier: nil,
42+
preciseIdentifier: nil
43+
)
44+
]
45+
)
46+
}
47+
48+
func testDoesNotReplaceSubHeadingFragmentsForOtherLanguagesThanSwift() throws {
49+
let subHeadingFragments = documentationContentRenderer
50+
.subHeadingFragments(for: nodeWithSubheadingAndNavigatorVariants)
51+
52+
guard case .replace(let fragments) = subHeadingFragments.variants.first?.patch.first else {
53+
XCTFail("Unexpected patch")
54+
return
55+
}
56+
57+
XCTAssertEqual(
58+
fragments,
59+
[
60+
DeclarationRenderSection.Token(
61+
text: "class",
62+
kind: .keyword, identifier: nil, preciseIdentifier: nil
63+
),
64+
DeclarationRenderSection.Token(
65+
text: " ",
66+
kind: .text, identifier: nil, preciseIdentifier: nil
67+
),
68+
DeclarationRenderSection.Token(
69+
text: "ClassInAnotherLanguage",
70+
kind: .typeIdentifier, identifier: nil, preciseIdentifier: nil
71+
)
72+
]
73+
)
74+
}
75+
76+
func testReplacesTypeIdentifierNavigatorFragmentWithIdentifierForSwift() throws {
77+
let navigatorFragments = documentationContentRenderer
78+
.navigatorFragments(for: nodeWithSubheadingAndNavigatorVariants)
79+
80+
XCTAssertEqual(
81+
navigatorFragments.defaultValue,
82+
[
83+
DeclarationRenderSection.Token(
84+
text: "class",
85+
kind: .keyword,
86+
identifier: nil,
87+
preciseIdentifier: nil
88+
),
89+
DeclarationRenderSection.Token(
90+
text: " ",
91+
kind: .text,
92+
identifier: nil,
93+
preciseIdentifier: nil
94+
),
95+
DeclarationRenderSection.Token(
96+
text: "ClassInSwift",
97+
98+
// The 'typeIdentifier' value of the symbol's declaration is replaced with an 'identifier'.
99+
kind: .identifier,
100+
identifier: nil,
101+
preciseIdentifier: nil
102+
)
103+
]
104+
)
105+
}
106+
107+
func testDoesNotReplacesNavigatorFragmentsForOtherLanguagesThanSwift() throws {
108+
let navigatorFragments = documentationContentRenderer
109+
.navigatorFragments(for: nodeWithSubheadingAndNavigatorVariants)
110+
111+
guard case .replace(let fragments) = navigatorFragments.variants.first?.patch.first else {
112+
XCTFail("Unexpected patch")
113+
return
114+
}
115+
116+
XCTAssertEqual(
117+
fragments,
118+
[
119+
DeclarationRenderSection.Token(
120+
text: "class",
121+
kind: .keyword, identifier: nil, preciseIdentifier: nil
122+
),
123+
DeclarationRenderSection.Token(
124+
text: " ",
125+
kind: .text, identifier: nil, preciseIdentifier: nil
126+
),
127+
DeclarationRenderSection.Token(
128+
text: "ClassInAnotherLanguage",
129+
kind: .typeIdentifier, identifier: nil, preciseIdentifier: nil
130+
)
131+
]
132+
)
133+
}
134+
}
135+
136+
private extension DocumentationDataVariantsTrait {
137+
static var otherLanguage: DocumentationDataVariantsTrait { .init(interfaceLanguage: "otherLanguage") }
138+
}
139+
140+
private extension DocumentationContentRendererTests {
141+
var documentationContentRenderer: DocumentationContentRenderer {
142+
DocumentationContentRenderer(
143+
documentationContext: try! DocumentationContext(dataProvider: DocumentationWorkspace()),
144+
bundle: DocumentationBundle(
145+
info: DocumentationBundle.Info(
146+
displayName: "Test",
147+
identifier: "org.swift.test",
148+
version: Version(arrayLiteral: 1,2,3)
149+
),
150+
baseURL: URL(string: "https://example.com/example")!,
151+
symbolGraphURLs: [],
152+
markupURLs: [],
153+
miscResourceURLs: []
154+
)
155+
)
156+
}
157+
158+
var nodeWithSubheadingAndNavigatorVariants: DocumentationNode {
159+
var node = DocumentationNode(
160+
reference: ResolvedTopicReference(
161+
bundleIdentifier: "org.swift.example",
162+
path: "/documentation/class",
163+
fragment: nil,
164+
sourceLanguage: .swift
165+
),
166+
kind: .class,
167+
sourceLanguage: .swift,
168+
availableSourceLanguages: [
169+
.swift,
170+
.init(id: DocumentationDataVariantsTrait.otherLanguage.interfaceLanguage!)
171+
],
172+
name: DocumentationNode.Name.symbol(declaration: AttributedCodeListing.Line()),
173+
markup: Document(parsing: ""),
174+
semantic: nil,
175+
platformNames: nil
176+
)
177+
178+
node.semantic = Symbol(
179+
kindVariants: .init(values: [
180+
.swift: SymbolGraph.Symbol.Kind(parsedIdentifier: .class, displayName: "Class"),
181+
.otherLanguage: SymbolGraph.Symbol.Kind(parsedIdentifier: .class, displayName: "Class"),
182+
]),
183+
titleVariants: .init(values: [
184+
.swift: "ClassInSwift",
185+
.otherLanguage: "ClassInAnotherLanguage",
186+
]),
187+
subHeadingVariants: .init(values: [
188+
.swift: [
189+
.init(kind: .keyword, spelling: "class", preciseIdentifier: nil),
190+
.init(kind: .text, spelling: " ", preciseIdentifier: nil),
191+
.init(kind: .typeIdentifier, spelling: "ClassInSwift", preciseIdentifier: nil),
192+
],
193+
.otherLanguage: [
194+
.init(kind: .keyword, spelling: "class", preciseIdentifier: nil),
195+
.init(kind: .text, spelling: " ", preciseIdentifier: nil),
196+
.init(kind: .typeIdentifier, spelling: "ClassInAnotherLanguage", preciseIdentifier: nil),
197+
],
198+
]),
199+
navigatorVariants: .init(values: [
200+
.swift: [
201+
.init(kind: .keyword, spelling: "class", preciseIdentifier: nil),
202+
.init(kind: .text, spelling: " ", preciseIdentifier: nil),
203+
.init(kind: .typeIdentifier, spelling: "ClassInSwift", preciseIdentifier: nil),
204+
],
205+
.otherLanguage: [
206+
.init(kind: .keyword, spelling: "class", preciseIdentifier: nil),
207+
.init(kind: .text, spelling: " ", preciseIdentifier: nil),
208+
.init(kind: .typeIdentifier, spelling: "ClassInAnotherLanguage", preciseIdentifier: nil),
209+
],
210+
]),
211+
roleHeadingVariants: .init(swiftVariant: ""),
212+
platformNameVariants: .init(swiftVariant: nil),
213+
moduleNameVariants: .init(swiftVariant: ""),
214+
externalIDVariants: .init(swiftVariant: nil),
215+
accessLevelVariants: .init(swiftVariant: nil),
216+
availabilityVariants: .init(swiftVariant: Availability(availability: [])),
217+
deprecatedSummaryVariants: .init(swiftVariant: nil),
218+
mixinsVariants: .init(swiftVariant: nil),
219+
abstractSectionVariants: .init(swiftVariant: nil),
220+
discussionVariants: .init(swiftVariant: nil),
221+
topicsVariants: .init(swiftVariant: nil),
222+
seeAlsoVariants: .init(swiftVariant: nil),
223+
returnsSectionVariants: .init(swiftVariant: nil),
224+
parametersSectionVariants: .init(swiftVariant: nil),
225+
redirectsVariants: .init(swiftVariant: nil)
226+
)
227+
228+
return node
229+
}
230+
}

Tests/SwiftDocCTests/Rendering/RenderNodeTranslatorSymbolVariantsTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ class RenderNodeTranslatorSymbolVariantsTests: XCTestCase {
265265
symbol.navigatorVariants[.objectiveC] = [
266266
.init(kind: .keyword, spelling: "objc", preciseIdentifier: nil)
267267
]
268+
269+
symbol.titleVariants[.swift] = "Swift Title"
270+
symbol.titleVariants[.objectiveC] = "Objective-C Title"
268271
},
269272
assertOriginalRenderNode: { renderNode in
270273
XCTAssertEqual(

0 commit comments

Comments
 (0)