Skip to content

Commit 5756ba1

Browse files
authored
Properly format @example in quick info (#2329)
1 parent b89019d commit 5756ba1

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

internal/ls/hover.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/microsoft/typescript-go/internal/collections"
1313
"github.com/microsoft/typescript-go/internal/core"
1414
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
15+
"github.com/microsoft/typescript-go/internal/scanner"
1516
)
1617

1718
const (
@@ -109,14 +110,19 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de
109110
}
110111
}
111112
comments := tag.Comments()
112-
if len(comments) != 0 {
113-
if commentHasPrefix(comments, "```") {
113+
if tag.Kind == ast.KindJSDocTag && tag.TagName().Text() == "example" {
114+
b.WriteString("\n")
115+
commentText := strings.TrimRight(getCommentText(comments), " \t\r\n")
116+
if len(commentText) > 6 && strings.HasPrefix(commentText, "```") && strings.HasSuffix(commentText, "```") && strings.Contains(commentText, "\n") {
117+
b.WriteString(commentText)
114118
b.WriteString("\n")
115119
} else {
116-
b.WriteString(" ")
117-
if !commentHasPrefix(comments, "-") {
118-
b.WriteString("— ")
119-
}
120+
writeCode(&b, "tsx", commentText)
121+
}
122+
} else if len(comments) != 0 {
123+
b.WriteString(" ")
124+
if !commentHasPrefix(comments, "-") {
125+
b.WriteString("— ")
120126
}
121127
l.writeComments(&b, c, comments, isMarkdown)
122128
}
@@ -127,6 +133,19 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de
127133
return b.String()
128134
}
129135

136+
func getCommentText(comments []*ast.Node) string {
137+
var b strings.Builder
138+
for _, comment := range comments {
139+
switch comment.Kind {
140+
case ast.KindJSDocText:
141+
b.WriteString(comment.Text())
142+
case ast.KindJSDocLink, ast.KindJSDocLinkCode, ast.KindJSDocLinkPlain:
143+
b.WriteString(scanner.GetTextOfNode(comment))
144+
}
145+
}
146+
return b.String()
147+
}
148+
130149
func formatQuickInfo(quickInfo string) string {
131150
var b strings.Builder
132151
b.Grow(32)

testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocCodefence.baseline

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
// | ```
3737
// |
3838
// |
39-
// | *@example* — ``
39+
// | *@example*
40+
// | ```tsx
41+
// | ``
4042
// | 1 + 2
4143
// | `
44+
// | ```
4245
// |
4346
// | ----------------------------------------------------------------------
4447
// return '2';
@@ -84,7 +87,7 @@
8487
"item": {
8588
"contents": {
8689
"kind": "markdown",
87-
"value": "```tsx\nfunction boo(): string\n```\n\n\n*@example* — ``\n1 + 2\n`\n"
90+
"value": "```tsx\nfunction boo(): string\n```\n\n\n*@example*\n```tsx\n``\n1 + 2\n`\n```\n"
8891
},
8992
"range": {
9093
"start": {

testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocUnknownTag.baseline

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
// | ```
1515
// |
1616
// |
17-
// | *@example* — if (true) {
17+
// | *@example*
18+
// | ```tsx
19+
// | if (true) {
1820
// | foo()
1921
// | }
22+
// | ```
2023
// |
2124
// | ----------------------------------------------------------------------
2225
// return '2';
@@ -35,9 +38,12 @@
3538
// | ```
3639
// |
3740
// |
38-
// | *@example* — {
41+
// | *@example*
42+
// | ```tsx
43+
// | {
3944
// | foo()
4045
// | }
46+
// | ```
4147
// |
4248
// | ----------------------------------------------------------------------
4349
// return '2';
@@ -56,9 +62,12 @@
5662
// | ```
5763
// |
5864
// |
59-
// | *@example* — x y
65+
// | *@example*
66+
// | ```tsx
67+
// | x y
6068
// | 12345
6169
// | b
70+
// | ```
6271
// |
6372
// | ----------------------------------------------------------------------
6473
// return '2';
@@ -80,9 +89,12 @@
8089
// |
8190
// | *@func*
8291
// |
83-
// | *@example* — x y
92+
// | *@example*
93+
// | ```tsx
94+
// | x y
8495
// | 12345
8596
// | b
97+
// | ```
8698
// |
8799
// | ----------------------------------------------------------------------
88100
// return '2';
@@ -103,9 +115,12 @@
103115
// |
104116
// | *@func*
105117
// |
106-
// | *@example* — x y
118+
// | *@example*
119+
// | ```tsx
120+
// | x y
107121
// | 12345
108122
// | b
123+
// | ```
109124
// |
110125
// | ----------------------------------------------------------------------
111126
// return '2';
@@ -124,7 +139,7 @@
124139
"item": {
125140
"contents": {
126141
"kind": "markdown",
127-
"value": "```tsx\nfunction foo(): string\n```\n\n\n*@example* — if (true) {\n foo()\n}\n"
142+
"value": "```tsx\nfunction foo(): string\n```\n\n\n*@example*\n```tsx\nif (true) {\n foo()\n}\n```\n"
128143
},
129144
"range": {
130145
"start": {
@@ -151,7 +166,7 @@
151166
"item": {
152167
"contents": {
153168
"kind": "markdown",
154-
"value": "```tsx\nfunction foo2(): string\n```\n\n\n*@example*{\n foo()\n}\n"
169+
"value": "```tsx\nfunction foo2(): string\n```\n\n\n*@example*\n```tsx\n{\n foo()\n}\n```\n"
155170
},
156171
"range": {
157172
"start": {
@@ -178,7 +193,7 @@
178193
"item": {
179194
"contents": {
180195
"kind": "markdown",
181-
"value": "```tsx\nfunction moo(): string\n```\n\n\n*@example*x y\n 12345\n b\n"
196+
"value": "```tsx\nfunction moo(): string\n```\n\n\n*@example*\n```tsx\n x y\n 12345\n b\n```\n"
182197
},
183198
"range": {
184199
"start": {
@@ -205,7 +220,7 @@
205220
"item": {
206221
"contents": {
207222
"kind": "markdown",
208-
"value": "```tsx\nfunction boo(): string\n```\n\n\n*@func*\n\n*@example*x y\n 12345\n b\n"
223+
"value": "```tsx\nfunction boo(): string\n```\n\n\n*@func*\n\n*@example*\n```tsx\n x y\n 12345\n b\n```\n"
209224
},
210225
"range": {
211226
"start": {
@@ -232,7 +247,7 @@
232247
"item": {
233248
"contents": {
234249
"kind": "markdown",
235-
"value": "```tsx\nfunction goo(): string\n```\n\n\n*@func*\n\n*@example* — x y\n12345\n b\n"
250+
"value": "```tsx\nfunction goo(): string\n```\n\n\n*@func*\n\n*@example*\n```tsx\nx y\n12345\n b\n```\n"
236251
},
237252
"range": {
238253
"start": {

0 commit comments

Comments
 (0)