Skip to content

Commit ac5ba29

Browse files
Display entitlement key in symbol reference (#1018)
References to property lists display the key name. When referencing a symbol in a topics section, if the symbol is a property that contains a key and a custom title, display the key name under the abstract. rdar://129540266
1 parent 9f1c9d8 commit ac5ba29

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

Sources/SwiftDocC/Model/Rendering/DocumentationContentRenderer.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,16 @@ public class DocumentationContentRenderer {
363363
let isRequired = (node?.semantic as? Symbol)?.isRequired ?? false
364364

365365
let estimatedTime = (node?.semantic as? Timed)?.durationMinutes.flatMap(formatEstimatedDuration(minutes:))
366-
366+
367+
// Add key information for property lists.
368+
let propertyListKeyNames = node?.symbol?.plistDetails.map {
369+
TopicRenderReference.PropertyListKeyNames(
370+
titleStyle: ($0.customTitle != nil) ? .useDisplayName : .useRawKey,
371+
rawKey: $0.rawKey,
372+
displayName: $0.customTitle
373+
)
374+
}
375+
367376
var renderReference = TopicRenderReference(
368377
identifier: .init(referenceURL),
369378
titleVariants: VariantCollection<String>(from: titleVariants) ?? .init(defaultValue: ""),
@@ -372,7 +381,8 @@ public class DocumentationContentRenderer {
372381
kind: kind,
373382
required: isRequired,
374383
role: referenceRole,
375-
estimatedTime: estimatedTime
384+
estimatedTime: estimatedTime,
385+
propertyListKeyNames: propertyListKeyNames
376386
)
377387

378388
renderReference.images = node?.metadata?.pageImages.compactMap { pageImage -> TopicImage? in

Sources/SwiftDocC/Model/Rendering/References/TopicRenderReference.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public struct TopicRenderReference: RenderReference, VariantContainer, Equatable
270270
let propertyListTitleStyle = try values.decodeIfPresent(PropertyListTitleStyle.self, forKey: .propertyListTitleStyle)
271271
let propertyListRawKey = try values.decodeIfPresent(String.self, forKey: .propertyListRawKey)
272272
let propertyListDisplayName = try values.decodeIfPresent(String.self, forKey: .propertyListDisplayName)
273-
if propertyListRawKey != nil || propertyListRawKey != nil || propertyListDisplayName != nil {
273+
if propertyListRawKey != nil || propertyListTitleStyle != nil || propertyListDisplayName != nil {
274274
propertyListKeyNames = PropertyListKeyNames(
275275
titleStyle: propertyListTitleStyle,
276276
rawKey: propertyListRawKey,

Tests/SwiftDocCTests/Rendering/RESTSymbolsTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import Foundation
1212
import XCTest
1313
@testable import SwiftDocC
14+
import SwiftDocCTestUtilities
15+
import SymbolKit
1416

1517
fileprivate extension [RenderBlockContent] {
1618
var firstParagraphText: String? {
@@ -309,4 +311,48 @@ class RESTSymbolsTests: XCTestCase {
309311

310312
AssertRoundtrip(for: object)
311313
}
314+
315+
func testReferenceOfEntitlementWithKeyName() throws {
316+
317+
func createDocumentationTopicRenderReferenceForSymbol(keyCustomName: String?) throws -> TopicRenderReference.PropertyListKeyNames {
318+
let exampleDocumentation = Folder(name: "unit-test.docc", content: [
319+
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(
320+
moduleName: "ModuleName",
321+
symbols: [
322+
SymbolGraph.Symbol(
323+
identifier: .init(precise: "symbol-id", interfaceLanguage: "swift"),
324+
names: .init(title: "Symbol Name", navigator: nil, subHeading: nil, prose: nil),
325+
pathComponents: ["Symbol Name"],
326+
docComment: nil,
327+
accessLevel: .public,
328+
kind: .init(parsedIdentifier: .typeProperty, displayName: "Type Property"),
329+
mixins: [SymbolGraph.Symbol.PlistDetails.mixinKey:SymbolGraph.Symbol.PlistDetails(rawKey: "plist-key-symbolname", customTitle: keyCustomName)]
330+
)
331+
]
332+
))
333+
])
334+
let (_, bundle, context) = try loadBundle(from: (try createTempFolder(content: [exampleDocumentation])))
335+
let moduleReference = ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/ModuleName", sourceLanguage: .swift)
336+
let moduleSymbol = try XCTUnwrap((try context.entity(with: moduleReference)).semantic as? Symbol)
337+
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: moduleReference)
338+
let renderNode = translator.visit(moduleSymbol) as! RenderNode
339+
return try XCTUnwrap((renderNode.references["doc://unit-test/documentation/ModuleName/Symbol_Name"] as? TopicRenderReference)?.propertyListKeyNames)
340+
}
341+
342+
// The symbol has a custom title.
343+
var propertyListKeyNames = try createDocumentationTopicRenderReferenceForSymbol(keyCustomName: "Symbol Custom Title")
344+
// Check that the reference contains the key symbol name.
345+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useDisplayName)
346+
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
347+
XCTAssertEqual(propertyListKeyNames.displayName, "Symbol Custom Title")
348+
349+
// The symbol does not have a custom title.
350+
propertyListKeyNames = try createDocumentationTopicRenderReferenceForSymbol(keyCustomName: nil)
351+
// Check that the reference does not contain the key symbol name.
352+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useRawKey)
353+
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
354+
XCTAssertNil(propertyListKeyNames.displayName)
355+
}
356+
357+
312358
}

0 commit comments

Comments
 (0)