-
Notifications
You must be signed in to change notification settings - Fork 128
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 directive for linking symbols as alternate representations #1097
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2c02ca
Add `@AlternateRepresentation` directive
anferbui c46e029
Resolve topic references in `@AlternateRepresentation`
anferbui 213ee04
Diagnose duplicate representations of `@AlternateRepresentation`
anferbui 711469a
Emit diagnostics for non-symbol pages
anferbui 74f3f2c
Render alternate representations as node variants
anferbui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Sources/SwiftDocC/Semantics/Metadata/AlternateRepresentation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Foundation | ||
import Markdown | ||
|
||
|
||
/// A directive that configures an alternate language representations of a symbol. | ||
/// | ||
/// An API that can be called from more than one source language has more than one language representation. | ||
/// | ||
/// Whenever possible, prefer to define alternative language representations for a symbol by using in-source annotations | ||
/// such as the `@objc` and `@_objcImplementation` attributes in Swift, | ||
/// or the `NS_SWIFT_NAME` macro in Objective C. | ||
/// | ||
/// If your source language doesn’t have a mechanism for specifying alternate representations or if your intended alternate representation isn't compatible with those attributes, | ||
/// you can use the `@AlternateRepresentation` directive to specify another symbol that should be considered an alternate representation of the documented symbol. | ||
/// | ||
/// ```md | ||
/// @Metadata { | ||
/// @AlternateRepresentation(MyApp/MyClass/property) | ||
/// } | ||
/// ``` | ||
/// If you prefer, you can wrap the symbol link in a set of double backticks (\`\`), or use any other supported syntax for linking to symbols. | ||
/// For more information about linking to symbols, see <doc:linking-to-symbols-and-other-content>. | ||
/// | ||
/// This provides a hint to the renderer as to the alternate language representations for the current symbol. | ||
/// The renderer may use this hint to provide a link to these alternate symbols. | ||
/// For example, Swift-DocC-Render shows a toggle between supported languages, where switching to a different language representation will redirect to the documentation for the configured alternate symbol. | ||
/// | ||
/// ### Special considerations | ||
/// | ||
/// Links containing a colon (`:`) must be wrapped in quotes: | ||
anferbui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ```md | ||
/// @Metadata { | ||
/// @AlternateRepresentation("doc://com.example/documentation/MyClass/property") | ||
/// @AlternateRepresentation("MyClass/myFunc(_:_:)") | ||
/// } | ||
/// ``` | ||
/// | ||
/// The `@AlternateRepresentation` directive only specifies an alternate language representation in one direction. | ||
/// To define a two-way relationship, add an `@AlternateRepresentation` directive, linking to this symbol, to the other symbol as well. | ||
/// | ||
/// You can only configure custom alternate language representations for languages that the documented symbol doesn't already have a language representation for, | ||
/// either from in-source annotations or from a previous `@AlternateRepresentation` directive. | ||
public final class AlternateRepresentation: Semantic, AutomaticDirectiveConvertible { | ||
public static let introducedVersion = "6.1" | ||
|
||
// Directive parameter definition | ||
|
||
/// A link to another symbol that should be considered an alternate language representation of the current symbol. | ||
/// | ||
/// If you prefer, you can wrap the symbol link in a set of double backticks (\`\`). | ||
@DirectiveArgumentWrapped( | ||
name: .unnamed, | ||
parseArgument: { _, argumentValue in | ||
// Allow authoring of links with leading and trailing "``"s | ||
var argumentValue = argumentValue | ||
if argumentValue.hasPrefix("``"), argumentValue.hasSuffix("``") { | ||
argumentValue = String(argumentValue.dropFirst(2).dropLast(2)) | ||
} | ||
guard let url = ValidatedURL(parsingAuthoredLink: argumentValue), !url.components.path.isEmpty else { | ||
return nil | ||
anferbui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return .unresolved(UnresolvedTopicReference(topicURL: url)) | ||
} | ||
) | ||
public internal(set) var reference: TopicReference | ||
|
||
static var keyPaths: [String : AnyKeyPath] = [ | ||
"reference" : \AlternateRepresentation._reference | ||
] | ||
|
||
// Boiler-plate required by conformance to AutomaticDirectiveConvertible | ||
|
||
public var originalMarkup: Markdown.BlockDirective | ||
|
||
@available(*, deprecated, message: "Do not call directly. Required for 'AutomaticDirectiveConvertible") | ||
init(originalMarkup: Markdown.BlockDirective) { | ||
self.originalMarkup = originalMarkup | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is the only place that checks that the link resolved we should emit a warning with the right link source range so that the developer knows that the link failed to resolve.
unresolvedReferenceProblem(...)
can create the same type of diagnostic as for other unresolved links by passing theTopicReferenceResolutionErrorInfo
from theTopicReferenceResolutionResult.failure(_:_:)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will already emit a diagnostic in the code below so I don't think it's also needed here, WDYT?
swift-docc/Sources/SwiftDocC/Infrastructure/DocumentationContext.swift
Lines 614 to 621 in bc6df51
Since this ultimately ends up adding this diagnostic:
swift-docc/Sources/SwiftDocC/Semantics/ReferenceResolver.swift
Line 117 in bc6df51
(I can add a comment to explain why we don't need a diagnostic here)