diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 1c0246c5..8b869170 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -676,7 +676,7 @@ export class YamlCompletion { key: string, propertySchema: JSONSchema, separatorAfter: string, - ident = this.indentation + indent = this.indentation ): string { const propertyText = this.getInsertTextForValue(key, '', 'string'); const resultText = propertyText + ':'; @@ -733,10 +733,10 @@ export class YamlCompletion { nValueProposals += propertySchema.examples.length; } if (propertySchema.properties) { - return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, ident).insertText}`; + return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, indent).insertText}`; } else if (propertySchema.items) { - return `${resultText}\n${this.indentation}- ${ - this.getInsertTextForArray(propertySchema.items, separatorAfter).insertText + return `${resultText}\n${indent}- ${ + this.getInsertTextForArray(propertySchema.items, separatorAfter, 1, indent).insertText }`; } if (nValueProposals === 0) { @@ -748,10 +748,10 @@ export class YamlCompletion { value = ' $1'; break; case 'object': - value = `\n${ident}`; + value = `\n${indent}`; break; case 'array': - value = `\n${ident}- `; + value = `\n${indent}- `; break; case 'number': case 'integer': @@ -804,7 +804,7 @@ export class YamlCompletion { break; case 'array': { - const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++); + const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++, indent); const arrayInsertLines = arrayInsertResult.insertText.split('\n'); let arrayTemplate = arrayInsertResult.insertText; if (arrayInsertLines.length > 1) { @@ -856,7 +856,7 @@ export class YamlCompletion { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1): InsertText { + private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1, indent = this.indentation): InsertText { let insertText = ''; if (!schema) { insertText = `$${insertIndex++}`; @@ -884,7 +884,7 @@ export class YamlCompletion { break; case 'object': { - const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${this.indentation} `, insertIndex++); + const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${indent} `, insertIndex++); insertText = objectInsertResult.insertText.trimLeft(); insertIndex = objectInsertResult.insertIndex; } diff --git a/test/autoCompletionFix.test.ts b/test/autoCompletionFix.test.ts index 75f75038..a770e733 100644 --- a/test/autoCompletionFix.test.ts +++ b/test/autoCompletionFix.test.ts @@ -32,6 +32,13 @@ describe('Auto Completion Fix Tests', () => { yamlSettings = settings; }); + /** + * + * @param content + * @param line starts with 0 index + * @param character starts with 1 index + * @returns + */ function parseSetup(content: string, line: number, character: number): Promise { const testTextDocument = setupSchemaIDTextDocument(content); yamlSettings.documents = new TextDocumentTestManager(); @@ -256,4 +263,70 @@ objB: }) ); }); + + it('Autocomplete indent on array when parent is array', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + examples: { + type: 'array', + items: { + type: 'object', + properties: { + objectWithArray: { + type: 'array', + items: { + type: 'string', + }, + }, + }, + }, + }, + }, + }); + const content = 'examples:\n - '; + const completion = await parseSetup(content, 1, 4); + + expect(completion.items.length).equal(1); + expect(completion.items[0]).to.be.deep.equal( + createExpectedCompletion('objectWithArray', 'objectWithArray:\n - ${1:""}', 1, 4, 1, 4, 10, 2, { + documentation: '', + }) + ); + }); + it('Autocomplete indent on array object when parent is array', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + examples: { + type: 'array', + items: { + type: 'object', + properties: { + objectWithArray: { + type: 'array', + items: { + type: 'object', + required: ['item', 'item2'], + properties: { + item: { type: 'string' }, + item2: { type: 'string' }, + }, + }, + }, + }, + }, + }, + }, + }); + const content = 'examples:\n - '; + const completion = await parseSetup(content, 1, 4); + + expect(completion.items.length).equal(1); + expect(completion.items[0]).to.be.deep.equal( + createExpectedCompletion('objectWithArray', 'objectWithArray:\n - item: $1\n item2: $2', 1, 4, 1, 4, 10, 2, { + documentation: '', + }) + ); + }); });