Skip to content

Commit e856ce6

Browse files
committed
Fix another case of un-indented array completions behaving badly
Contributed to upstream as redhat-developer/yaml-language-server#989
1 parent 1be2e57 commit e856ce6

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/languageservice/services/yamlCompletion.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -477,20 +477,6 @@ export class YamlCompletion {
477477
node = pair.value;
478478
}
479479
}
480-
} else if (isSeq(node)) {
481-
if (lineContent.charAt(position.character - 1) !== '-') {
482-
const map = this.createTempObjNode(currentWord, node, currentDoc);
483-
map.items = [];
484-
currentDoc.updateFromInternalDocument();
485-
for (const pair of node.items) {
486-
if (isMap(pair)) {
487-
pair.items.forEach((value) => {
488-
map.items.push(value);
489-
});
490-
}
491-
}
492-
node = map;
493-
}
494480
}
495481
}
496482
}
@@ -722,9 +708,7 @@ export class YamlCompletion {
722708

723709
for (const schema of matchingSchemas) {
724710
if (
725-
((schema.node.internalNode === node && !matchOriginal) ||
726-
(schema.node.internalNode === originalNode && !hasColon) ||
727-
(schema.node.parent?.internalNode === originalNode && !hasColon)) &&
711+
((schema.node.internalNode === node && !matchOriginal) || (schema.node.internalNode === originalNode && !hasColon)) &&
728712
!schema.inverted
729713
) {
730714
this.collectDefaultSnippets(schema.schema, separatorAfter, collector, {

test/autoCompletion.test.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ describe('Auto Completion Tests', () => {
22512251
expect(completion.items[0].insertText).eq('prop2: ');
22522252
});
22532253

2254-
it('should complete properties of an object under a list', async () => {
2254+
it('should complete properties of an object under an array', async () => {
22552255
schemaProvider.addSchema(SCHEMA_ID, {
22562256
type: 'object',
22572257
properties: {
@@ -2269,15 +2269,51 @@ describe('Auto Completion Tests', () => {
22692269
},
22702270
},
22712271
},
2272+
name: {
2273+
type: 'string',
2274+
},
22722275
},
22732276
});
22742277

2278+
// Thanks to the indentation, the intent is clear: continue adding to the current object.
22752279
const content = 'steps:\n- task: PowerShell@2\n '; // len: 30
22762280
const completion = await parseSetup(content, 30);
22772281
expect(completion.items).lengthOf(1);
22782282
expect(completion.items[0].label).eq('inputs');
22792283
});
22802284

2285+
it('should not show bare property completions for array items when the cursor indentation is ambiguous', async () => {
2286+
schemaProvider.addSchema(SCHEMA_ID, {
2287+
type: 'object',
2288+
properties: {
2289+
steps: {
2290+
type: 'array',
2291+
items: {
2292+
type: 'object',
2293+
properties: {
2294+
task: {
2295+
type: 'string',
2296+
},
2297+
inputs: {
2298+
type: 'string',
2299+
},
2300+
},
2301+
},
2302+
},
2303+
name: {
2304+
type: 'string',
2305+
},
2306+
},
2307+
});
2308+
2309+
// Here, the intent _could_ be to move out of the array, or it could be to continue adding to the array.
2310+
// Thus, `name: ` is fine and so is `- task: `, but _not_ a bare `task: `.
2311+
const content = 'steps:\n- task: PowerShell@2\n'; // len: 28
2312+
const completion = await parseSetup(content, 28);
2313+
expect(completion.items[0].label).eq('name');
2314+
expect(completion.items.slice(1).filter((item) => !item.insertText.startsWith('- '))).to.be.empty;
2315+
});
2316+
22812317
it('should complete string which contains number in default value', async () => {
22822318
schemaProvider.addSchema(SCHEMA_ID, {
22832319
type: 'object',

0 commit comments

Comments
 (0)