Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/services/jsonHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -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}\``;
}
26 changes: 26 additions & 0 deletions src/test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down