From 6ab70bdd195443e1cb7268e6c66ad1260fbbc668 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 31 Mar 2021 12:04:34 +0300 Subject: [PATCH] #408 support $ref in additionalItems --- .../services/yamlCompletion.ts | 6 +- .../services/yamlSchemaService.ts | 1 + test/autoCompletion.test.ts | 64 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 8c225e9e..2ceefb40 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -1016,7 +1016,7 @@ export class YAMLCompletion extends JSONCompletion { document.getText().substr(lineOffset[linePos + 1] || document.getText().length); // For when missing semi colon case - } else { + } else if (trimmedText.indexOf('[') === -1) { // Add a semicolon to the end of the current line so we can validate the node newText = document.getText().substring(0, start + textLine.length) + @@ -1024,6 +1024,10 @@ export class YAMLCompletion extends JSONCompletion { document.getText().substr(lineOffset[linePos + 1] || document.getText().length); } + if (newText.length === 0) { + newText = document.getText(); + } + return { newText: newText, newPosition: textDocumentPosition, diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts index 06aae076..7e6023b5 100644 --- a/src/languageservice/services/yamlSchemaService.ts +++ b/src/languageservice/services/yamlSchemaService.ts @@ -244,6 +244,7 @@ export class YAMLSchemaService extends JSONSchemaService { collectEntries( next.items, + next.additionalItems, next.additionalProperties, next.not, next.contains, diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index 2b664227..0fedd0a6 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -1840,6 +1840,70 @@ describe('Auto Completion Tests', () => { createExpectedCompletion('kind', 'kind', 0, 0, 0, 2, 10, InsertTextFormat.Snippet, { documentation: '' }) ); }); + + it('should follow $ref in additionalItems', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + test: { + $ref: '#/definitions/Recur', + }, + }, + definitions: { + Recur: { + type: 'array', + items: [ + { + type: 'string', + enum: ['and'], + }, + ], + additionalItems: { + $ref: '#/definitions/Recur', + }, + }, + }, + }); + + const content = 'test:\n - and\n - - '; + const completion = await parseSetup(content, 19); + expect(completion.items).lengthOf(1); + expect(completion.items[0]).eql( + createExpectedCompletion('and', 'and', 2, 4, 2, 5, 12, InsertTextFormat.Snippet, { documentation: undefined }) + ); + }); + + it('should follow $ref in additionalItems for flow style array', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + test: { + $ref: '#/definitions/Recur', + }, + }, + definitions: { + Recur: { + type: 'array', + items: [ + { + type: 'string', + enum: ['and'], + }, + ], + additionalItems: { + $ref: '#/definitions/Recur', + }, + }, + }, + }); + + const content = 'test:\n - and\n - []'; + const completion = await parseSetup(content, 18); + expect(completion.items).lengthOf(1); + expect(completion.items[0]).eql( + createExpectedCompletion('and', 'and', 2, 4, 2, 4, 12, InsertTextFormat.Snippet, { documentation: undefined }) + ); + }); }); describe('Array completion', () => { it('Simple array object completion with "-" without any item', async () => {