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

Add new DisplayName directive to customize symbol names #92

Merged
merged 13 commits into from
Mar 3, 2022

Conversation

d-ronnqvist
Copy link
Contributor

@d-ronnqvist d-ronnqvist commented Feb 19, 2022

Bug/issue #, if applicable: SR-15572 rdar://72969138

Summary

This adds a new @DisplayName directive to allow for customization of symbol display names, as described in this forums post.

There are two pieces that don't work like what's described in that forums post yet:

  • Customizing the display name of a module symbol doesn't change how that module is listed as a Technology, neither for the module symbol itself nor for other symbols in that module.
  • Documentation without a docc catalog doesn't leverage this directive yet to customize the display name based on the command line display name argument.

Dependencies

This depends on swiftlang/swift-markdown#27

Testing

With a documentation catalog

In a documentation catalog with symbol graph files and documentation extension files:

  1. Preview the documentation for that docc catalog with docc preview /path/to/Example.docc

  2. In the documentation extension file for the module symbol: add a display name directive. For example:

@Metadata {
  @DisplayName("Test name")
}

Navigate to that page in the preview. The custom name ("Test name") should appear as the title, technology name, and breadcrumb for that symbol.

Navigate to a child symbol within that technology. The custom name ("Test name") should appear as the technology name and in the breadcrumbs for that symbol.

Without a documentation catalog

With a collection of symbol graph files:

  1. Preview the documentation and pass a custom display name on the command line:
docc \
 --fallback-display-name "Test name again" \
 --additional-symbol-graph-dir /path/to/directory-of-symbol-graph-files/

Navigate to that page in the preview. The custom name ("Test name again") should appear as the title, technology name, and breadcrumb for that symbol.

Navigate to a child symbol within that technology. The custom name ("Test name again") should appear as the technology name and in the breadcrumbs for that symbol.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@@ -148,7 +148,8 @@ public struct DocumentationNode {
/// > Warning: Using this initializer will not report any diagnostics raised during the initialization of `DocumentationNode`.
@available(*, deprecated, message: "Use init(reference:symbol:platformName:moduleName:article:engine:) instead")
public init(reference: ResolvedTopicReference, symbol: SymbolGraph.Symbol, platformName: String?, moduleName: String, article: Article?, problems: inout [Problem]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if to remove this initializer. It's been deprecated for a while now.

@d-ronnqvist d-ronnqvist marked this pull request as ready for review March 2, 2022 02:55
Package.resolved Outdated
"branch": "main",
"revision": "9a9050095ec93853c0ed01930a9d108d79dc4776",
"branch": "unlabeled-directive-argument",
"revision": "5cb5ae2c69fe2d0d490fe62fb119bd724fd98ac6",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your plan for landing these changes? It doesn't look like we're introducing new API in that branch that we're using in DocC, but I assume that some tests will fail without the Swift-Markdown changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan on landing the Swift-Markdown changes first and then this. Without the Swift-Markdown changes, this new directive (and the tests that make use of it) will encounter parsing errors.

@@ -12,11 +12,12 @@ import Foundation
import SymbolKit

/// A type that provides documentation bundles that it discovers by traversing the local file system.
public struct GeneratedDataProvider: DocumentationWorkspaceDataProvider {
public class GeneratedDataProvider: DocumentationWorkspaceDataProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate here? Switching this to a class makes this type harder to use in concurrent environments. Also, might want to make this final.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is driven by needing to know what data to return from contentsOfURL. That data depend on if the generated bundle contains one or more modules. However, the argument to this method doesn't indicate one or the other so the number of modules (or its derivate values) need to be stored as private state. The information needed to know if the bundle represents one or more modules isn't known until bundles(options:) is called so it can't be computed during initialization.

Given the choice between making one provider a class or making bundles(options:) a mutating func I went with the former because it was a smaller change but I can see the argument for making mutation this a formal part of the provider API.

In practice, a provider is only asked to provide its bundles once and is only asked to provide content for urls in the returned bundle, so this won't have any impact on concurrency. If a developer did set up a shared GeneratedDataProvider with more than one documentation context, the real issue would be calling bundles(options:) twice.

This class isn't open, so making it final would only make a difference within Swift-DocC. I don't think we have a case for subclassing this but we don't strictly have a case for not allowing subclassing within Swift-DocC either.

}
let extendedModule: String? = unifiedSymbol.mixins
.first(where: { $0.0.platform == platformName })?.value
.getValueIfPresent(for: SymbolGraph.Symbol.Swift.Extension.self)?.extendedModule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this change, can you elaborate? Should we not traverse the mixins for each language variant the symbol is available in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, module names are required to be the same in all languages. The Symbol semantic no longer stores variants of module related information so this computes a single language agnostic value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense, because the UnifiedSymbolGraph logic relies on the symbol graph files for each language variant of a module to be the same, right @QuietMisdreavus?

let isDocumentationExtension = title.child(at: 0) is AnyLink
if !isDocumentationExtension, let metadata = optionalMetadata, let displayName = metadata.displayName {
let diagnosticSummary = """
A \(DisplayName.directiveName.singleQuoted) directive is only supported in documentation extensions files. To customize the display name of an article, change the content of the level-1 heading.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A \(DisplayName.directiveName.singleQuoted) directive is only supported in documentation extensions files. To customize the display name of an article, change the content of the level-1 heading.
A \(DisplayName.directiveName.singleQuoted) directive is only supported in documentation extension files. To customize the display name of an article, change the content of the level-1 heading.

let solutions: [Solution]
if let displayNameRange = displayName.originalMarkup.range, let titleRange = title.range {
let removeDisplayNameReplacement = Replacement(range: displayNameRange, replacement: "")
let changeTitleReplacement = Replacement(range: titleRange, replacement: "# \(displayName.name)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!



/// A cache of plain string module names, keyed by the module node reference.
private var moduleNameCache: [ResolvedTopicReference: String] = [:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand the need for this cache, rather than just using the node's title. Is this for performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is only for performance. The module symbol's name will be displayed on all symbol's pages so it's a good candidate for a value to cache.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. Wondering if we should make this an IOU so that we don't accidentally access it before it's been populated. We do the same for DocumentationContext.rootModules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good idea. I made this non-optional and added asserts about how this API is used.

@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

 - Assert if module names are not found
 - Fix spelling in Article diagnostic message
@d-ronnqvist d-ronnqvist force-pushed the customize-symbol-display-name branch from 3ff4c32 to 609e200 Compare March 2, 2022 23:03
@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants