Skip to content

Commit 37ab7f0

Browse files
committed
fix: array default value
PR: redhat-developer#968
1 parent 944ad29 commit 37ab7f0

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
@@ -1449,7 +1449,12 @@ export class YamlCompletion {
14491449
if (Array.isArray(value)) {
14501450
let insertText = '\n';
14511451
for (const arrValue of value) {
1452-
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
1452+
if (typeof arrValue === 'object') {
1453+
const objectText = this.getInsertTemplateForValue(arrValue, indent, { ...navOrder }, separatorAfter);
1454+
insertText += convertObjectToArrayItem(objectText, indent);
1455+
} else {
1456+
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
1457+
}
14531458
}
14541459
return insertText;
14551460
} else if (typeof value === 'object') {
@@ -1933,3 +1938,11 @@ export function addUniquePostfix(uri: string): string {
19331938
export function removeUniquePostfix(uri: string): string {
19341939
return uri.replace(/(^|\/)_tmp_[0-9a-z]+\//, '$1');
19351940
}
1941+
1942+
export function convertObjectToArrayItem(objectText: string, indent: string): string {
1943+
const objectItem = objectText.replace(/^(\s+)/gm, (match, _, index) => {
1944+
// first line can contains newLine, so use indent from input parameter
1945+
return index === 0 ? `${indent}- ` : `${match} `;
1946+
});
1947+
return objectItem;
1948+
}

test/autoCompletion.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
2222
import { LanguageService } from '../src';
2323
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
2424
import { jigxBranchTest } from './utils/testHelperJigx';
25+
import { convertObjectToArrayItem } from '../src/languageservice/services/yamlCompletion';
2526

2627
//TODO Petr fix merge
2728
describe('Auto Completion Tests', () => {
@@ -1166,6 +1167,89 @@ describe('Auto Completion Tests', () => {
11661167
expect(completion.items.map((i) => i.insertText)).to.deep.equal(['car:\n engine: ${1:type\\$1234}']);
11671168
});
11681169

1170+
it('Autocompletion with default value as an object', async () => {
1171+
schemaProvider.addSchema(SCHEMA_ID, {
1172+
type: 'object',
1173+
properties: {
1174+
car: {
1175+
type: 'object',
1176+
default: {
1177+
engine: {
1178+
fuel: 'gasoline',
1179+
},
1180+
wheel: 4,
1181+
},
1182+
},
1183+
},
1184+
});
1185+
const content = 'car: |\n|';
1186+
const completion = await parseSetup(content);
1187+
expect(completion.items.map((i) => i.insertText)).to.deep.equal([
1188+
'\n ${1:engine}:\n ${2:fuel}: ${3:gasoline}\n ${4:wheel}: ${5:4}\n',
1189+
]);
1190+
});
1191+
1192+
it('Autocompletion with default value as an array', async () => {
1193+
schemaProvider.addSchema(SCHEMA_ID, {
1194+
type: 'object',
1195+
properties: {
1196+
garage: {
1197+
type: 'array',
1198+
items: {
1199+
type: 'object',
1200+
},
1201+
default: [
1202+
{
1203+
car: {
1204+
engine: { fuel: 'gasoline' },
1205+
wheel: [1, 2],
1206+
},
1207+
},
1208+
{
1209+
car: {
1210+
engine: { fuel: 'diesel' },
1211+
},
1212+
},
1213+
],
1214+
},
1215+
},
1216+
});
1217+
const content = 'garage: |\n|';
1218+
const completion = await parseSetup(content);
1219+
const expected = `
1220+
- \${1:car}:
1221+
\${2:engine}:
1222+
\${3:fuel}: \${4:gasoline}
1223+
\${5:wheel}:
1224+
- \${6:1}
1225+
- \${7:2}
1226+
- \${1:car}:
1227+
\${2:engine}:
1228+
\${3:fuel}: \${4:diesel}
1229+
`;
1230+
expect(completion.items.map((i) => i.insertText)).to.deep.equal([expected]);
1231+
});
1232+
1233+
it('should convert object to array item', () => {
1234+
const objectText = `
1235+
car:
1236+
engine:
1237+
fuel: gasoline
1238+
wheel:
1239+
- 1
1240+
- 2
1241+
`;
1242+
const expectedArrayItem = ` - car:
1243+
engine:
1244+
fuel: gasoline
1245+
wheel:
1246+
- 1
1247+
- 2
1248+
`;
1249+
const arrayItem = convertObjectToArrayItem(objectText, ' ');
1250+
expect(arrayItem).to.equal(expectedArrayItem);
1251+
});
1252+
11691253
it('Autocompletion should escape colon when indicating map', async () => {
11701254
schemaProvider.addSchema(SCHEMA_ID, {
11711255
type: 'object',

0 commit comments

Comments
 (0)