Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: array default value #968

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,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 @@ -1729,3 +1734,11 @@ function evaluateTab1Symbol(value: string): string {
function isParentCompletionItem(item: CompletionItemBase): item is CompletionItem {
return 'parent' in item;
}

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 @@ -21,6 +21,7 @@ import { expect } from 'chai';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { LanguageService } from '../src';
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
import { convertObjectToArrayItem } from '../src/languageservice/services/yamlCompletion';

describe('Auto Completion Tests', () => {
let languageSettingsSetup: ServiceSetup;
Expand Down Expand Up @@ -931,6 +932,89 @@ describe('Auto Completion Tests', () => {
);
});

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