From 73251c1b3f7b1f5476bc8432aed382be50c62ca8 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 22 Jun 2021 15:34:48 +0300 Subject: [PATCH 1/2] Fix error when completion item label not string Signed-off-by: Yevhen Vydolob --- .../services/yamlCompletion.ts | 12 ++++++++--- test/autoCompletion.test.ts | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 31129491..33dcca7e 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -519,7 +519,7 @@ export class YAMLCompletion extends JSONCompletion { } collector.add({ kind: this.getSuggestionKind(type), - label: value, + label: this.getLabelForValue(value), insertText: this.getInsertTextForValue(value, separatorAfter, type), insertTextFormat: InsertTextFormat.Snippet, }); @@ -967,11 +967,17 @@ export class YAMLCompletion extends JSONCompletion { return this.getInsertTextForValue(value, separatorAfter, type); } - private getLabelForValue(value: string): string { + private getLabelForValue(value: unknown): string { if (value === null) { return 'null'; // return string with 'null' value if schema contains null as possible value } - return value; + if (Array.isArray(value)) { + return JSON.stringify(value); + } + if (typeof value === 'object') { + return JSON.stringify(value); + } + return value as string; } /** diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index d0d17516..8306e3d4 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -1815,6 +1815,26 @@ describe('Auto Completion Tests', () => { expect(trueItem.textEdit.newText).equal('"true"'); }); + it('should provide label as string for examples completion item', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + fooBar: { + type: 'array', + items: { + type: 'string', + examples: ['test'], + }, + }, + }, + }); + + const content = 'fooBar: \n'; + const completion = await parseSetup(content, 8); + + expect(completion.items).length(1); + }); + it('should provide completion for flow map', async () => { languageService.addSchema(SCHEMA_ID, { type: 'object', From 646b91762ed31186c3967df38cd6b324b8825067 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 22 Jun 2021 17:15:58 +0300 Subject: [PATCH 2/2] remove unnecessary check Signed-off-by: Yevhen Vydolob --- src/languageservice/services/yamlCompletion.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 33dcca7e..f3e6a129 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -974,9 +974,6 @@ export class YAMLCompletion extends JSONCompletion { if (Array.isArray(value)) { return JSON.stringify(value); } - if (typeof value === 'object') { - return JSON.stringify(value); - } return value as string; }