From d6d264e900ca0d7a3e1bff6186c43447f1933cba Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Wed, 2 Mar 2022 17:14:47 +0530 Subject: [PATCH 1/4] fixed auto competion not suggest const for array --- .../services/yamlCompletion.ts | 18 ++++++++++----- test/autoCompletion.test.ts | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 019825b4..74b74531 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -538,7 +538,7 @@ export class YamlCompletion { const hasColon = lineContent.indexOf(':') !== -1; const nodeParent = doc.getParent(node); - + const isArray = nodeParent && isSeq(nodeParent) && nodeParent.items.length > 0 ? true : false; const matchOriginal = matchingSchemas.find((it) => it.node.internalNode === originalNode && it.schema.properties); for (const schema of matchingSchemas) { if ( @@ -663,7 +663,7 @@ export class YamlCompletion { // - item1 // it will treated as a property key since `:` has been appended if (nodeParent && isSeq(nodeParent) && schema.schema.type !== 'object') { - this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}); + this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, isArray); } } @@ -1137,10 +1137,11 @@ export class YamlCompletion { schema: JSONSchemaRef, separatorAfter: string, collector: CompletionsCollector, - types: unknown + types: unknown, + isArray?: boolean ): void { if (typeof schema === 'object') { - this.addEnumValueCompletions(schema, separatorAfter, collector); + this.addEnumValueCompletions(schema, separatorAfter, collector, isArray); this.addDefaultValueCompletions(schema, separatorAfter, collector); this.collectTypes(schema, types); if (Array.isArray(schema.allOf)) { @@ -1231,8 +1232,13 @@ export class YamlCompletion { } } - private addEnumValueCompletions(schema: JSONSchema, separatorAfter: string, collector: CompletionsCollector): void { - if (isDefined(schema.const)) { + private addEnumValueCompletions( + schema: JSONSchema, + separatorAfter: string, + collector: CompletionsCollector, + isArray: boolean + ): void { + if (isDefined(schema.const) && !isArray) { collector.add({ kind: this.getSuggestionKind(schema.type), label: this.getLabelForValue(schema.const), diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index 4d0bd868..32d08292 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -1518,6 +1518,29 @@ describe('Auto Completion Tests', () => { ); }); + it('Array completion - should not suggest const', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + test: { + type: 'array', + items: { + type: 'object', + properties: { + constProp: { + type: 'string', + const: 'const1', + }, + }, + }, + }, + }, + }); + const content = 'test:\n - constProp:\n '; + const result = await parseSetup(content, content.length); + expect(result.items.length).to.be.equal(0); + }); + it('Object in array with 4 space indentation check', async () => { const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' '); languageService.configure(languageSettingsSetup.languageSettings); From 4ab8639373b7487fe47fcd8919342af195ee9853 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Mon, 7 Mar 2022 21:15:52 +0530 Subject: [PATCH 2/4] review comments addressed --- src/languageservice/services/yamlCompletion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 74b74531..f0363273 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -538,7 +538,6 @@ export class YamlCompletion { const hasColon = lineContent.indexOf(':') !== -1; const nodeParent = doc.getParent(node); - const isArray = nodeParent && isSeq(nodeParent) && nodeParent.items.length > 0 ? true : false; const matchOriginal = matchingSchemas.find((it) => it.node.internalNode === originalNode && it.schema.properties); for (const schema of matchingSchemas) { if ( @@ -663,6 +662,7 @@ export class YamlCompletion { // - item1 // it will treated as a property key since `:` has been appended if (nodeParent && isSeq(nodeParent) && schema.schema.type !== 'object') { + const isArray = nodeParent && isSeq(nodeParent) && nodeParent.items.length > 0; this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, isArray); } } From 53fbd1ff705922446a27c27113f8e4efd715f558 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Mon, 7 Mar 2022 21:20:34 +0530 Subject: [PATCH 3/4] check nodeparent is array or not --- src/languageservice/services/yamlCompletion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index f0363273..67853c47 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -662,7 +662,7 @@ export class YamlCompletion { // - item1 // it will treated as a property key since `:` has been appended if (nodeParent && isSeq(nodeParent) && schema.schema.type !== 'object') { - const isArray = nodeParent && isSeq(nodeParent) && nodeParent.items.length > 0; + const isArray = nodeParent.items.length > 0; this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, isArray); } } From ba099546a80b607a145237889483a7e63f87e99d Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Wed, 9 Mar 2022 18:30:17 +0530 Subject: [PATCH 4/4] changed to Array.isArray() instead of check its length --- src/languageservice/services/yamlCompletion.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 67853c47..4d4be52e 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -662,8 +662,7 @@ export class YamlCompletion { // - item1 // it will treated as a property key since `:` has been appended if (nodeParent && isSeq(nodeParent) && schema.schema.type !== 'object') { - const isArray = nodeParent.items.length > 0; - this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, isArray); + this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, Array.isArray(nodeParent.items)); } }