Skip to content

Commit 9d56a93

Browse files
committed
Show full documentation without declaration in completion
1 parent d3ed8b1 commit 9d56a93

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

Sources/SourceKitLSP/Swift/CodeCompletionSession.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ class CodeCompletionSession {
587587
private static func documentationString(from response: SKDResponseDictionary, sourcekitd: SourceKitD) -> String? {
588588
if let docFullAsXML: String = response[sourcekitd.keys.docFullAsXML] {
589589
return orLog("Converting XML documentation to markdown") {
590-
try xmlDocumentationToMarkdown(docFullAsXML)
590+
try xmlDocumentationToMarkdown(docFullAsXML, includeDeclaration: false)
591591
}
592592
}
593593

Sources/SourceKitLSP/Swift/CommentXML.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ enum CommentXMLError: Error {
2424
/// Converts from sourcekit's XML documentation format to Markdown.
2525
///
2626
/// This code should go away and sourcekitd should return the Markdown directly.
27-
package func xmlDocumentationToMarkdown(_ xmlString: String) throws -> String {
27+
///
28+
/// - Parameters:
29+
/// - xmlString: The XML string to convert.
30+
/// - includeDeclaration: If true, the declaration will be included at the top of the output markdown.
31+
package func xmlDocumentationToMarkdown(_ xmlString: String, includeDeclaration: Bool = true) throws -> String {
2832
let xml = try XMLDocument(xmlString: xmlString)
2933
guard let root = xml.rootElement() else {
3034
throw CommentXMLError.noRootElement
3135
}
3236

33-
var convert = XMLToMarkdown()
37+
var convert = XMLToMarkdown(includeDeclaration: includeDeclaration)
3438
convert.out.reserveCapacity(xmlString.utf16.count)
3539
convert.toMarkdown(root)
3640
return convert.out
@@ -42,6 +46,11 @@ private struct XMLToMarkdown {
4246
let indentWidth: Int = 4
4347
var lineNumber: Int = 0
4448
var inParam: Bool = false
49+
let includeDeclaration: Bool
50+
51+
init(includeDeclaration: Bool) {
52+
self.includeDeclaration = includeDeclaration
53+
}
4554

4655
mutating func newlineIfNeeded(count: Int = 1) {
4756
if !out.isEmpty && out.last! != "\n" {
@@ -74,10 +83,12 @@ private struct XMLToMarkdown {
7483
mutating func toMarkdown(_ node: XMLElement) {
7584
switch node.name {
7685
case "Declaration":
77-
newlineIfNeeded(count: 2)
78-
out += "```swift\n"
79-
toMarkdown(node.children)
80-
out += "\n```\n"
86+
if includeDeclaration {
87+
newlineIfNeeded(count: 2)
88+
out += "```swift\n"
89+
toMarkdown(node.children)
90+
out += "\n```\n"
91+
}
8192

8293
case "Name", "USR", "Direction":
8394
break

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,32 @@ final class LocalSwiftTests: XCTestCase {
11491149
)
11501150
}
11511151

1152+
func testXMLToMarkdownCommentOmitDeclaration() {
1153+
XCTAssertEqual(
1154+
try xmlDocumentationToMarkdown(
1155+
"""
1156+
<Function file="Pi.swift" line="3" column="14">\
1157+
<Declaration>func pi()</Declaration>\
1158+
<CommentParts>\
1159+
<Abstract><Para>Computes π with infinite precision.</Para></Abstract>\
1160+
<Discussion>\
1161+
<Para>This function doesn’t terminate.</Para>\
1162+
</Discussion>\
1163+
</CommentParts>\
1164+
</Function>
1165+
""",
1166+
includeDeclaration: false
1167+
),
1168+
"""
1169+
Computes π with infinite precision.
1170+
1171+
### Discussion
1172+
1173+
This function doesn’t terminate.
1174+
"""
1175+
)
1176+
}
1177+
11521178
func testSymbolInfo() async throws {
11531179
let testClient = try await TestSourceKitLSPClient()
11541180
let url = URL(fileURLWithPath: "/\(UUID())/a.swift")

Tests/SourceKitLSPTests/SwiftCompletionTests.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ final class SwiftCompletionTests: XCTestCase {
4040
"""
4141
struct S {
4242
/// Documentation for `abc`.
43+
///
44+
/// - Note: This is a note.
4345
var abc: Int
4446
4547
func test(a: Int) {
@@ -71,10 +73,11 @@ final class SwiftCompletionTests: XCTestCase {
7173
assertMarkdown(
7274
documentation: abc.documentation,
7375
expected: """
74-
```swift
75-
var abc: Int
76-
```
7776
Documentation for `abc`.
77+
78+
### Discussion
79+
80+
This is a note.
7881
"""
7982
)
8083
XCTAssertEqual(abc.filterText, "abc")
@@ -99,10 +102,11 @@ final class SwiftCompletionTests: XCTestCase {
99102
assertMarkdown(
100103
documentation: abc.documentation,
101104
expected: """
102-
```swift
103-
var abc: Int
104-
```
105105
Documentation for `abc`.
106+
107+
### Discussion
108+
109+
This is a note.
106110
"""
107111
)
108112
XCTAssertEqual(abc.filterText, "abc")
@@ -126,6 +130,8 @@ final class SwiftCompletionTests: XCTestCase {
126130
"""
127131
struct S {
128132
/// Documentation for `abc`.
133+
///
134+
/// - Note: This is a note.
129135
var abc: Int
130136
131137
func test(a: Int) {
@@ -182,6 +188,8 @@ final class SwiftCompletionTests: XCTestCase {
182188
"""
183189
struct S {
184190
/// Documentation for `abc`.
191+
///
192+
/// - Note: This is a note.
185193
var abc: Int
186194
187195
func test(a: Int) {
@@ -1207,12 +1215,7 @@ final class SwiftCompletionTests: XCTestCase {
12071215
let resolvedItem = try await testClient.send(CompletionItemResolveRequest(item: item))
12081216
assertMarkdown(
12091217
documentation: resolvedItem.documentation,
1210-
expected: """
1211-
```swift
1212-
func makeBool() -> Bool
1213-
```
1214-
Creates a true value
1215-
"""
1218+
expected: "Creates a true value"
12161219
)
12171220
}
12181221

@@ -1310,7 +1313,7 @@ private func assertMarkdown(
13101313
file: StaticString = #filePath,
13111314
line: UInt = #line
13121315
) {
1313-
XCTAssertEqual(documentation, .markupContent(MarkupContent(kind: .markdown, value: expected)))
1316+
XCTAssertEqual(documentation, .markupContent(MarkupContent(kind: .markdown, value: expected)), file: file, line: line)
13141317
}
13151318

13161319
fileprivate extension Position {

0 commit comments

Comments
 (0)