Skip to content

Commit ce643a9

Browse files
committed
fix: array default value
1 parent 7203630 commit ce643a9

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/languageservice/services/yamlCompletion.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,12 @@ export class YamlCompletion {
12771277
if (Array.isArray(value)) {
12781278
let insertText = '\n';
12791279
for (const arrValue of value) {
1280-
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
1280+
if (typeof arrValue === 'object') {
1281+
const objectText = this.getInsertTemplateForValue(arrValue, indent, { ...navOrder }, separatorAfter);
1282+
insertText += convertObjectToArrayItem(objectText, indent);
1283+
} else {
1284+
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
1285+
}
12811286
}
12821287
return insertText;
12831288
} else if (typeof value === 'object') {
@@ -1729,3 +1734,11 @@ function evaluateTab1Symbol(value: string): string {
17291734
function isParentCompletionItem(item: CompletionItemBase): item is CompletionItem {
17301735
return 'parent' in item;
17311736
}
1737+
1738+
export function convertObjectToArrayItem(objectText: string, indent: string): string {
1739+
const objectItem = objectText.replace(/^(\s+)/gm, (match, _, index) => {
1740+
// first line can contains newLine, so use indent from input parameter
1741+
return index === 0 ? `${indent}- ` : `${match} `;
1742+
});
1743+
return objectItem;
1744+
}

test/autoCompletion.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { expect } from 'chai';
2121
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
2222
import { LanguageService } from '../src';
2323
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
24+
import { convertObjectToArrayItem } from '../src/languageservice/services/yamlCompletion';
2425

2526
describe('Auto Completion Tests', () => {
2627
let languageSettingsSetup: ServiceSetup;
@@ -931,6 +932,89 @@ describe('Auto Completion Tests', () => {
931932
);
932933
});
933934

935+
it('Autocompletion with default value as an object', async () => {
936+
schemaProvider.addSchema(SCHEMA_ID, {
937+
type: 'object',
938+
properties: {
939+
car: {
940+
type: 'object',
941+
default: {
942+
engine: {
943+
fuel: 'gasoline',
944+
},
945+
wheel: 4,
946+
},
947+
},
948+
},
949+
});
950+
const content = 'car: |\n|';
951+
const completion = await parseSetup(content);
952+
expect(completion.items.map((i) => i.insertText)).to.deep.equal([
953+
'\n ${1:engine}:\n ${2:fuel}: ${3:gasoline}\n ${4:wheel}: ${5:4}\n',
954+
]);
955+
});
956+
957+
it('Autocompletion with default value as an array', async () => {
958+
schemaProvider.addSchema(SCHEMA_ID, {
959+
type: 'object',
960+
properties: {
961+
garage: {
962+
type: 'array',
963+
items: {
964+
type: 'object',
965+
},
966+
default: [
967+
{
968+
car: {
969+
engine: { fuel: 'gasoline' },
970+
wheel: [1, 2],
971+
},
972+
},
973+
{
974+
car: {
975+
engine: { fuel: 'diesel' },
976+
},
977+
},
978+
],
979+
},
980+
},
981+
});
982+
const content = 'garage: |\n|';
983+
const completion = await parseSetup(content);
984+
const expected = `
985+
- \${1:car}:
986+
\${2:engine}:
987+
\${3:fuel}: \${4:gasoline}
988+
\${5:wheel}:
989+
- \${6:1}
990+
- \${7:2}
991+
- \${1:car}:
992+
\${2:engine}:
993+
\${3:fuel}: \${4:diesel}
994+
`;
995+
expect(completion.items.map((i) => i.insertText)).to.deep.equal([expected]);
996+
});
997+
998+
it('should convert object to array item', () => {
999+
const objectText = `
1000+
car:
1001+
engine:
1002+
fuel: gasoline
1003+
wheel:
1004+
- 1
1005+
- 2
1006+
`;
1007+
const expectedArrayItem = ` - car:
1008+
engine:
1009+
fuel: gasoline
1010+
wheel:
1011+
- 1
1012+
- 2
1013+
`;
1014+
const arrayItem = convertObjectToArrayItem(objectText, ' ');
1015+
expect(arrayItem).to.equal(expectedArrayItem);
1016+
});
1017+
9341018
it('Autocompletion should escape colon when indicating map', async () => {
9351019
schemaProvider.addSchema(SCHEMA_ID, {
9361020
type: 'object',

0 commit comments

Comments
 (0)