Skip to content

Commit be127ca

Browse files
committed
Make hover popover prettier
rdar://126488098
1 parent 0de726d commit be127ca

File tree

3 files changed

+11
-32
lines changed

3 files changed

+11
-32
lines changed

Sources/SourceKitLSP/Swift/CommentXML.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private struct XMLToMarkdown {
7777
newlineIfNeeded(count: 2)
7878
out += "```swift\n"
7979
toMarkdown(node.children)
80-
out += "\n```\n\n---\n"
80+
out += "\n```\n"
8181

8282
case "Name", "USR", "Direction":
8383
break

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -569,46 +569,35 @@ extension SwiftLanguageService {
569569
let cursorInfoResults = try await cursorInfo(uri, position..<position).cursorInfo
570570

571571
let symbolDocumentations = cursorInfoResults.compactMap { (cursorInfo) -> String? in
572-
guard let name: String = cursorInfo.symbolInfo.name else {
573-
// There is a cursor but we don't know how to deal with it.
574-
return nil
575-
}
576-
577-
/// Prepend backslash to `*` and `_`, to prevent them
578-
/// from being interpreted as markdown.
579-
func escapeNameMarkdown(_ str: String) -> String {
580-
return String(str.flatMap({ ($0 == "*" || $0 == "_") ? ["\\", $0] : [$0] }))
581-
}
582-
583-
var result = escapeNameMarkdown(name)
584572
if let documentation = cursorInfo.documentation {
573+
var result = ""
585574
if let annotatedDeclaration = cursorInfo.annotatedDeclaration {
586575
let markdownDecl =
587576
orLog("Convert XML declaration to Markdown") {
588577
try xmlDocumentationToMarkdown(annotatedDeclaration)
589578
} ?? annotatedDeclaration
590-
result += "\n\(markdownDecl)"
579+
result += "\(markdownDecl)\n---\n"
591580
}
592581
result += documentation
582+
return result
593583
} else if let doc = cursorInfo.documentationXML {
594-
result += """
595-
584+
return """
596585
\(orLog("Convert XML to Markdown") { try xmlDocumentationToMarkdown(doc) } ?? doc)
597586
"""
598587
} else if let annotated: String = cursorInfo.annotatedDeclaration {
599-
result += """
600-
588+
return """
601589
\(orLog("Convert XML to Markdown") { try xmlDocumentationToMarkdown(annotated) } ?? annotated)
602590
"""
591+
} else {
592+
return nil
603593
}
604-
return result
605594
}
606595

607596
if symbolDocumentations.isEmpty {
608597
return nil
609598
}
610599

611-
let joinedDocumentation = symbolDocumentations.joined(separator: "\n# Alternative result\n")
600+
let joinedDocumentation = symbolDocumentations.joined(separator: "\n### Alternative result\n")
612601

613602
return HoverResponse(
614603
contents: .markupContent(MarkupContent(kind: .markdown, value: joinedDocumentation)),
@@ -851,7 +840,7 @@ extension SwiftLanguageService {
851840
// Instead of returning an error, return empty results.
852841
logger.error(
853842
"""
854-
Loading diagnostic failed with the following error. Returning empty diagnostics.
843+
Loading diagnostic failed with the following error. Returning empty diagnostics.
855844
\(error.forLogging)
856845
"""
857846
)

Tests/SourceKitLSPTests/HoverTests.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ final class HoverTests: XCTestCase {
2626
struct 1️⃣S {}
2727
""",
2828
expectedContent: """
29-
S
3029
```swift
3130
struct S
3231
```
@@ -70,21 +69,15 @@ final class HoverTests: XCTestCase {
7069
_ = 1️⃣Foo()
7170
""",
7271
expectedContent: """
73-
Foo
7472
```swift
7573
struct Foo
7674
```
7775
78-
---
79-
80-
# Alternative result
81-
init()
76+
### Alternative result
8277
```swift
8378
init()
8479
```
8580
86-
---
87-
8881
"""
8982
)
9083
}
@@ -96,7 +89,6 @@ final class HoverTests: XCTestCase {
9689
func 1️⃣test(_ a: Int, _ b: Int) { }
9790
""",
9891
expectedContent: ##"""
99-
test(\_:\_:)
10092
```swift
10193
func test(_ a: Int, _ b: Int)
10294
```
@@ -114,7 +106,6 @@ final class HoverTests: XCTestCase {
114106
func 1️⃣*%*(lhs: String, rhs: String) { }
115107
""",
116108
expectedContent: ##"""
117-
\*%\*(\_:\_:)
118109
```swift
119110
func *%* (lhs: String, rhs: String)
120111
```
@@ -135,7 +126,6 @@ final class HoverTests: XCTestCase {
135126
func 1️⃣eatApple() {}
136127
""",
137128
expectedContent: """
138-
eatApple()
139129
```swift
140130
func eatApple()
141131
```

0 commit comments

Comments
 (0)