Skip to content

Commit

Permalink
Add: show examples on hover.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Feb 14, 2022
1 parent 2f34acf commit 80d4558
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ export class YAMLHover {

let title: string | undefined = undefined;
let markdownDescription: string | undefined = undefined;
let markdownEnumValueDescription: string | undefined = undefined,
enumValue: string | undefined = undefined;
let markdownEnumValueDescription: string | undefined = undefined;
let enumValue: string | undefined = undefined;
const markdownExamples: string[] = [];

matchingSchemas.every((s) => {
if (s.node === node && !s.inverted && s.schema) {
title = title || s.schema.title;
Expand All @@ -122,6 +124,11 @@ export class YAMLHover {
}
}
}
if (s.schema.examples) {
s.schema.examples.forEach((example) => {
markdownExamples.push(JSON.stringify(example));
});
}
}
return true;
});
Expand All @@ -141,7 +148,16 @@ export class YAMLHover {
}
result += `\`${toMarkdownCodeBlock(enumValue)}\`: ${markdownEnumValueDescription}`;
}

if (markdownExamples.length !== 0) {
if (result.length > 0) {
result += '\n\n';
}
result += 'Examples: [\n\n';
markdownExamples.forEach((example) => {
result += `\`\`\`${example}\`\`\`\n\n`;
});
result += ']';
}
if (result.length > 0 && schema.schema.url) {
result += `\n\nSource: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
}
Expand Down
33 changes: 33 additions & 0 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,39 @@ storage:
);
});

it('Hover works on examples', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
animal: {
type: 'string',
description: 'should return this description',
enum: ['cat', 'dog'],
examples: ['cat', 'dog'],
},
},
});
const content = 'animal:\n cat';
const result = await parseSetup(content, 12);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
assert.strictEqual(
(result.contents as MarkupContent).value,
`should return this description
Examples: [
\`\`\`"cat"\`\`\`
\`\`\`"dog"\`\`\`
]
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});

it('Hover on property next value on null', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down

0 comments on commit 80d4558

Please sign in to comment.