Skip to content

Commit

Permalink
Fix another case of un-indented array completions behaving badly
Browse files Browse the repository at this point in the history
  • Loading branch information
winstliu committed Nov 1, 2024
1 parent 1be2e57 commit e856ce6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
18 changes: 1 addition & 17 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,20 +477,6 @@ 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 = [];
currentDoc.updateFromInternalDocument();
for (const pair of node.items) {
if (isMap(pair)) {
pair.items.forEach((value) => {
map.items.push(value);
});
}
}
node = map;
}
}
}
}
Expand Down Expand Up @@ -722,9 +708,7 @@ export class YamlCompletion {

for (const schema of matchingSchemas) {
if (
((schema.node.internalNode === node && !matchOriginal) ||
(schema.node.internalNode === originalNode && !hasColon) ||
(schema.node.parent?.internalNode === originalNode && !hasColon)) &&
((schema.node.internalNode === node && !matchOriginal) || (schema.node.internalNode === originalNode && !hasColon)) &&
!schema.inverted
) {
this.collectDefaultSnippets(schema.schema, separatorAfter, collector, {
Expand Down
38 changes: 37 additions & 1 deletion test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,7 @@ describe('Auto Completion Tests', () => {
expect(completion.items[0].insertText).eq('prop2: ');
});

it('should complete properties of an object under a list', async () => {
it('should complete properties of an object under an array', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
Expand All @@ -2269,15 +2269,51 @@ describe('Auto Completion Tests', () => {
},
},
},
name: {
type: 'string',
},
},
});

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

it('should not show bare property completions for array items when the cursor indentation is ambiguous', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
steps: {
type: 'array',
items: {
type: 'object',
properties: {
task: {
type: 'string',
},
inputs: {
type: 'string',
},
},
},
},
name: {
type: 'string',
},
},
});

// Here, the intent _could_ be to move out of the array, or it could be to continue adding to the array.
// Thus, `name: ` is fine and so is `- task: `, but _not_ a bare `task: `.
const content = 'steps:\n- task: PowerShell@2\n'; // len: 28
const completion = await parseSetup(content, 28);
expect(completion.items[0].label).eq('name');
expect(completion.items.slice(1).filter((item) => !item.insertText.startsWith('- '))).to.be.empty;
});

it('should complete string which contains number in default value', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down

0 comments on commit e856ce6

Please sign in to comment.