Skip to content

Commit 2bf078a

Browse files
committed
[ConvertService] Set metadata.remoteSource
When using ConvertService to generate documentation, makes DocC set the symbol's declaration file path and line to the `metadata.remoteSource` property. rdar://104049149
1 parent 70d8cc1 commit 2bf078a

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,12 +1297,22 @@ public struct RenderNodeTranslator: SemanticVisitor {
12971297
)
12981298
)
12991299

1300+
var sourceRepository = sourceRepository
1301+
13001302
if shouldEmitSymbolSourceFileURIs {
13011303
node.metadata.sourceFileURIVariants = VariantCollection<String?>(
13021304
from: symbol.locationVariants
13031305
) { _, location in
13041306
location.uri
13051307
} ?? .init(defaultValue: nil)
1308+
1309+
// If a source repository is not set, set the device's
1310+
// filesystem as the source repository. This causes
1311+
// the `metadata.remoteSource` property to link to the
1312+
// file's location on disk.
1313+
if sourceRepository == nil {
1314+
sourceRepository = .localFilesystem()
1315+
}
13061316
}
13071317

13081318
if let sourceRepository = sourceRepository {

Sources/SwiftDocC/SourceRepository/SourceRepository.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,16 @@ public extension SourceRepository {
8989
formatLineNumber: { line in "lines-\(line)" }
9090
)
9191
}
92+
93+
/// Creates a source repository hosted by the device's filesystem.
94+
///
95+
/// Use this source repository to format `file://` links to files on the
96+
/// device where documentation is being presented.
97+
static func localFilesystem() -> SourceRepository {
98+
SourceRepository(
99+
checkoutPath: "",
100+
sourceServiceBaseURL: URL(fileURLWithPath: "/"),
101+
formatLineNumber: { line in "L\(line)" }
102+
)
103+
}
92104
}

Tests/SwiftDocCTests/DocumentationService/ConvertService/ConvertServiceTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,47 @@ class ConvertServiceTests: XCTestCase {
152152
}
153153
}
154154

155+
func testIncludesRemoteSourceInformationForSymbol() throws {
156+
let symbolGraphFile = Bundle.module.url(
157+
forResource: "mykit-one-symbol",
158+
withExtension: "symbols.json",
159+
subdirectory: "Test Resources"
160+
)!
161+
162+
let symbolGraph = try Data(contentsOf: symbolGraphFile)
163+
164+
let request = ConvertRequest(
165+
bundleInfo: testBundleInfo,
166+
externalIDsToConvert: ["s:5MyKit0A5ClassC10myFunctionyyF"],
167+
documentPathsToConvert: [],
168+
symbolGraphs: [symbolGraph],
169+
markupFiles: [],
170+
miscResourceURLs: []
171+
)
172+
173+
try processAndAssert(request: request) { message in
174+
XCTAssertEqual(message.type, "convert-response")
175+
XCTAssertEqual(message.identifier, "test-identifier-response")
176+
177+
let renderNodes = try JSONDecoder().decode(
178+
ConvertResponse.self, from: XCTUnwrap(message.payload)).renderNodes
179+
180+
XCTAssertEqual(renderNodes.count, 1)
181+
let data = try XCTUnwrap(renderNodes.first)
182+
let renderNode = try JSONDecoder().decode(RenderNode.self, from: data)
183+
184+
XCTAssertEqual(
185+
renderNode.metadata.remoteSource?.fileName,
186+
"test.swift"
187+
)
188+
189+
XCTAssertEqual(
190+
renderNode.metadata.remoteSource?.url.absoluteString,
191+
"file:///private/tmp/test.swift#L2"
192+
)
193+
}
194+
}
195+
155196
func testConvertSinglePageWithUnrelatedDocumentationExtension() throws {
156197
let symbolGraphFile = Bundle.module.url(
157198
forResource: "mykit-one-symbol",

Tests/SwiftDocCTests/SourceRepository/SourceRepositoryTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,12 @@ class SourceRepositoryTests: XCTestCase {
8383
URL(string: "https://example.com/source/file#lines-5")!
8484
)
8585
}
86+
87+
func testLocalFileSystemFormatting() {
88+
XCTAssertEqual(
89+
SourceRepository.localFilesystem()
90+
.format(sourceFileURL: URL(string: "file:///path/to/file")!, lineNumber: 5),
91+
URL(string: "file:///path/to/file#L5")!
92+
)
93+
}
8694
}

0 commit comments

Comments
 (0)