Skip to content

Commit

Permalink
Merge branch 'fix/suggest-hyphen-in-arrays' into release/23
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed Oct 13, 2023
2 parents 1e7779b + 8e4d05f commit 4a50bef
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ export class YamlCompletion {
collector,
{},
'property',
Array.isArray(nodeParent.items)
Array.isArray(nodeParent.items) && !isInArray
);
}

Expand Down Expand Up @@ -1079,13 +1079,8 @@ export class YamlCompletion {
if (index < s.schema.items.length) {
this.addSchemaValueCompletions(s.schema.items[index], separatorAfter, collector, types, 'value');
}
} else if (
typeof s.schema.items === 'object' &&
(s.schema.items.type === 'object' || isAnyOfAllOfOneOfType(s.schema.items))
) {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
} else {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value');
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
}
}
}
Expand Down Expand Up @@ -1127,7 +1122,7 @@ export class YamlCompletion {
);
collector.add({
kind: this.getSuggestionKind(schema.type),
label: '- (array item) ' + (schemaType || index),
label: '- (array item) ' + ((schemaType || index) ?? ''),
documentation: documentation,
insertText: insertText,
insertTextFormat: InsertTextFormat.Snippet,
Expand Down Expand Up @@ -1611,10 +1606,11 @@ export class YamlCompletion {
} else if (schema.enumDescriptions && i < schema.enumDescriptions.length) {
documentation = schema.enumDescriptions[i];
}
const insertText = (isArray ? '- ' : '') + this.getInsertTextForValue(enm, separatorAfter, schema.type);
collector.add({
kind: this.getSuggestionKind(schema.type),
label: this.getLabelForValue(enm),
insertText: this.getInsertTextForValue(enm, separatorAfter, schema.type),
insertText,
insertTextFormat: InsertTextFormat.Snippet,
documentation: documentation,
});
Expand Down
36 changes: 35 additions & 1 deletion test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,39 @@ describe('Auto Completion Tests', () => {
.then(done, done);
});

it('Array of enum autocomplete on 2nd position without `-` should auto add `-` and `- (array item)`', (done) => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
references: {
type: 'array',
items: {
enum: ['Test'],
},
},
},
});
const content = 'references:\n - Test\n |\n|';
const completion = parseSetup(content);
completion
.then(function (result) {
assert.deepEqual(
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
[
{
insertText: '- Test', // auto added `- `
label: 'Test',
},
{
insertText: '- $1\n',
label: '- (array item) ',
},
]
);
})
.then(done, done);
});

it('Array of objects autocomplete with 4 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
Expand Down Expand Up @@ -2134,7 +2167,8 @@ describe('Auto Completion Tests', () => {
assert.equal(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);

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

it('should handle absolute path', async () => {
Expand Down
28 changes: 24 additions & 4 deletions test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ describe('Default Snippet Tests', () => {
const completion = parseSetup(content, content.length);
completion
.then(function (result) {
assert.equal(result.items.length, 1);
assert.equal(result.items[0].insertText, '- item1: $1\n item2: $2');
assert.equal(result.items[0].label, 'My array item');
assert.deepEqual(
result.items.map((i) => ({ insertText: i.insertText, label: i.label })),
[
{ insertText: '- item1: $1\n item2: $2', label: 'My array item' },
{
insertText: '- $1\n',
label: '- (array item) ',
},
]
);
})
.then(done, done);
});
Expand Down Expand Up @@ -235,6 +242,19 @@ describe('Default Snippet Tests', () => {
.then(done, done);
});

it('Snippet in string schema should autocomplete on same line (snippet is defined in body property)', (done) => {
const content = 'arrayStringValueSnippet:\n - |\n|';
const completion = parseSetup(content);
completion
.then(function (result) {
assert.deepEqual(
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
[{ insertText: 'banana', label: 'Banana' }]
);
})
.then(done, done);
});

it('Snippet in boolean schema should autocomplete on same line', (done) => {
const content = 'boolean: | |'; // len: 10, pos: 9
const completion = parseSetup(content);
Expand Down Expand Up @@ -268,7 +288,7 @@ describe('Default Snippet Tests', () => {
const completion = parseSetup(content);
completion
.then(function (result) {
assert.equal(result.items.length, 15); // This is just checking the total number of snippets in the defaultSnippets.json
assert.equal(result.items.length, 16); // This is just checking the total number of snippets in the defaultSnippets.json
assert.equal(result.items[4].label, 'longSnippet');
// eslint-disable-next-line
assert.equal(
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/defaultSnippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@
}
]
},
"arrayStringValueSnippet": {
"type": "array",
"items": {
"type": "string",
"defaultSnippets": [
{
"label": "Banana",
"body": "banana"
}
]
}
},
"arrayObjectSnippet": {
"type": "object",
"defaultSnippets": [
Expand Down

0 comments on commit 4a50bef

Please sign in to comment.