diff --git a/src/services/jsonHover.ts b/src/services/jsonHover.ts index 8a7f7a8..13fbb4a 100644 --- a/src/services/jsonHover.ts +++ b/src/services/jsonHover.ts @@ -102,7 +102,7 @@ export class JSONHover { if (result.length > 0) { result += "\n\n"; } - result += `\`${toMarkdownCodeBlock(enumValue!)}\`: ${markdownEnumValueDescription}`; + result += `${toMarkdownCodeSpan(enumValue!)}: ${markdownEnumValueDescription}`; } return createHover([result]); }); @@ -124,10 +124,12 @@ function toMarkdown(plain: string | undefined): string | undefined { return undefined; } -function toMarkdownCodeBlock(content: string) { - // see https://daringfireball.net/projects/markdown/syntax#precode - if (content.indexOf('`') !== -1) { - return '`` ' + content + ' ``'; +function toMarkdownCodeSpan(content: string) { + const backtickRuns = content.match(/`+/g); + if (backtickRuns) { + const fenceLength = backtickRuns.reduce((length, run) => Math.max(length, run.length + 1), 3); + const fence = '`'.repeat(fenceLength); + return `${fence} ${content} ${fence}`; } - return content; + return `\`${content}\``; } diff --git a/src/test/hover.test.ts b/src/test/hover.test.ts index ebe3ebd..6417c4c 100644 --- a/src/test/hover.test.ts +++ b/src/test/hover.test.ts @@ -164,6 +164,32 @@ suite('JSON Hover', () => { }); }); + for (const [value, fence] of [ + ['before```after', '````'], + ['```', '````'], + ['```before', '````'], + ['after```', '````'], + ['before````after', '`````'], + ['`````', '``````'], + ['` mixed ``` runs ``', '````'], + [' ``` ', '````'], + ['before`after', '```'], + ['before``after', '```'], + ]) { + test(`Enum code span with backtick value: ${JSON.stringify(value)}`, async function () { + const schema: JSONSchema = { + properties: { + prop: { enum: [value], enumDescriptions: ['A value'] } + } + }; + const content = JSON.stringify({ prop: value }); + for (const character of [2, 9]) { + const result = await testComputeInfo(content, schema, { line: 0, character }); + assert.deepStrictEqual(result.contents, [`${fence} ${value} ${fence}: A value`]); + } + }); + } + test('Multiline descriptions', async function () { const schema: JSONSchema = { type: 'object',