Skip to content

Commit 2af180a

Browse files
committed
fix: suggest hyphen in array
1 parent 88c6dae commit 2af180a

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/languageservice/services/yamlCompletion.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,8 @@ export class YamlCompletion {
910910
if (index < s.schema.items.length) {
911911
this.addSchemaValueCompletions(s.schema.items[index], separatorAfter, collector, types, 'value');
912912
}
913-
} else if (
914-
typeof s.schema.items === 'object' &&
915-
(s.schema.items.type === 'object' || isAnyOfAllOfOneOfType(s.schema.items))
916-
) {
917-
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
918913
} else {
919-
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value');
914+
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
920915
}
921916
}
922917
}
@@ -958,7 +953,7 @@ export class YamlCompletion {
958953
);
959954
collector.add({
960955
kind: this.getSuggestionKind(schema.type),
961-
label: '- (array item) ' + (schemaType || index),
956+
label: '- (array item) ' + ((schemaType || index) ?? ''),
962957
documentation: documentation,
963958
insertText: insertText,
964959
insertTextFormat: InsertTextFormat.Snippet,

test/autoCompletion.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,39 @@ describe('Auto Completion Tests', () => {
14881488
.then(done, done);
14891489
});
14901490

1491+
it('Array of enum autocomplete on 2nd position without `-`', (done) => {
1492+
languageService.addSchema(SCHEMA_ID, {
1493+
type: 'object',
1494+
properties: {
1495+
references: {
1496+
type: 'array',
1497+
items: {
1498+
enum: ['Test'],
1499+
},
1500+
},
1501+
},
1502+
});
1503+
const content = 'references:\n - Test\n |\n|';
1504+
const completion = parseSetup(content);
1505+
completion
1506+
.then(function (result) {
1507+
assert.deepEqual(
1508+
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
1509+
[
1510+
{
1511+
insertText: 'Test',
1512+
label: 'Test',
1513+
},
1514+
{
1515+
insertText: '- $1\n',
1516+
label: '- (array item) ',
1517+
},
1518+
]
1519+
);
1520+
})
1521+
.then(done, done);
1522+
});
1523+
14911524
it('Array of objects autocomplete with 4 space indentation check', async () => {
14921525
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
14931526
languageService.configure(languageSettingsSetup.languageSettings);
@@ -1926,7 +1959,8 @@ describe('Auto Completion Tests', () => {
19261959
assert.equal(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
19271960

19281961
const resultDoc2 = await parseSetup(content, content.length);
1929-
assert.equal(resultDoc2.items.length, 0, `Expecting no items in completion but found ${resultDoc2.items.length}`);
1962+
assert.equal(resultDoc2.items.length, 1, `Expecting 1 item in completion but found ${resultDoc2.items.length}`);
1963+
assert.equal(resultDoc2.items[0].label, '- (array item) ');
19301964
});
19311965

19321966
it('should handle absolute path', async () => {

test/defaultSnippets.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,16 @@ describe('Default Snippet Tests', () => {
9191
const completion = parseSetup(content, content.length);
9292
completion
9393
.then(function (result) {
94-
assert.equal(result.items.length, 1);
95-
assert.equal(result.items[0].insertText, '- item1: $1\n item2: $2');
96-
assert.equal(result.items[0].label, 'My array item');
94+
assert.deepEqual(
95+
result.items.map((i) => ({ insertText: i.insertText, label: i.label })),
96+
[
97+
{ insertText: '- item1: $1\n item2: $2', label: 'My array item' },
98+
{
99+
insertText: '- $1\n',
100+
label: '- (array item) ',
101+
},
102+
]
103+
);
97104
})
98105
.then(done, done);
99106
});

0 commit comments

Comments
 (0)