From fb319333b931bdd27fac2ec8ebb5e7537d9adf72 Mon Sep 17 00:00:00 2001 From: Pradeep Ramola Date: Mon, 23 Nov 2026 04:16:53 -0500 Subject: [PATCH] Preserve backtick runs in enum hover code spans --- src/services/jsonHover.ts | 14 ++++++++------ src/test/hover.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/services/jsonHover.ts b/src/services/jsonHover.ts index 8a7f7a84..13fbb4a6 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 ebe3ebde..6417c4c1 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',