From b8430bd4bbade54a6f566c95e1253d7fdccc8773 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Thu, 13 Jan 2022 01:13:29 +0800 Subject: [PATCH] Check documentation as SymbolDisplayPart[] Closes #338 --- src/server/features/completionItemProvider.ts | 6 +++--- src/server/features/hover.ts | 14 ++++++++------ src/server/features/signatureHelp.ts | 8 ++++---- src/server/utils/previewer.ts | 15 ++++++++------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/server/features/completionItemProvider.ts b/src/server/features/completionItemProvider.ts index 25733cf..8280ee2 100644 --- a/src/server/features/completionItemProvider.ts +++ b/src/server/features/completionItemProvider.ts @@ -251,7 +251,7 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP } const detail = details[0] if (!item.detail && detail.displayParts.length) { - item.detail = Previewer.plain(detail.displayParts) + item.detail = Previewer.plainWithLinks(detail.displayParts) } item.documentation = this.getDocumentation(detail) const { command, additionalTextEdits } = this.getCodeActions(detail, filepath) @@ -354,12 +354,12 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP private getDocumentation(detail: Proto.CompletionEntryDetails): MarkupContent | undefined { let documentation = '' if (detail.source) { - const importPath = `'${Previewer.plain(detail.source)}'` + const importPath = `'${Previewer.plainWithLinks(detail.source)}'` const autoImportLabel = `Auto import from ${importPath}` documentation += `${autoImportLabel}\n` } let parts = [ - Previewer.plain(detail.documentation), + Previewer.plainWithLinks(detail.documentation), Previewer.tagsMarkdownPreview(detail.tags) ] parts = parts.filter(s => s && s.trim() != '') diff --git a/src/server/features/hover.ts b/src/server/features/hover.ts index a9d3e26..c669f44 100644 --- a/src/server/features/hover.ts +++ b/src/server/features/hover.ts @@ -7,7 +7,7 @@ import { HoverProvider } from 'coc.nvim' import { CancellationToken, Hover, MarkedString, Position } from 'vscode-languageserver-protocol' import * as Proto from '../protocol' import { ITypeScriptServiceClient } from '../typescriptService' -import { tagsMarkdownPreview } from '../utils/previewer' +import { markdownDocumentation } from '../utils/previewer' import * as typeConverters from '../utils/typeConverters' export default class TypeScriptHoverProvider implements HoverProvider { @@ -42,14 +42,16 @@ export default class TypeScriptHoverProvider implements HoverProvider { } private static getContents(data: Proto.QuickInfoResponseBody): MarkedString[] { // tslint:disable-line - const parts = [] - + const parts: MarkedString[] = [] if (data.displayString) { + // const displayParts: string[] = [] parts.push({ language: 'typescript', value: data.displayString }) } - - const tags = tagsMarkdownPreview(data.tags) - parts.push(data.documentation + (tags ? '\n\n' + tags : '')) + const markup = markdownDocumentation(data.documentation, data.tags) + parts.push({ + language: 'markdown', + value: markup.value + }) return parts } } diff --git a/src/server/features/signatureHelp.ts b/src/server/features/signatureHelp.ts index c0a48d0..2a74a89 100644 --- a/src/server/features/signatureHelp.ts +++ b/src/server/features/signatureHelp.ts @@ -60,13 +60,13 @@ export default class TypeScriptSignatureHelpProvider implements SignatureHelpPro private convertSignature(item: Proto.SignatureHelpItem): SignatureInformation { let parameters = item.parameters.map(p => { return { - label: Previewer.plain(p.displayParts), + label: Previewer.plainWithLinks(p.displayParts), documentation: Previewer.markdownDocumentation(p.documentation, []) } }) - let label = Previewer.plain(item.prefixDisplayParts) - label += parameters.map(parameter => parameter.label).join(Previewer.plain(item.separatorDisplayParts)) - label += Previewer.plain(item.suffixDisplayParts) + let label = Previewer.plainWithLinks(item.prefixDisplayParts) + label += parameters.map(parameter => parameter.label).join(Previewer.plainWithLinks(item.separatorDisplayParts)) + label += Previewer.plainWithLinks(item.suffixDisplayParts) return { label, documentation: Previewer.markdownDocumentation( diff --git a/src/server/utils/previewer.ts b/src/server/utils/previewer.ts index ccae4f5..9ea0351 100644 --- a/src/server/utils/previewer.ts +++ b/src/server/utils/previewer.ts @@ -94,20 +94,15 @@ function getTagDocumentation(tag: Proto.JSDocTagInfo): string | undefined { return label + (text.match(/\r\n|\n/g) ? ' \n' + text : ` — ${text}`) } -export function plain(parts: Proto.SymbolDisplayPart[]): string { - if (!parts || !parts.length) return '' - return parts.map(part => part.text).join('') -} - export function tagsMarkdownPreview(tags: Proto.JSDocTagInfo[]): string { return (tags || []).map(getTagDocumentation).join(' \n\n') } export function markdownDocumentation( - documentation: Proto.SymbolDisplayPart[], + documentation: Proto.SymbolDisplayPart[] | string, tags: Proto.JSDocTagInfo[] ): MarkupContent { - let out = plain(documentation) + let out = plainWithLinks(documentation) const tagsPreview = tagsMarkdownPreview(tags) if (tagsPreview) { out = out + ('\n\n' + tagsPreview) @@ -118,6 +113,12 @@ export function markdownDocumentation( } } +export function plainWithLinks( + parts: readonly Proto.SymbolDisplayPart[] | string, +): string { + return processInlineTags(convertLinkTags(parts)) +} + /** * Convert `@link` inline tags to markdown links */