Skip to content

Commit 5a9d30c

Browse files
authored
Add a suffix property to LinkCompletionTools.ParsedDisambiguation (#1151)
* Add a `suffix` property to `LinkCompletionTools.ParsedDisambiguation` * Fix parsing bug for disambiguation with both parameter and return types
1 parent d5bf7a5 commit 5a9d30c

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Sources/SwiftDocC/DocumentationService/Convert/Symbol Link Resolution/LinkCompletionTools.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ public enum LinkCompletionTools {
7474
self = .none
7575
}
7676
}
77+
78+
/// A string representation of the disambiguation.
79+
public var suffix: String {
80+
typealias Disambiguation = PathHierarchy.DisambiguationContainer.Disambiguation
81+
82+
switch self {
83+
case .kindAndOrHash(let kind?, nil):
84+
return Disambiguation.kind(kind).makeSuffix()
85+
case .kindAndOrHash(nil, let hash?):
86+
return Disambiguation.hash(hash).makeSuffix()
87+
case .kindAndOrHash(let kind?, let hash?): // This is never necessary but a developer could redundantly write it in a parsed link
88+
return Disambiguation.kind(kind).makeSuffix() + Disambiguation.hash(hash).makeSuffix()
89+
90+
case .typeSignature(let parameterTypes?, nil):
91+
return Disambiguation.parameterTypes(parameterTypes).makeSuffix()
92+
case .typeSignature(nil, let returnTypes?):
93+
return Disambiguation.returnTypes(returnTypes).makeSuffix()
94+
case .typeSignature(let parameterTypes?, let returnTypes?):
95+
return Disambiguation.mixedTypes(parameterTypes: parameterTypes, returnTypes: returnTypes).makeSuffix()
96+
97+
// Unexpected error cases
98+
case .kindAndOrHash(kind: nil, hash: nil):
99+
assertionFailure("Parsed `.kindAndOrHash` disambiguation missing both kind and hash should use `.none` instead. This is a logic bug.")
100+
return Disambiguation.none.makeSuffix()
101+
case .typeSignature(parameterTypes: nil, returnTypes: nil):
102+
assertionFailure("Parsed `.typeSignature` disambiguation missing both parameter types and return types should use `.none` instead. This is a logic bug.")
103+
return Disambiguation.none.makeSuffix()
104+
105+
// Since this is within DocC we want to have an error if we don't handle new future cases.
106+
case .none, ._nonFrozenEnum_useDefaultCase:
107+
return Disambiguation.none.makeSuffix()
108+
}
109+
}
77110
}
78111

79112
/// Suggests the minimal most readable disambiguation string for each symbol with the same name.

Tests/SwiftDocCTests/Infrastructure/Symbol Link Resolution/LinkCompletionToolsTests.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,61 @@ class LinkCompletionToolsTests: XCTestCase {
162162
])
163163
}
164164

165+
func testDisambiguationSuffixStrings() {
166+
typealias Disambiguation = LinkCompletionTools.ParsedDisambiguation
167+
168+
XCTAssertEqual(Disambiguation.none.suffix,"")
169+
170+
XCTAssertEqual(Disambiguation.kindAndOrHash(kind: "class", hash: nil).suffix,
171+
"-class")
172+
XCTAssertEqual(Disambiguation.kindAndOrHash(kind: nil, hash: "z3jl").suffix,
173+
"-z3jl")
174+
175+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: [], returnTypes: nil).suffix,
176+
"-()")
177+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: ["Int"], returnTypes: nil).suffix,
178+
"-(Int)")
179+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: ["Int", "_", "String"], returnTypes: nil).suffix,
180+
"-(Int,_,String)")
181+
182+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: nil, returnTypes: []).suffix,
183+
"->()")
184+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: nil, returnTypes: ["Int"]).suffix,
185+
"->Int")
186+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: nil, returnTypes: ["Int", "_", "String"]).suffix,
187+
"->(Int,_,String)")
188+
189+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: ["_", "Bool"], returnTypes: []).suffix,
190+
"-(_,Bool)->()")
191+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: ["_", "Bool"], returnTypes: ["Int"]).suffix,
192+
"-(_,Bool)->Int")
193+
XCTAssertEqual(Disambiguation.typeSignature(parameterTypes: ["_", "Bool"], returnTypes: ["Int", "_", "String"]).suffix,
194+
"-(_,Bool)->(Int,_,String)")
195+
}
196+
197+
func testParsingDisambiguationSuffixStrings() throws {
198+
for disambiguationSuffixString in [
199+
"",
200+
"-class",
201+
"-z3jl",
202+
203+
"-()",
204+
"-(Int)",
205+
"-(Int,_,String)",
206+
207+
"->()",
208+
"->Int",
209+
"->(Int,_,String)",
210+
211+
"-(_,Bool)->()",
212+
"-(_,Bool)->Int",
213+
"-(_,Bool)->(Int,_,String)",
214+
] {
215+
let parsedLinkComponent = try XCTUnwrap(LinkCompletionTools.parse(linkString: "SymbolName\(disambiguationSuffixString)").first)
216+
XCTAssertEqual(parsedLinkComponent.disambiguation.suffix, disambiguationSuffixString)
217+
}
218+
}
219+
165220
func testSuggestingBothParameterAndReturnTypesInTheSameDisambiguation() {
166221
let overloads = [
167222
(parameters: ["Int"], returns: []), // (Int) -> Void

0 commit comments

Comments
 (0)