Skip to content

Commit 057cf41

Browse files
committed
Prevent adding extra Snippets path component when converting to symbol graph symbols
rdar://104044227
1 parent e013865 commit 057cf41

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Sources/snippet-extract/Utility/SymbolGraph+Snippet.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ extension SymbolGraph.Symbol {
1919
let identifier = SymbolGraph.Symbol.Identifier(precise: "$snippet__\(moduleName).\(basename)", interfaceLanguage: "swift")
2020
let names = SymbolGraph.Symbol.Names.init(title: basename, navigator: nil, subHeading: nil, prose: nil)
2121

22-
var pathComponents = snippet.sourceFile.absoluteURL.deletingPathExtension().pathComponents[...]
22+
var pathComponents = Array(snippet.sourceFile.absoluteURL.deletingPathExtension().pathComponents[...])
2323

2424
guard let snippetsPathComponentIndex = pathComponents.firstIndex(where: {
2525
$0 == "Snippets"
2626
}) else {
2727
throw SnippetExtractCommand.ArgumentError.snippetNotContainedInSnippetsDirectory(snippet.sourceFile)
2828
}
29-
pathComponents = pathComponents[snippetsPathComponentIndex...]
29+
30+
// In theory, there may be differently named snippet root directories in the future.
31+
// Replace that path component with the standardized `Snippets`.
32+
pathComponents.replaceSubrange(pathComponents.startIndex...snippetsPathComponentIndex,
33+
with: CollectionOfOne("Snippets"))
3034

3135
let docComment = SymbolGraph.LineList(snippet.explanation
3236
.split(separator: "\n", maxSplits: Int.max, omittingEmptySubsequences: false)
@@ -39,7 +43,7 @@ extension SymbolGraph.Symbol {
3943

4044
self.init(identifier: identifier,
4145
names: names,
42-
pathComponents: ["Snippets"] + Array(pathComponents),
46+
pathComponents: pathComponents,
4347
docComment: docComment,
4448
accessLevel: accessLevel,
4549
kind: kind,

Tests/SwiftDocCPluginUtilitiesTests/Snippets/SnippetSymbolTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ class SnippetSymbolTests: XCTestCase {
3030
}
3131
})
3232
}
33+
34+
func testPathComponentsForSnippetSymbol() throws {
35+
let source = """
36+
// A snippet.
37+
foo() {}
38+
"""
39+
let snippet = Snippets.Snippet(parsing: source,
40+
sourceFile: URL(fileURLWithPath: "/path/to/my-package/Snippets/ASnippet.swift"))
41+
let symbol = try SymbolGraph.Symbol(snippet, moduleName: "my-package")
42+
XCTAssertEqual(["Snippets", "ASnippet"], symbol.pathComponents)
43+
}
3344
}

0 commit comments

Comments
 (0)