Skip to content

Commit 83e1522

Browse files
committed
Extract parameter documentation with raw identifiers
1 parent 63e9704 commit 83e1522

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

Sources/SwiftLanguageService/ParametersDocumentationExtractor.swift

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private struct ParametersDocumentationExtractor {
168168

169169
let remainingContent = String(paragraphContent[prefixEnd...]).trimmingCharacters(in: .whitespaces)
170170

171-
return extractParam(firstTextContent: remainingContent, listItem: listItem)
171+
return extractParam(firstTextContent: remainingContent, listItem: listItem, single: true)
172172
}
173173

174174
/// Extracts a parameter field from a list item (used for parameter outline items)
@@ -177,23 +177,23 @@ private struct ParametersDocumentationExtractor {
177177
return nil
178178
}
179179

180-
guard let paragraphText = paragraph.child(at: 0) as? Text else {
181-
return nil
182-
}
180+
let firstText = paragraph.child(at: 0) as? Text
183181

184-
return extractParam(firstTextContent: paragraphText.string, listItem: listItem)
182+
return extractParam(firstTextContent: firstText?.string ?? "", listItem: listItem, single: false)
185183
}
186184

187185
/// Extracts a parameter field from a list item provided the relevant first text content allowing reuse in ``extractOutlineItem`` and ``extractSingle``
188186
///
189187
/// - Parameters:
190188
/// - firstTextContent: The content of the first text child of the list item's first paragraph
191189
/// - listItem: The list item to extract the parameter from
190+
/// - single: Whether the parameter is a single parameter or part of a parameter outline
192191
///
193192
/// - Returns: A tuple containing the parameter name and documentation if a parameter was found, nil otherwise.
194193
private func extractParam(
195194
firstTextContent: String,
196-
listItem: ListItem
195+
listItem: ListItem,
196+
single: Bool
197197
) -> Parameter? {
198198
guard let paragraph = listItem.child(at: 0) as? Paragraph else {
199199
return nil
@@ -202,7 +202,7 @@ private struct ParametersDocumentationExtractor {
202202
let components = firstTextContent.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false)
203203

204204
guard components.count == 2 else {
205-
return nil
205+
return extractWithRawIdentifier(from: listItem, single: single)
206206
}
207207

208208
let name = String(components[0]).trimmingCharacters(in: .whitespaces)
@@ -217,6 +217,44 @@ private struct ParametersDocumentationExtractor {
217217

218218
return Parameter(name: name, documentation: documentation)
219219
}
220+
221+
/// Extracts a parameter with its name as a raw identifier.
222+
///
223+
/// Example:
224+
/// ```markdown
225+
/// - Parameter `foo bar`: documentation
226+
/// - Parameters:
227+
/// - `foo bar`: documentation
228+
/// ```
229+
///
230+
/// - Parameters:
231+
/// - listItem: The list item to extract the parameter from
232+
/// - single: Whether the parameter is a single parameter or part of a parameter outline
233+
func extractWithRawIdentifier(from listItem: ListItem, single: Bool) -> Parameter? {
234+
/// The index of ``InlineCode`` for the raw identifier parameter name in the first paragraph of ``listItem``
235+
let inlineCodeIndex = single ? 1 : 0
236+
237+
guard let paragraph = listItem.child(at: 0) as? Paragraph,
238+
let rawIdentifier = paragraph.child(at: inlineCodeIndex) as? InlineCode,
239+
let text = paragraph.child(at: inlineCodeIndex + 1) as? Text
240+
else {
241+
return nil
242+
}
243+
244+
let textContent = text.string.trimmingCharacters(in: .whitespaces)
245+
246+
guard textContent.hasPrefix(":") else {
247+
return nil
248+
}
249+
250+
let remainingTextContent = String(textContent.dropFirst()).trimmingCharacters(in: .whitespaces)
251+
let remainingParagraphChildren =
252+
[Text(remainingTextContent)] + paragraph.inlineChildren.dropFirst(inlineCodeIndex + 2)
253+
let remainingChildren = [Paragraph(remainingParagraphChildren)] + listItem.blockChildren.dropFirst(1)
254+
let documentation = Document(remainingChildren).format()
255+
256+
return Parameter(name: rawIdentifier.code, documentation: documentation)
257+
}
220258
}
221259

222260
/// Extracts parameter documentation from markdown text.

Tests/SourceKitLSPTests/ParametersDocumentationExtractorTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,27 @@ final class ParametersDocumentationExtractorTests: XCTestCase {
324324
"""
325325
)
326326
}
327+
328+
func testSeparatedParameterWithRawIdentifier() {
329+
let comment = """
330+
- Parameter `foo: bar :) `: hello
331+
"""
332+
333+
let (parameters, remaining) = extractParametersDocumentation(from: comment)
334+
335+
XCTAssertEqual(parameters, ["foo: bar :) ": "hello"])
336+
XCTAssertTrue(remaining.isEmpty)
337+
}
338+
339+
func testParameterOutlineWithRawIdentifier() {
340+
let comment = """
341+
- Parameters:
342+
- `foo: bar :) `: hello
343+
"""
344+
345+
let (parameters, remaining) = extractParametersDocumentation(from: comment)
346+
347+
XCTAssertEqual(parameters, ["foo: bar :) ": "hello"])
348+
XCTAssertTrue(remaining.isEmpty)
349+
}
327350
}

0 commit comments

Comments
 (0)