Skip to content

Commit

Permalink
fix: array default value
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed May 15, 2024
1 parent 944ad29 commit 37ab7f0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,12 @@ export class YamlCompletion {
if (Array.isArray(value)) {
let insertText = '\n';
for (const arrValue of value) {
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
if (typeof arrValue === 'object') {
const objectText = this.getInsertTemplateForValue(arrValue, indent, { ...navOrder }, separatorAfter);
insertText += convertObjectToArrayItem(objectText, indent);
} else {
insertText += `${indent}- \${${navOrder.index++}:${arrValue}}\n`;
}
}
return insertText;
} else if (typeof value === 'object') {
Expand Down Expand Up @@ -1933,3 +1938,11 @@ export function addUniquePostfix(uri: string): string {
export function removeUniquePostfix(uri: string): string {
return uri.replace(/(^|\/)_tmp_[0-9a-z]+\//, '$1');
}

export function convertObjectToArrayItem(objectText: string, indent: string): string {
const objectItem = objectText.replace(/^(\s+)/gm, (match, _, index) => {
// first line can contains newLine, so use indent from input parameter
return index === 0 ? `${indent}- ` : `${match} `;
});
return objectItem;
}
84 changes: 84 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { LanguageService } from '../src';
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
import { jigxBranchTest } from './utils/testHelperJigx';
import { convertObjectToArrayItem } from '../src/languageservice/services/yamlCompletion';

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

it('Autocompletion with default value as an object', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
car: {
type: 'object',
default: {
engine: {
fuel: 'gasoline',
},
wheel: 4,
},
},
},
});
const content = 'car: |\n|';
const completion = await parseSetup(content);
expect(completion.items.map((i) => i.insertText)).to.deep.equal([
'\n ${1:engine}:\n ${2:fuel}: ${3:gasoline}\n ${4:wheel}: ${5:4}\n',
]);
});

it('Autocompletion with default value as an array', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
garage: {
type: 'array',
items: {
type: 'object',
},
default: [
{
car: {
engine: { fuel: 'gasoline' },
wheel: [1, 2],
},
},
{
car: {
engine: { fuel: 'diesel' },
},
},
],
},
},
});
const content = 'garage: |\n|';
const completion = await parseSetup(content);
const expected = `
- \${1:car}:
\${2:engine}:
\${3:fuel}: \${4:gasoline}
\${5:wheel}:
- \${6:1}
- \${7:2}
- \${1:car}:
\${2:engine}:
\${3:fuel}: \${4:diesel}
`;
expect(completion.items.map((i) => i.insertText)).to.deep.equal([expected]);
});

it('should convert object to array item', () => {
const objectText = `
car:
engine:
fuel: gasoline
wheel:
- 1
- 2
`;
const expectedArrayItem = ` - car:
engine:
fuel: gasoline
wheel:
- 1
- 2
`;
const arrayItem = convertObjectToArrayItem(objectText, ' ');
expect(arrayItem).to.equal(expectedArrayItem);
});

it('Autocompletion should escape colon when indicating map', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down

0 comments on commit 37ab7f0

Please sign in to comment.