diff --git a/internal/ls/hover.go b/internal/ls/hover.go index 6c8c644adc..17b11bd896 100644 --- a/internal/ls/hover.go +++ b/internal/ls/hover.go @@ -12,6 +12,7 @@ import ( "github.com/microsoft/typescript-go/internal/collections" "github.com/microsoft/typescript-go/internal/core" "github.com/microsoft/typescript-go/internal/lsp/lsproto" + "github.com/microsoft/typescript-go/internal/scanner" ) const ( @@ -109,14 +110,19 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de } } comments := tag.Comments() - if len(comments) != 0 { - if commentHasPrefix(comments, "```") { + if tag.Kind == ast.KindJSDocTag && tag.TagName().Text() == "example" { + b.WriteString("\n") + commentText := strings.TrimRight(getCommentText(comments), " \t\r\n") + if len(commentText) > 6 && strings.HasPrefix(commentText, "```") && strings.HasSuffix(commentText, "```") && strings.Contains(commentText, "\n") { + b.WriteString(commentText) b.WriteString("\n") } else { - b.WriteString(" ") - if !commentHasPrefix(comments, "-") { - b.WriteString("— ") - } + writeCode(&b, "tsx", commentText) + } + } else if len(comments) != 0 { + b.WriteString(" ") + if !commentHasPrefix(comments, "-") { + b.WriteString("— ") } l.writeComments(&b, c, comments, isMarkdown) } @@ -127,6 +133,19 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de return b.String() } +func getCommentText(comments []*ast.Node) string { + var b strings.Builder + for _, comment := range comments { + switch comment.Kind { + case ast.KindJSDocText: + b.WriteString(comment.Text()) + case ast.KindJSDocLink, ast.KindJSDocLinkCode, ast.KindJSDocLinkPlain: + b.WriteString(scanner.GetTextOfNode(comment)) + } + } + return b.String() +} + func formatQuickInfo(quickInfo string) string { var b strings.Builder b.Grow(32) diff --git a/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocCodefence.baseline b/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocCodefence.baseline index 63b64218fb..13ed7f8f58 100644 --- a/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocCodefence.baseline +++ b/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocCodefence.baseline @@ -36,9 +36,12 @@ // | ``` // | // | -// | *@example* — `` +// | *@example* +// | ```tsx +// | `` // | 1 + 2 // | ` +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -84,7 +87,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction boo(): string\n```\n\n\n*@example* — ``\n1 + 2\n`\n" + "value": "```tsx\nfunction boo(): string\n```\n\n\n*@example*\n```tsx\n``\n1 + 2\n`\n```\n" }, "range": { "start": { diff --git a/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocUnknownTag.baseline b/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocUnknownTag.baseline index 1ace37d779..aa3b34277f 100644 --- a/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocUnknownTag.baseline +++ b/testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocUnknownTag.baseline @@ -14,9 +14,12 @@ // | ``` // | // | -// | *@example* — if (true) { +// | *@example* +// | ```tsx +// | if (true) { // | foo() // | } +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -35,9 +38,12 @@ // | ``` // | // | -// | *@example* — { +// | *@example* +// | ```tsx +// | { // | foo() // | } +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -56,9 +62,12 @@ // | ``` // | // | -// | *@example* — x y +// | *@example* +// | ```tsx +// | x y // | 12345 // | b +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -80,9 +89,12 @@ // | // | *@func* // | -// | *@example* — x y +// | *@example* +// | ```tsx +// | x y // | 12345 // | b +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -103,9 +115,12 @@ // | // | *@func* // | -// | *@example* — x y +// | *@example* +// | ```tsx +// | x y // | 12345 // | b +// | ``` // | // | ---------------------------------------------------------------------- // return '2'; @@ -124,7 +139,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction foo(): string\n```\n\n\n*@example* — if (true) {\n foo()\n}\n" + "value": "```tsx\nfunction foo(): string\n```\n\n\n*@example*\n```tsx\nif (true) {\n foo()\n}\n```\n" }, "range": { "start": { @@ -151,7 +166,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction foo2(): string\n```\n\n\n*@example* — {\n foo()\n}\n" + "value": "```tsx\nfunction foo2(): string\n```\n\n\n*@example*\n```tsx\n{\n foo()\n}\n```\n" }, "range": { "start": { @@ -178,7 +193,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction moo(): string\n```\n\n\n*@example* — x y\n 12345\n b\n" + "value": "```tsx\nfunction moo(): string\n```\n\n\n*@example*\n```tsx\n x y\n 12345\n b\n```\n" }, "range": { "start": { @@ -205,7 +220,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction boo(): string\n```\n\n\n*@func*\n\n*@example* — x y\n 12345\n b\n" + "value": "```tsx\nfunction boo(): string\n```\n\n\n*@func*\n\n*@example*\n```tsx\n x y\n 12345\n b\n```\n" }, "range": { "start": { @@ -232,7 +247,7 @@ "item": { "contents": { "kind": "markdown", - "value": "```tsx\nfunction goo(): string\n```\n\n\n*@func*\n\n*@example* — x y\n12345\n b\n" + "value": "```tsx\nfunction goo(): string\n```\n\n\n*@func*\n\n*@example*\n```tsx\nx y\n12345\n b\n```\n" }, "range": { "start": {