Skip to content

Commit 9f1c9d8

Browse files
authored
Support documenting http responses, property list values, parameters, etc. with spaces in the name (#1017)
1 parent 4d915c5 commit 9f1c9d8

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

Sources/SwiftDocC/Utility/MarkupExtensions/ListItemExtractor.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,37 +284,38 @@ private struct ExtractedTag {
284284
case httpBodyParameters
285285

286286
init?(_ string: String) {
287-
let components = string.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
287+
let separatorIndex = string.firstIndex(where: \.isWhitespace) ?? string.endIndex
288+
let secondComponent = String(string[separatorIndex...].drop(while: \.isWhitespace))
288289

289-
switch components.first?.lowercased() {
290+
switch string[..<separatorIndex].lowercased() {
290291
case "returns":
291292
self = .returns
292293
case "throws":
293294
self = .throws
294-
case "parameter" where components.count == 2:
295-
self = .parameter(String(components.last!))
295+
case "parameter" where !secondComponent.isEmpty:
296+
self = .parameter(secondComponent)
296297
case "parameters":
297298
self = .parameters
298-
case "dictionarykey" where components.count == 2:
299-
self = .dictionaryKey(String(components.last!))
299+
case "dictionarykey" where !secondComponent.isEmpty:
300+
self = .dictionaryKey(secondComponent)
300301
case "dictionarykeys":
301302
self = .dictionaryKeys
302-
case "possiblevalue" where components.count == 2:
303-
self = .possibleValue(String(components.last!))
303+
case "possiblevalue" where !secondComponent.isEmpty:
304+
self = .possibleValue(secondComponent)
304305
case "possiblevalues":
305306
self = .possibleValues
306307
case "httpbody":
307308
self = .httpBody
308-
case "httpresponse" where components.count == 2:
309-
self = .httpResponse(String(components.last!))
309+
case "httpresponse" where !secondComponent.isEmpty:
310+
self = .httpResponse(secondComponent)
310311
case "httpresponses":
311312
self = .httpResponses
312-
case "httpparameter" where components.count == 2:
313-
self = .httpParameter(String(components.last!))
313+
case "httpparameter" where !secondComponent.isEmpty:
314+
self = .httpParameter(secondComponent)
314315
case "httpparameters":
315316
self = .httpParameters
316-
case "httpbodyparameter" where components.count == 2:
317-
self = .httpBodyParameter(String(components.last!))
317+
case "httpbodyparameter" where !secondComponent.isEmpty:
318+
self = .httpBodyParameter(secondComponent)
318319
case "httpbodyparameters":
319320
self = .httpBodyParameters
320321
default:

Tests/SwiftDocCTests/Utility/ListItemExtractorTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,65 @@ import Markdown
1515

1616
class ListItemExtractorTests: XCTestCase {
1717

18+
func testSupportsSpacesInTaggedElementNames() throws {
19+
let testSource = URL(fileURLWithPath: "/path/to/test-source-\(ProcessInfo.processInfo.globallyUniqueString)")
20+
func extractedTags(_ markup: String) -> TaggedListItemExtractor {
21+
let document = Document(parsing: markup, source: testSource, options: .parseSymbolLinks)
22+
23+
var extractor = TaggedListItemExtractor()
24+
_ = extractor.visit(document)
25+
return extractor
26+
}
27+
28+
for whitespace in [" ", " ", "\t"] {
29+
let parameters = extractedTags("""
30+
- Parameter\(whitespace)some parameter with spaces: Some description of this parameter.
31+
""").parameters
32+
XCTAssertEqual(parameters.count, 1)
33+
let parameter = try XCTUnwrap(parameters.first)
34+
XCTAssertEqual(parameter.name, "some parameter with spaces")
35+
XCTAssertEqual(parameter.contents.map { $0.format() }, ["Some description of this parameter."])
36+
XCTAssertEqual(parameter.nameRange?.source?.path, testSource.path)
37+
XCTAssertEqual(parameter.range?.source?.path, testSource.path)
38+
}
39+
40+
let parameters = extractedTags("""
41+
- Parameters:
42+
- some parameter with spaces: Some description of this parameter.
43+
""").parameters
44+
XCTAssertEqual(parameters.count, 1)
45+
let parameter = try XCTUnwrap(parameters.first)
46+
XCTAssertEqual(parameter.name, "some parameter with spaces")
47+
XCTAssertEqual(parameter.contents.map { $0.format() }, ["Some description of this parameter."])
48+
XCTAssertEqual(parameter.nameRange?.source?.path, testSource.path)
49+
XCTAssertEqual(parameter.range?.source?.path, testSource.path)
50+
51+
let dictionaryKeys = extractedTags("""
52+
- DictionaryKeys:
53+
- some key with spaces: Some description of this key.
54+
""").dictionaryKeys
55+
XCTAssertEqual(dictionaryKeys.count, 1)
56+
let dictionaryKey = try XCTUnwrap(dictionaryKeys.first)
57+
XCTAssertEqual(dictionaryKey.name, "some key with spaces")
58+
XCTAssertEqual(dictionaryKey.contents.map { $0.format() }, ["Some description of this key."])
59+
60+
let possibleValues = extractedTags("""
61+
- PossibleValue some value with spaces: Some description of this value.
62+
""").possiblePropertyListValues
63+
XCTAssertEqual(possibleValues.count, 1)
64+
let possibleValue = try XCTUnwrap(possibleValues.first)
65+
XCTAssertEqual(possibleValue.value, "some value with spaces")
66+
XCTAssertEqual(possibleValue.contents.map { $0.format() }, ["Some description of this value."])
67+
XCTAssertEqual(possibleValue.nameRange?.source?.path, testSource.path)
68+
XCTAssertEqual(possibleValue.range?.source?.path, testSource.path)
69+
70+
XCTAssert(extractedTags("- Parameter: Missing parameter name.").parameters.isEmpty)
71+
XCTAssert(extractedTags("- Parameter : Missing parameter name.").parameters.isEmpty)
72+
73+
XCTAssert(extractedTags("- DictionaryKey: Missing key name.").dictionaryKeys.isEmpty)
74+
XCTAssert(extractedTags("- PossibleValue: Missing value name.").possiblePropertyListValues.isEmpty)
75+
}
76+
1877
func testExtractingTags() throws {
1978
try assertExtractsRichContentFor(
2079
tagName: "Returns",

0 commit comments

Comments
 (0)