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 completion indentation for object in array #332

Merged
merged 2 commits into from
Oct 21, 2020
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
20 changes: 5 additions & 15 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class YAMLCompletion extends JSONCompletion {
addValue: boolean,
separatorAfter: string,
collector: CompletionsCollector,
document
document: TextDocument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you know that TextDocument is deprecated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we use it across all project.

): void {
const matchingSchemas = doc.getMatchingSchemas(schema.schema);
matchingSchemas.forEach((s) => {
Expand Down Expand Up @@ -723,12 +723,7 @@ export class YAMLCompletion extends JSONCompletion {
break;
case 'array':
{
const arrayInsertResult = this.getInsertTextForArray(
propertySchema.items,
separatorAfter,
`${indent}${this.indentation}`,
insertIndex++
);
const arrayInsertResult = this.getInsertTextForArray(propertySchema.items, separatorAfter, insertIndex++);
insertIndex = arrayInsertResult.insertIndex;
insertText += `${indent}${key}:\n${indent}${this.indentation}- ${arrayInsertResult.insertText}\n`;
}
Expand Down Expand Up @@ -769,7 +764,7 @@ export class YAMLCompletion extends JSONCompletion {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForArray(schema: any, separatorAfter: string, indent = this.indentation, insertIndex = 1): InsertText {
private getInsertTextForArray(schema: any, separatorAfter: string, insertIndex = 1): InsertText {
let insertText = '';
if (!schema) {
insertText = `$${insertIndex++}`;
Expand Down Expand Up @@ -797,12 +792,7 @@ export class YAMLCompletion extends JSONCompletion {
break;
case 'object':
{
const objectInsertResult = this.getInsertTextForObject(
schema,
separatorAfter,
`${indent}${this.indentation}`,
insertIndex++
);
const objectInsertResult = this.getInsertTextForObject(schema, separatorAfter, `${this.indentation} `, insertIndex++);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to also make this change for line 736 above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

insertText = objectInsertResult.insertText.trimLeft();
insertIndex = objectInsertResult.insertIndex;
}
Expand Down Expand Up @@ -864,7 +854,7 @@ export class YAMLCompletion extends JSONCompletion {
return `${resultText}\n${this.getInsertTextForObject(propertySchema, separatorAfter, ident).insertText}`;
} else if (propertySchema.items) {
return `${resultText}\n${this.indentation}- ${
this.getInsertTextForArray(propertySchema.items, separatorAfter, ident).insertText
this.getInsertTextForArray(propertySchema.items, separatorAfter).insertText
}`;
}
if (nValueProposals === 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ connection.onDidChangeConfiguration((change) => {

schemaConfigurationSettings = [];

if (settings.editor?.tabSize) {
if (settings['[yaml]'] && settings['[yaml]']['editor.tabSize']) {
indentation = ' '.repeat(settings['[yaml]']['editor.tabSize']);
JPinkney marked this conversation as resolved.
Show resolved Hide resolved
} else if (settings.editor?.tabSize) {
indentation = ' '.repeat(settings.editor.tabSize);
}

Expand Down
156 changes: 156 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,162 @@ suite('Auto Completion Tests', () => {
})
.then(done, done);
});

it('Array of objects autocomplete with 4 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
metadata: {
type: 'object',
properties: {
ownerReferences: {
type: 'array',
items: {
type: 'object',
properties: {
apiVersion: {
type: 'string',
},
kind: {
type: 'string',
},
name: {
type: 'string',
},
uid: {
type: 'string',
},
},
required: ['apiVersion', 'kind', 'name', 'uid'],
},
},
},
},
},
});

const content = 'metadata:\n ownerReferences';
const completion = await parseSetup(content, 29);
expect(completion.items[0]).deep.eq(
createExpectedCompletion(
'ownerReferences',
'ownerReferences:\n - apiVersion: $1\n kind: $2\n name: $3\n uid: $4',
1,
4,
1,
19,
10,
2,
{ documentation: '' }
)
);
});
});

it('Array of objects autocomplete with 2 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
metadata: {
type: 'object',
properties: {
ownerReferences: {
type: 'array',
items: {
type: 'object',
properties: {
apiVersion: {
type: 'string',
},
kind: {
type: 'string',
},
name: {
type: 'string',
},
uid: {
type: 'string',
},
},
required: ['apiVersion', 'kind', 'name', 'uid'],
},
},
},
},
},
});

const content = 'metadata:\n ownerReferences';
const completion = await parseSetup(content, 27);
expect(completion.items[0]).deep.eq(
createExpectedCompletion(
'ownerReferences',
'ownerReferences:\n - apiVersion: $1\n kind: $2\n name: $3\n uid: $4',
1,
2,
1,
17,
10,
2,
{ documentation: '' }
)
);

it('Array of objects autocomplete with 3 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
metadata: {
type: 'object',
properties: {
ownerReferences: {
type: 'array',
items: {
type: 'object',
properties: {
apiVersion: {
type: 'string',
},
kind: {
type: 'string',
},
name: {
type: 'string',
},
uid: {
type: 'string',
},
},
required: ['apiVersion', 'kind', 'name', 'uid'],
},
},
},
},
},
});

const content = 'metadata:\n ownerReferences';
const completion = await parseSetup(content, 27);
expect(completion.items[0]).deep.eq(
createExpectedCompletion(
'ownerReferences',
'ownerReferences:\n - apiVersion: $1\n kind: $2\n name: $3\n uid: $4',
1,
2,
1,
17,
10,
2,
{ documentation: '' }
)
);
});
});

describe('JSON Schema 7 Specific Tests', function () {
Expand Down