Skip to content

Commit 9cf612d

Browse files
Fix property list key/name reference. (#1046)
Fix property list key/name reference. This implementation displays the property list custom name under the symbol abstract when curated. rdar://137113068
1 parent a674931 commit 9cf612d

File tree

2 files changed

+94
-22
lines changed

2 files changed

+94
-22
lines changed

Sources/SwiftDocC/Model/Rendering/DocumentationContentRenderer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@ public class DocumentationContentRenderer {
365365
let estimatedTime = (node?.semantic as? Timed)?.durationMinutes.flatMap(formatEstimatedDuration(minutes:))
366366

367367
// Add key information for property lists.
368+
// If the symbol overrides the title with a custom name, display the symbol key.
368369
let propertyListKeyNames = node?.symbol?.plistDetails.map {
369370
TopicRenderReference.PropertyListKeyNames(
370-
titleStyle: ($0.customTitle != nil) ? .useDisplayName : .useRawKey,
371+
titleStyle: (titleVariants[.fallback] != nil) ? .useDisplayName : .useRawKey,
371372
rawKey: $0.rawKey,
372373
displayName: $0.customTitle
373374
)

Tests/SwiftDocCTests/Rendering/RESTSymbolsTests.swift

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -314,44 +314,115 @@ class RESTSymbolsTests: XCTestCase {
314314

315315
func testReferenceOfEntitlementWithKeyName() throws {
316316

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-
])
317+
func createDocumentationTopicRenderReferenceForSymbol(keyCustomName: String?, extraFiles: [TextFile] = []) throws -> TopicRenderReference {
318+
let someSymbol = makeSymbol(
319+
id: "plist-key-symbolname",
320+
kind: .init(rawValue: "enum"),
321+
pathComponents: ["plist-key-symbolname"],
322+
otherMixins: [SymbolGraph.Symbol.PlistDetails(rawKey: "plist-key-symbolname", customTitle: keyCustomName)]
323+
)
324+
let symbols: [SymbolGraph.Symbol] = [someSymbol]
325+
let exampleDocumentation = Folder(
326+
name: "unit-test.docc",
327+
content: [
328+
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(
329+
moduleName: "ModuleName",
330+
symbols: symbols
331+
)),
332+
] + extraFiles
333+
)
334334
let (_, bundle, context) = try loadBundle(from: (try createTempFolder(content: [exampleDocumentation])))
335335
let moduleReference = ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/ModuleName", sourceLanguage: .swift)
336336
let moduleSymbol = try XCTUnwrap((try context.entity(with: moduleReference)).semantic as? Symbol)
337337
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: moduleReference)
338338
let renderNode = translator.visit(moduleSymbol) as! RenderNode
339-
return try XCTUnwrap((renderNode.references["doc://unit-test/documentation/ModuleName/Symbol_Name"] as? TopicRenderReference)?.propertyListKeyNames)
339+
return try XCTUnwrap((renderNode.references["doc://unit-test/documentation/ModuleName/plist-key-symbolname"] as? TopicRenderReference))
340340
}
341341

342342
// The symbol has a custom title.
343-
var propertyListKeyNames = try createDocumentationTopicRenderReferenceForSymbol(keyCustomName: "Symbol Custom Title")
343+
var propertyListKeyNames = try XCTUnwrap(createDocumentationTopicRenderReferenceForSymbol(keyCustomName: "Symbol Custom Title").propertyListKeyNames)
344344
// Check that the reference contains the key symbol name.
345-
XCTAssertEqual(propertyListKeyNames.titleStyle, .useDisplayName)
345+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useRawKey)
346346
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
347347
XCTAssertEqual(propertyListKeyNames.displayName, "Symbol Custom Title")
348348

349349
// The symbol does not have a custom title.
350-
propertyListKeyNames = try createDocumentationTopicRenderReferenceForSymbol(keyCustomName: nil)
350+
propertyListKeyNames = try XCTUnwrap(createDocumentationTopicRenderReferenceForSymbol(keyCustomName: nil).propertyListKeyNames)
351351
// Check that the reference does not contain the key symbol name.
352352
XCTAssertEqual(propertyListKeyNames.titleStyle, .useRawKey)
353353
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
354354
XCTAssertNil(propertyListKeyNames.displayName)
355+
356+
// The symbol has a custom title and is extended via markdown.
357+
var referenceNode = try XCTUnwrap(createDocumentationTopicRenderReferenceForSymbol(
358+
keyCustomName: "Symbol Custom Title",
359+
extraFiles: [
360+
TextFile(name: "plist-key-symbolname.md", utf8Content:
361+
"""
362+
# ``ModuleName/plist-key-symbolname``
363+
364+
A documentation extension for my plist-key-symbolname.
365+
"""
366+
)
367+
]
368+
))
369+
propertyListKeyNames = try XCTUnwrap(referenceNode.propertyListKeyNames)
370+
// Check that the reference contains the raw key and title matches the
371+
// key name.
372+
XCTAssertEqual(referenceNode.title, "plist-key-symbolname")
373+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useRawKey)
374+
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
375+
XCTAssertEqual(propertyListKeyNames.displayName, "Symbol Custom Title")
376+
377+
// The symbol has a custom title and is the markdown defines a `Display Name` directive.
378+
referenceNode = try XCTUnwrap(createDocumentationTopicRenderReferenceForSymbol(
379+
keyCustomName: "Symbol Custom Title",
380+
extraFiles: [
381+
TextFile(name: "plist-key-symbolname.md", utf8Content:
382+
"""
383+
# ``ModuleName/plist-key-symbolname``
384+
385+
@Metadata {
386+
@DisplayName("Custom Title")
387+
}
388+
389+
A documentation extension for my plist-key-symbolname.
390+
"""
391+
)
392+
]
393+
))
394+
propertyListKeyNames = try XCTUnwrap(referenceNode.propertyListKeyNames)
395+
// Check that the reference contains the raw key and the title matches the
396+
// markdown display name.
397+
XCTAssertEqual(referenceNode.title, "Custom Title")
398+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useDisplayName)
399+
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
400+
XCTAssertEqual(propertyListKeyNames.displayName, "Symbol Custom Title")
401+
402+
// The symbol does not have a custom title and is extended via markdown using a `Display Name` directive.
403+
referenceNode = try createDocumentationTopicRenderReferenceForSymbol(
404+
keyCustomName: nil,
405+
extraFiles: [
406+
TextFile(name: "plist-key-symbolname.md", utf8Content:
407+
"""
408+
# ``ModuleName/plist-key-symbolname``
409+
410+
@Metadata {
411+
@DisplayName("Custom Name")
412+
}
413+
414+
A documentation extension for my plist-key-symbolname.
415+
"""
416+
)
417+
]
418+
)
419+
propertyListKeyNames = try XCTUnwrap(referenceNode.propertyListKeyNames)
420+
// Check that the custom display name is the same as the markdown even that the
421+
// property list didn't define a custom title.
422+
XCTAssertEqual(referenceNode.title, "Custom Name")
423+
XCTAssertEqual(propertyListKeyNames.titleStyle, .useDisplayName)
424+
XCTAssertEqual(propertyListKeyNames.rawKey, "plist-key-symbolname")
425+
XCTAssertEqual(propertyListKeyNames.displayName, nil)
355426
}
356427

357428

0 commit comments

Comments
 (0)