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 indent doesn't work properly inside another array #634

Merged
merged 2 commits into from
Jan 11, 2022
Merged
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
18 changes: 9 additions & 9 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export class YamlCompletion {
key: string,
propertySchema: JSONSchema,
separatorAfter: string,
ident = this.indentation
indent = this.indentation
): string {
const propertyText = this.getInsertTextForValue(key, '', 'string');
const resultText = propertyText + ':';
Expand Down Expand Up @@ -733,10 +733,10 @@ export class YamlCompletion {
nValueProposals += propertySchema.examples.length;
}
if (propertySchema.properties) {
return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, ident).insertText}`;
return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, indent).insertText}`;
} else if (propertySchema.items) {
return `${resultText}\n${this.indentation}- ${
this.getInsertTextForArray(propertySchema.items, separatorAfter).insertText
return `${resultText}\n${indent}- ${
this.getInsertTextForArray(propertySchema.items, separatorAfter, 1, indent).insertText
}`;
}
if (nValueProposals === 0) {
Expand All @@ -748,10 +748,10 @@ export class YamlCompletion {
value = ' $1';
break;
case 'object':
value = `\n${ident}`;
value = `\n${indent}`;
break;
case 'array':
value = `\n${ident}- `;
value = `\n${indent}- `;
break;
case 'number':
case 'integer':
Expand Down Expand Up @@ -804,7 +804,7 @@ export class YamlCompletion {
break;
case 'array':
{
const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++);
const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++, indent);
const arrayInsertLines = arrayInsertResult.insertText.split('\n');
let arrayTemplate = arrayInsertResult.insertText;
if (arrayInsertLines.length > 1) {
Expand Down Expand Up @@ -856,7 +856,7 @@ export class YamlCompletion {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1): InsertText {
private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1, indent = this.indentation): InsertText {
let insertText = '';
if (!schema) {
insertText = `$${insertIndex++}`;
Expand Down Expand Up @@ -884,7 +884,7 @@ export class YamlCompletion {
break;
case 'object':
{
const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${this.indentation} `, insertIndex++);
const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${indent} `, insertIndex++);
insertText = objectInsertResult.insertText.trimLeft();
insertIndex = objectInsertResult.insertIndex;
}
Expand Down
73 changes: 73 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe('Auto Completion Fix Tests', () => {
yamlSettings = settings;
});

/**
*
* @param content
* @param line starts with 0 index
* @param character starts with 1 index
* @returns
*/
function parseSetup(content: string, line: number, character: number): Promise<CompletionList> {
const testTextDocument = setupSchemaIDTextDocument(content);
yamlSettings.documents = new TextDocumentTestManager();
Expand Down Expand Up @@ -256,4 +263,70 @@ objB:
})
);
});

it('Autocomplete indent on array when parent is array', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
examples: {
type: 'array',
items: {
type: 'object',
properties: {
objectWithArray: {
type: 'array',
items: {
type: 'string',
},
},
},
},
},
},
});
const content = 'examples:\n - ';
const completion = await parseSetup(content, 1, 4);

expect(completion.items.length).equal(1);
expect(completion.items[0]).to.be.deep.equal(
createExpectedCompletion('objectWithArray', 'objectWithArray:\n - ${1:""}', 1, 4, 1, 4, 10, 2, {
documentation: '',
})
);
});
it('Autocomplete indent on array object when parent is array', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
examples: {
type: 'array',
items: {
type: 'object',
properties: {
objectWithArray: {
type: 'array',
items: {
type: 'object',
required: ['item', 'item2'],
properties: {
item: { type: 'string' },
item2: { type: 'string' },
},
},
},
},
},
},
},
});
const content = 'examples:\n - ';
const completion = await parseSetup(content, 1, 4);

expect(completion.items.length).equal(1);
expect(completion.items[0]).to.be.deep.equal(
createExpectedCompletion('objectWithArray', 'objectWithArray:\n - item: $1\n item2: $2', 1, 4, 1, 4, 10, 2, {
documentation: '',
})
);
});
});