diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 62da4ce1c..6ef52974d 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -482,6 +482,21 @@ export class YamlCompletion { node = pair.value; } } + } else if (isSeq(node)) { + if (lineContent.charAt(position.character - 1) !== '-') { + const map = this.createTempObjNode(currentWord, node, currentDoc); + map.items = []; + // eslint-disable-next-line no-self-assign + currentDoc.internalDocument = currentDoc.internalDocument; + for (const pair of node.items) { + if (isMap(pair)) { + pair.items.forEach((value) => { + map.items.push(value); + }); + } + } + node = map; + } } } } @@ -691,7 +706,9 @@ export class YamlCompletion { } for (const schema of matchingSchemas) { if ( - ((schema.node.internalNode === node && !matchOriginal) || (schema.node.internalNode === originalNode && !hasColon)) && + ((schema.node.internalNode === node && !matchOriginal) || + (schema.node.internalNode === originalNode && !hasColon) || + (schema.node.parent?.internalNode === originalNode && !hasColon)) && !schema.inverted ) { this.collectDefaultSnippets(schema.schema, separatorAfter, collector, { diff --git a/test/autoCompletionFix.test.ts b/test/autoCompletionFix.test.ts index 4250d2c5e..f947d594b 100644 --- a/test/autoCompletionFix.test.ts +++ b/test/autoCompletionFix.test.ts @@ -101,6 +101,39 @@ describe('Auto Completion Fix Tests', () => { ); }); + it('completion with array objects', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'array', + items: { + type: 'object', + properties: { + prop1: { + type: 'string', + }, + prop2: { + type: 'string', + }, + prop3: { + type: 'string', + }, + }, + }, + }); + const content = '- prop1: a\n | |'; // len: 12, pos: 11 + const completion = await parseCaret(content); + expect(completion.items).lengthOf(2); + expect(completion.items[0]).eql( + createExpectedCompletion('prop2', 'prop2: ', 1, 3, 1, 4, 10, 2, { + documentation: '', + }) + ); + expect(completion.items[1]).eql( + createExpectedCompletion('prop3', 'prop3: ', 1, 3, 1, 4, 10, 2, { + documentation: '', + }) + ); + }); + it('should show completion on array empty array item', async () => { languageService.addSchema(SCHEMA_ID, { type: 'array',