Skip to content

Commit a674931

Browse files
Don't suggest replacing part of the symbol name in link diagnostic (#1045)
rdar://136865335 Co-authored-by: Sofía Rodríguez <[email protected]>
1 parent d0abbaf commit a674931

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy+Error.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ extension PathHierarchy.Error {
9292
foundDisambiguation: Substring,
9393
solutions: [Solution]
9494
) {
95-
let pathPrefix = partialResultPrefix + nextPathComponent.name
96-
let foundDisambiguation = nextPathComponent.full.dropFirst(nextPathComponent.name.count)
95+
guard let symbolName = candidates.first?.node.name else {
96+
return (partialResultPrefix, "", [])
97+
}
98+
let foundDisambiguation = nextPathComponent.full.dropFirst(symbolName.count)
99+
let pathPrefix: Substring = partialResultPrefix + nextPathComponent.full.prefix(symbolName.count)
100+
97101
let replacementRange = SourceRange.makeRelativeRange(startColumn: pathPrefix.count, length: foundDisambiguation.count)
98102

99103
let solutions: [Solution] = candidates

Tests/SwiftDocCTests/Infrastructure/PathHierarchyTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,37 @@ class PathHierarchyTests: XCTestCase {
14991499
}
15001500
}
15011501

1502+
func testDiagnosticDoesNotSuggestReplacingPartOfSymbolName() throws {
1503+
let exampleDocumentation = Folder(name: "CatalogName.docc", content: [
1504+
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(moduleName: "ModuleName", symbols: [
1505+
makeSymbol(id: "some-class-id-1", kind: .class, pathComponents: ["SomeClass-(Something)"]),
1506+
makeSymbol(id: "some-class-id-2", kind: .class, pathComponents: ["SomeClass-(Something)"]),
1507+
])),
1508+
])
1509+
let catalogURL = try exampleDocumentation.write(inside: createTemporaryDirectory())
1510+
let (_, _, context) = try loadBundle(from: catalogURL)
1511+
let tree = context.linkResolver.localResolver.pathHierarchy
1512+
1513+
XCTAssert(context.problems.isEmpty, "Unexpected problems \(context.problems.map(\.diagnostic.summary))")
1514+
1515+
try assertPathCollision("ModuleName/SomeClass-(Something)", in: tree, collisions: [
1516+
("some-class-id-1", "-5bq4k"),
1517+
("some-class-id-2", "-5bq4n"),
1518+
])
1519+
1520+
XCTAssertThrowsError(
1521+
try tree.findNode(path: "ModuleName/SomeClass-(Something)", onlyFindSymbols: true, parent: nil)
1522+
) { untypedError in
1523+
let error = untypedError as! PathHierarchy.Error
1524+
let referenceError = error.makeTopicReferenceResolutionErrorInfo() { context.linkResolver.localResolver.fullName(of: $0, in: context) }
1525+
XCTAssertEqual(referenceError.message, "'SomeClass-(Something)' is ambiguous at '/ModuleName'")
1526+
XCTAssertEqual(referenceError.solutions.map(\.summary), [
1527+
"Insert \'-5bq4k\' for \n\'SomeClass-(Something)\'",
1528+
"Insert \'-5bq4n\' for \n\'SomeClass-(Something)\'",
1529+
])
1530+
}
1531+
}
1532+
15021533
func testSnippets() throws {
15031534
let (_, context) = try testBundleAndContext(named: "Snippets")
15041535
let tree = context.linkResolver.localResolver.pathHierarchy

0 commit comments

Comments
 (0)