Skip to content

Commit 4a50bef

Browse files
committed
Merge branch 'fix/suggest-hyphen-in-arrays' into release/23
2 parents 1e7779b + 8e4d05f commit 4a50bef

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

src/languageservice/services/yamlCompletion.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ export class YamlCompletion {
983983
collector,
984984
{},
985985
'property',
986-
Array.isArray(nodeParent.items)
986+
Array.isArray(nodeParent.items) && !isInArray
987987
);
988988
}
989989

@@ -1079,13 +1079,8 @@ export class YamlCompletion {
10791079
if (index < s.schema.items.length) {
10801080
this.addSchemaValueCompletions(s.schema.items[index], separatorAfter, collector, types, 'value');
10811081
}
1082-
} else if (
1083-
typeof s.schema.items === 'object' &&
1084-
(s.schema.items.type === 'object' || isAnyOfAllOfOneOfType(s.schema.items))
1085-
) {
1086-
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
10871082
} else {
1088-
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value');
1083+
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
10891084
}
10901085
}
10911086
}
@@ -1127,7 +1122,7 @@ export class YamlCompletion {
11271122
);
11281123
collector.add({
11291124
kind: this.getSuggestionKind(schema.type),
1130-
label: '- (array item) ' + (schemaType || index),
1125+
label: '- (array item) ' + ((schemaType || index) ?? ''),
11311126
documentation: documentation,
11321127
insertText: insertText,
11331128
insertTextFormat: InsertTextFormat.Snippet,
@@ -1611,10 +1606,11 @@ export class YamlCompletion {
16111606
} else if (schema.enumDescriptions && i < schema.enumDescriptions.length) {
16121607
documentation = schema.enumDescriptions[i];
16131608
}
1609+
const insertText = (isArray ? '- ' : '') + this.getInsertTextForValue(enm, separatorAfter, schema.type);
16141610
collector.add({
16151611
kind: this.getSuggestionKind(schema.type),
16161612
label: this.getLabelForValue(enm),
1617-
insertText: this.getInsertTextForValue(enm, separatorAfter, schema.type),
1613+
insertText,
16181614
insertTextFormat: InsertTextFormat.Snippet,
16191615
documentation: documentation,
16201616
});

test/autoCompletion.test.ts

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

1699+
it('Array of enum autocomplete on 2nd position without `-` should auto add `-` and `- (array item)`', (done) => {
1700+
schemaProvider.addSchema(SCHEMA_ID, {
1701+
type: 'object',
1702+
properties: {
1703+
references: {
1704+
type: 'array',
1705+
items: {
1706+
enum: ['Test'],
1707+
},
1708+
},
1709+
},
1710+
});
1711+
const content = 'references:\n - Test\n |\n|';
1712+
const completion = parseSetup(content);
1713+
completion
1714+
.then(function (result) {
1715+
assert.deepEqual(
1716+
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
1717+
[
1718+
{
1719+
insertText: '- Test', // auto added `- `
1720+
label: 'Test',
1721+
},
1722+
{
1723+
insertText: '- $1\n',
1724+
label: '- (array item) ',
1725+
},
1726+
]
1727+
);
1728+
})
1729+
.then(done, done);
1730+
});
1731+
16991732
it('Array of objects autocomplete with 4 space indentation check', async () => {
17001733
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
17011734
languageService.configure(languageSettingsSetup.languageSettings);
@@ -2134,7 +2167,8 @@ describe('Auto Completion Tests', () => {
21342167
assert.equal(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
21352168

21362169
const resultDoc2 = await parseSetup(content, content.length);
2137-
assert.equal(resultDoc2.items.length, 0, `Expecting no items in completion but found ${resultDoc2.items.length}`);
2170+
assert.equal(resultDoc2.items.length, 1, `Expecting 1 item in completion but found ${resultDoc2.items.length}`);
2171+
assert.equal(resultDoc2.items[0].label, '- (array item) ');
21382172
});
21392173

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

test/defaultSnippets.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,16 @@ describe('Default Snippet Tests', () => {
9292
const completion = parseSetup(content, content.length);
9393
completion
9494
.then(function (result) {
95-
assert.equal(result.items.length, 1);
96-
assert.equal(result.items[0].insertText, '- item1: $1\n item2: $2');
97-
assert.equal(result.items[0].label, 'My array item');
95+
assert.deepEqual(
96+
result.items.map((i) => ({ insertText: i.insertText, label: i.label })),
97+
[
98+
{ insertText: '- item1: $1\n item2: $2', label: 'My array item' },
99+
{
100+
insertText: '- $1\n',
101+
label: '- (array item) ',
102+
},
103+
]
104+
);
98105
})
99106
.then(done, done);
100107
});
@@ -235,6 +242,19 @@ describe('Default Snippet Tests', () => {
235242
.then(done, done);
236243
});
237244

245+
it('Snippet in string schema should autocomplete on same line (snippet is defined in body property)', (done) => {
246+
const content = 'arrayStringValueSnippet:\n - |\n|';
247+
const completion = parseSetup(content);
248+
completion
249+
.then(function (result) {
250+
assert.deepEqual(
251+
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
252+
[{ insertText: 'banana', label: 'Banana' }]
253+
);
254+
})
255+
.then(done, done);
256+
});
257+
238258
it('Snippet in boolean schema should autocomplete on same line', (done) => {
239259
const content = 'boolean: | |'; // len: 10, pos: 9
240260
const completion = parseSetup(content);
@@ -268,7 +288,7 @@ describe('Default Snippet Tests', () => {
268288
const completion = parseSetup(content);
269289
completion
270290
.then(function (result) {
271-
assert.equal(result.items.length, 15); // This is just checking the total number of snippets in the defaultSnippets.json
291+
assert.equal(result.items.length, 16); // This is just checking the total number of snippets in the defaultSnippets.json
272292
assert.equal(result.items[4].label, 'longSnippet');
273293
// eslint-disable-next-line
274294
assert.equal(

test/fixtures/defaultSnippets.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@
110110
}
111111
]
112112
},
113+
"arrayStringValueSnippet": {
114+
"type": "array",
115+
"items": {
116+
"type": "string",
117+
"defaultSnippets": [
118+
{
119+
"label": "Banana",
120+
"body": "banana"
121+
}
122+
]
123+
}
124+
},
113125
"arrayObjectSnippet": {
114126
"type": "object",
115127
"defaultSnippets": [

0 commit comments

Comments
 (0)