Skip to content

Commit d354c58

Browse files
authored
fix: show all enums on hover (#942)
1 parent ed03cbf commit d354c58

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

src/languageservice/services/yamlHover.ts

+44-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { setKubernetesParserOption } from '../parser/isKubernetes';
1212
import { TextDocument } from 'vscode-languageserver-textdocument';
1313
import { yamlDocumentsCache } from '../parser/yaml-documents';
1414
import { SingleYAMLDocument } from '../parser/yamlParser07';
15-
import { getNodeValue, IApplicableSchema } from '../parser/jsonParser07';
15+
import { IApplicableSchema } from '../parser/jsonParser07';
1616
import { JSONSchema } from '../jsonSchema';
1717
import { URI } from 'vscode-uri';
1818
import * as path from 'path';
@@ -113,27 +113,31 @@ export class YAMLHover {
113113

114114
let title: string | undefined = undefined;
115115
let markdownDescription: string | undefined = undefined;
116-
let markdownEnumValueDescription: string | undefined = undefined;
117-
let enumValue: string | undefined = undefined;
116+
let markdownEnumDescriptions: string[] = [];
118117
const markdownExamples: string[] = [];
118+
const markdownEnums: markdownEnum[] = [];
119119

120120
matchingSchemas.every((s) => {
121121
if ((s.node === node || (node.type === 'property' && node.valueNode === s.node)) && !s.inverted && s.schema) {
122122
title = title || s.schema.title || s.schema.closestTitle;
123123
markdownDescription = markdownDescription || s.schema.markdownDescription || toMarkdown(s.schema.description);
124124
if (s.schema.enum) {
125-
const idx = s.schema.enum.indexOf(getNodeValue(node));
126125
if (s.schema.markdownEnumDescriptions) {
127-
markdownEnumValueDescription = s.schema.markdownEnumDescriptions[idx];
126+
markdownEnumDescriptions = s.schema.markdownEnumDescriptions;
128127
} else if (s.schema.enumDescriptions) {
129-
markdownEnumValueDescription = toMarkdown(s.schema.enumDescriptions[idx]);
128+
markdownEnumDescriptions = s.schema.enumDescriptions.map(toMarkdown);
129+
} else {
130+
markdownEnumDescriptions = [];
130131
}
131-
if (markdownEnumValueDescription) {
132-
enumValue = s.schema.enum[idx];
132+
s.schema.enum.forEach((enumValue, idx) => {
133133
if (typeof enumValue !== 'string') {
134134
enumValue = JSON.stringify(enumValue);
135135
}
136-
}
136+
markdownEnums.push({
137+
value: enumValue,
138+
description: markdownEnumDescriptions[idx],
139+
});
140+
});
137141
}
138142
if (s.schema.anyOf && isAllSchemasMatched(node, matchingSchemas, s.schema)) {
139143
//if append title and description of all matched schemas on hover
@@ -163,28 +167,30 @@ export class YAMLHover {
163167
result = '#### ' + toMarkdown(title);
164168
}
165169
if (markdownDescription) {
166-
if (result.length > 0) {
167-
result += '\n\n';
168-
}
170+
result = ensureLineBreak(result);
169171
result += markdownDescription;
170172
}
171-
if (markdownEnumValueDescription) {
172-
if (result.length > 0) {
173-
result += '\n\n';
174-
}
175-
result += `\`${toMarkdownCodeBlock(enumValue)}\`: ${markdownEnumValueDescription}`;
173+
if (markdownEnums.length !== 0) {
174+
result = ensureLineBreak(result);
175+
result += 'Allowed Values:\n\n';
176+
markdownEnums.forEach((me) => {
177+
if (me.description) {
178+
result += `* \`${toMarkdownCodeBlock(me.value)}\`: ${me.description}\n`;
179+
} else {
180+
result += `* \`${toMarkdownCodeBlock(me.value)}\`\n`;
181+
}
182+
});
176183
}
177184
if (markdownExamples.length !== 0) {
178-
if (result.length > 0) {
179-
result += '\n\n';
180-
}
181-
result += 'Examples:';
185+
result = ensureLineBreak(result);
186+
result += 'Examples:\n\n';
182187
markdownExamples.forEach((example) => {
183-
result += `\n\n\`\`\`${example}\`\`\``;
188+
result += `* \`\`\`${example}\`\`\`\n`;
184189
});
185190
}
186191
if (result.length > 0 && schema.schema.url) {
187-
result += `\n\nSource: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
192+
result = ensureLineBreak(result);
193+
result += `Source: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
188194
}
189195
return createHover(result);
190196
}
@@ -193,6 +199,21 @@ export class YAMLHover {
193199
}
194200
}
195201

202+
interface markdownEnum {
203+
value: string;
204+
description: string;
205+
}
206+
207+
function ensureLineBreak(content: string): string {
208+
if (content.length === 0) {
209+
return content;
210+
}
211+
if (!content.endsWith('\n')) {
212+
content += '\n';
213+
}
214+
return content + '\n';
215+
}
216+
196217
function getSchemaName(schema: JSONSchema): string {
197218
let result = 'JSON Schema';
198219
const urlString = schema.url;

test/hover.test.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,37 @@ users:
556556
);
557557
});
558558

559+
it('Hover displays enum descriptions if present', async () => {
560+
schemaProvider.addSchema(SCHEMA_ID, {
561+
type: 'object',
562+
properties: {
563+
animal: {
564+
type: 'string',
565+
description: 'should return this description',
566+
enum: ['cat', 'dog', 'non'],
567+
enumDescriptions: ['', 'Canis familiaris'],
568+
},
569+
},
570+
});
571+
const content = 'animal:\n ca|t|'; // len: 13, pos: 12
572+
const result = await parseSetup(content);
573+
574+
assert.strictEqual(MarkupContent.is(result.contents), true);
575+
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
576+
assert.strictEqual(
577+
(result.contents as MarkupContent).value,
578+
`should return this description
579+
580+
Allowed Values:
581+
582+
* \`cat\`
583+
* \`dog\`: Canis familiaris
584+
* \`non\`
585+
586+
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
587+
);
588+
});
589+
559590
it('Hover works on examples', async () => {
560591
schemaProvider.addSchema(SCHEMA_ID, {
561592
type: 'object',
@@ -577,11 +608,15 @@ users:
577608
(result.contents as MarkupContent).value,
578609
`should return this description
579610
580-
Examples:
611+
Allowed Values:
612+
613+
* \`cat\`
614+
* \`dog\`
581615
582-
\`\`\`"cat"\`\`\`
616+
Examples:
583617
584-
\`\`\`"dog"\`\`\`
618+
* \`\`\`"cat"\`\`\`
619+
* \`\`\`"dog"\`\`\`
585620
586621
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
587622
);

0 commit comments

Comments
 (0)