Skip to content

Commit 5d8ace7

Browse files
authored
Move configuration before context initialization (#1031)
1 parent 9cf612d commit 5d8ace7

25 files changed

+513
-455
lines changed

Sources/SwiftDocC/DocumentationService/Convert/ConvertService.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ public struct ConvertService: DocumentationService {
150150
provider = inMemoryProvider
151151
}
152152

153-
let context = try DocumentationContext(dataProvider: workspace)
154-
context.knownDisambiguatedSymbolPathComponents = request.knownDisambiguatedSymbolPathComponents
153+
var configuration = DocumentationContext.Configuration()
154+
155+
configuration.convertServiceConfiguration.knownDisambiguatedSymbolPathComponents = request.knownDisambiguatedSymbolPathComponents
155156

156157
// Enable support for generating documentation for standalone articles and tutorials.
157-
context.allowsRegisteringArticlesWithoutTechnologyRoot = true
158-
context.considerDocumentationExtensionsThatDoNotMatchSymbolsAsResolved = true
158+
configuration.convertServiceConfiguration.allowsRegisteringArticlesWithoutTechnologyRoot = true
159+
configuration.convertServiceConfiguration.considerDocumentationExtensionsThatDoNotMatchSymbolsAsResolved = true
159160

160-
context.configureSymbolGraph = { symbolGraph in
161+
configuration.convertServiceConfiguration.symbolGraphTransformer = { symbolGraph in
161162
for (symbolIdentifier, overridingDocumentationComment) in request.overridingDocumentationComments ?? [:] {
162163
symbolGraph.symbols[symbolIdentifier]?.docComment = SymbolGraph.LineList(
163164
overridingDocumentationComment.map(SymbolGraph.LineList.Line.init(_:))
@@ -172,10 +173,12 @@ public struct ConvertService: DocumentationService {
172173
convertRequestIdentifier: messageIdentifier
173174
)
174175

175-
context.convertServiceFallbackResolver = resolver
176-
context.globalExternalSymbolResolver = resolver
176+
configuration.convertServiceConfiguration.fallbackResolver = resolver
177+
configuration.externalDocumentationConfiguration.globalSymbolResolver = resolver
177178
}
178-
179+
180+
let context = try DocumentationContext(dataProvider: workspace, configuration: configuration)
181+
179182
var converter = try self.converter ?? DocumentationConverter(
180183
documentationBundleURL: request.bundleLocation ?? URL(fileURLWithPath: "/"),
181184
emitDigest: false,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2024 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+
extension DocumentationContext {
12+
13+
@available(*, deprecated, renamed: "configuration.externalMetadata", message: "Use 'configuration.externalMetadata' instead. This deprecated API will be removed after Swift 6.2 is released.")
14+
public var externalMetadata: ExternalMetadata {
15+
get { configuration.externalMetadata }
16+
set { configuration.externalMetadata = newValue }
17+
}
18+
19+
@available(*, deprecated, renamed: "configuration.externalDocumentationConfiguration.sources", message: "Use 'configuration.externalDocumentationConfiguration.sources' instead. This deprecated API will be removed after Swift 6.2 is released.")
20+
public var externalDocumentationSources: [BundleIdentifier: ExternalDocumentationSource] {
21+
get { configuration.externalDocumentationConfiguration.sources }
22+
set { configuration.externalDocumentationConfiguration.sources = newValue }
23+
}
24+
25+
@available(*, deprecated, renamed: "configuration.externalDocumentationConfiguration.globalSymbolResolver", message: "Use 'configuration.externalDocumentationConfiguration.globalSymbolResolver' instead. This deprecated API will be removed after Swift 6.2 is released.")
26+
public var globalExternalSymbolResolver: GlobalExternalSymbolResolver? {
27+
get { configuration.externalDocumentationConfiguration.globalSymbolResolver }
28+
set { configuration.externalDocumentationConfiguration.globalSymbolResolver = newValue }
29+
}
30+
31+
@available(*, deprecated, renamed: "configuration.experimentalCoverageConfiguration.shouldStoreManuallyCuratedReferences", message: "Use 'configuration.experimentalCoverageConfiguration.shouldStoreManuallyCuratedReferences' instead. This deprecated API will be removed after Swift 6.2 is released.")
32+
public var shouldStoreManuallyCuratedReferences: Bool {
33+
get { configuration.experimentalCoverageConfiguration.shouldStoreManuallyCuratedReferences }
34+
set { configuration.experimentalCoverageConfiguration.shouldStoreManuallyCuratedReferences = newValue }
35+
}
36+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2024 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 Foundation
12+
import SymbolKit
13+
14+
extension DocumentationContext {
15+
/// A collection of configuration to apply to a documentation context during initialization.
16+
public struct Configuration {
17+
// This type exists so that the context doesn't need to be modified in-between creation and registration of the data provider.
18+
// It's a small step towards making the context fully immutable.
19+
20+
/// Creates a new default configuration.
21+
public init() {}
22+
23+
// MARK: Convert Service configuration
24+
25+
/// Configuration specific to the ``ConvertService``.
26+
var convertServiceConfiguration = ConvertServiceConfiguration()
27+
28+
/// A collection of configuration specific to the ``ConvertService``.
29+
struct ConvertServiceConfiguration {
30+
/// The mapping of external symbol identifiers to known disambiguated symbol path components.
31+
///
32+
/// In situations where the local documentation context doesn't contain all of the current module's
33+
/// symbols, for example when using a ``ConvertService`` with a partial symbol graph,
34+
/// the documentation context is otherwise unable to accurately detect a collision for a given symbol and correctly
35+
/// disambiguate its path components. This value can be used to inject already disambiguated symbol
36+
/// path components into the documentation context.
37+
var knownDisambiguatedSymbolPathComponents: [String: [String]]?
38+
39+
/// Controls whether bundle registration should allow registering articles when no technology root is defined.
40+
///
41+
/// Set this property to `true` to enable registering documentation for standalone articles,
42+
/// for example when using ``ConvertService``.
43+
var allowsRegisteringArticlesWithoutTechnologyRoot = false
44+
45+
/// Controls whether documentation extension files are considered resolved even when they don't match a symbol.
46+
///
47+
/// Set this property to `true` to always consider documentation extensions as "resolved", for example when using ``ConvertService``.
48+
///
49+
/// > Note:
50+
/// > Setting this property tor `true` means taking over the responsibility to match documentation extension files to symbols
51+
/// > diagnosing unmatched documentation extension files, and diagnostic symbols that match multiple documentation extension files.
52+
var considerDocumentationExtensionsThatDoNotMatchSymbolsAsResolved = false
53+
54+
/// A resolver that attempts to resolve local references to content that wasn't included in the catalog or symbol input.
55+
///
56+
/// > Warning:
57+
/// > Setting a fallback reference resolver makes accesses to the context non-thread-safe.
58+
/// > This is because the fallback resolver can run during both local link resolution and during rendering, which both happen concurrently for each page.
59+
/// > In practice this shouldn't matter because the convert service only builds documentation for one page.
60+
var fallbackResolver: ConvertServiceFallbackResolver?
61+
62+
/// A closure that modifies each symbol graph before the context registers the symbol graph's information.
63+
var symbolGraphTransformer: ((inout SymbolGraph) -> ())? = nil
64+
}
65+
66+
// MARK: External metadata
67+
68+
/// External metadata injected into the context, for example via command line arguments.
69+
public var externalMetadata = ExternalMetadata()
70+
71+
// MARK: External documentation
72+
73+
/// Configuration related to external sources of documentation.
74+
public var externalDocumentationConfiguration = ExternalDocumentationConfiguration()
75+
76+
/// A collection of configuration related to external sources of documentation.
77+
public struct ExternalDocumentationConfiguration {
78+
/// The lookup of external documentation sources by their bundle identifiers.
79+
public var sources: [BundleIdentifier: ExternalDocumentationSource] = [:]
80+
/// A type that resolves all symbols that are referenced in symbol graph files but can't be found in any of the locally available symbol graph files.
81+
public var globalSymbolResolver: GlobalExternalSymbolResolver?
82+
/// A list of URLs to documentation archives that the local documentation depends on.
83+
@_spi(ExternalLinks) // This needs to be public SPI so that the ConvertAction can set it.
84+
public var dependencyArchives: [URL] = []
85+
}
86+
87+
// MARK: Experimental coverage
88+
89+
/// Configuration related to the experimental documentation coverage feature.
90+
package var experimentalCoverageConfiguration = ExperimentalCoverageConfiguration()
91+
92+
/// A collection of configuration related to the experimental documentation coverage feature.
93+
package struct ExperimentalCoverageConfiguration {
94+
/// Controls whether the context stores the set of references that are manually curated.
95+
package var shouldStoreManuallyCuratedReferences: Bool = false
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)