Skip to content

Commit

Permalink
fix: hyphen for array item
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed Oct 13, 2023
1 parent f7df5fc commit 8e4d05f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ export class YamlCompletion {
collector,
{},
'property',
Array.isArray(nodeParent.items)
Array.isArray(nodeParent.items) && !isInArray
);
}

Expand Down Expand Up @@ -1424,10 +1424,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
4 changes: 2 additions & 2 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ describe('Auto Completion Tests', () => {
.then(done, done);
});

it('Array of enum autocomplete on 2nd position without `-`', (done) => {
it('Array of enum autocomplete on 2nd position without `-` should auto add `-` and `- (array item)`', (done) => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
Expand All @@ -1508,7 +1508,7 @@ describe('Auto Completion Tests', () => {
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
[
{
insertText: 'Test',
insertText: '- Test', // auto added `- `
label: 'Test',
},
{
Expand Down
15 changes: 14 additions & 1 deletion test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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 @@ -273,7 +286,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 8e4d05f

Please sign in to comment.