Skip to content

Commit 3d6ea18

Browse files
fix/improve enum value descriptions for merged enum lists (#1085)
This is a follow-up submission for #1028, which removes duplicate enum values in merged enum lists. This fix/improvement ensures that the first **non-empty** enum description is used for the corresponding enum value. Before this fix: if the first non-unique enum value was not providing a description but the second one was, it was not picked up and the enum value description stayed empty. Co-authored-by: xxx <xxx> Co-authored-by: Muthurajan Sivasubramanian <[email protected]>
1 parent 17eec9d commit 3d6ea18

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

src/languageservice/services/yamlHover.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,15 @@ export class YAMLHover {
134134
enumValue = JSON.stringify(enumValue);
135135
}
136136
//insert only if the value is not present yet (avoiding duplicates)
137-
if (!markdownEnums.some((me) => me.value === enumValue)) {
137+
//but it also adds or keeps the description of the enum value
138+
const foundIdx = markdownEnums.findIndex((me) => me.value === enumValue);
139+
if (foundIdx < 0) {
138140
markdownEnums.push({
139141
value: enumValue,
140142
description: markdownEnumDescriptions[idx],
141143
});
144+
} else {
145+
markdownEnums[foundIdx].description ||= markdownEnumDescriptions[idx];
142146
}
143147
});
144148
}

test/hover.test.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,124 @@ Allowed Values:
623623
* \`bird\`
624624
* \`fish\`: Special fish
625625
626+
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
627+
);
628+
});
629+
630+
it('Hover displays unique enum values with prroper description (1st case)', async () => {
631+
schemaProvider.addSchema(SCHEMA_ID, {
632+
type: 'object',
633+
properties: {
634+
animal: {
635+
description: 'should return this description',
636+
anyOf: [
637+
{
638+
enum: ['cat', 'dog', 'fish'],
639+
enumDescriptions: ['1st cat', '1st dog', '1st fish'], // should use this description for "fish"
640+
},
641+
{
642+
enum: ['bird', 'fish', 'ant'],
643+
},
644+
],
645+
},
646+
},
647+
});
648+
const content = 'animal:\n fis|n|'; // len: 13, pos: 12
649+
const result = await parseSetup(content);
650+
651+
assert.strictEqual(MarkupContent.is(result.contents), true);
652+
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
653+
assert.strictEqual(
654+
(result.contents as MarkupContent).value,
655+
`should return this description
656+
657+
Allowed Values:
658+
659+
* \`ant\`
660+
* \`cat\`: 1st cat
661+
* \`dog\`: 1st dog
662+
* \`fish\`: 1st fish
663+
* \`bird\`
664+
665+
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
666+
);
667+
});
668+
669+
it('Hover displays unique enum values with prroper description (2nd case)', async () => {
670+
schemaProvider.addSchema(SCHEMA_ID, {
671+
type: 'object',
672+
properties: {
673+
animal: {
674+
description: 'should return this description',
675+
anyOf: [
676+
{
677+
enum: ['cat', 'dog', 'fish'],
678+
},
679+
{
680+
enum: ['bird', 'fish', 'ant'],
681+
enumDescriptions: ['2nd bird', '2nd fish', '2nd ant'], // should use this description for "fish"
682+
},
683+
],
684+
},
685+
},
686+
});
687+
const content = 'animal:\n fis|n|'; // len: 13, pos: 12
688+
const result = await parseSetup(content);
689+
690+
assert.strictEqual(MarkupContent.is(result.contents), true);
691+
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
692+
assert.strictEqual(
693+
(result.contents as MarkupContent).value,
694+
`should return this description
695+
696+
Allowed Values:
697+
698+
* \`ant\`: 2nd ant
699+
* \`cat\`
700+
* \`dog\`
701+
* \`fish\`: 2nd fish
702+
* \`bird\`: 2nd bird
703+
704+
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
705+
);
706+
});
707+
708+
it('Hover displays unique enum values with prroper description (3rd case)', async () => {
709+
schemaProvider.addSchema(SCHEMA_ID, {
710+
type: 'object',
711+
properties: {
712+
animal: {
713+
description: 'should return this description',
714+
anyOf: [
715+
{
716+
enum: ['cat', 'dog', 'fish'],
717+
enumDescriptions: ['1st cat', '1st dog', '1st fish'], // should use this description for "fish"
718+
},
719+
{
720+
enum: ['bird', 'fish', 'ant'],
721+
enumDescriptions: ['2nd bird', '2nd fish', '2nd ant'],
722+
},
723+
],
724+
},
725+
},
726+
});
727+
const content = 'animal:\n fis|n|'; // len: 13, pos: 12
728+
const result = await parseSetup(content);
729+
730+
assert.strictEqual(MarkupContent.is(result.contents), true);
731+
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
732+
assert.strictEqual(
733+
(result.contents as MarkupContent).value,
734+
`should return this description
735+
736+
Allowed Values:
737+
738+
* \`ant\`: 2nd ant
739+
* \`cat\`: 1st cat
740+
* \`dog\`: 1st dog
741+
* \`fish\`: 1st fish
742+
* \`bird\`: 2nd bird
743+
626744
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
627745
);
628746
});

0 commit comments

Comments
 (0)