Skip to content

Commit

Permalink
fix: array indent on different index position (#635)
Browse files Browse the repository at this point in the history
Co-authored-by: Petr Spacek <[email protected]>
  • Loading branch information
p-spacek and Petr Spacek authored Jan 12, 2022
1 parent 4a36b9b commit 47c840a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export class YamlCompletion {

if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
let identCompensation = '';
if (nodeParent && isSeq(nodeParent) && node.items.length <= 1) {
if (nodeParent && isSeq(nodeParent) && node.items.length <= 1 && !hasOnlyWhitespace) {
// because there is a slash '-' to prevent the properties generated to have the correct
// indent
const sourceText = textBuffer.getText();
Expand Down
54 changes: 54 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,58 @@ objB:
})
);
});
describe('array indent on different index position', () => {
const schema = {
type: 'object',
properties: {
objectWithArray: {
type: 'array',
items: {
type: 'object',
required: ['item', 'item2'],
properties: {
item: { type: 'string' },
item2: {
type: 'object',
required: ['prop1', 'prop2'],
properties: {
prop1: { type: 'string' },
prop2: { type: 'string' },
},
},
},
},
},
},
};
it('array indent on the first item', async () => {
languageService.addSchema(SCHEMA_ID, schema);
const content = 'objectWithArray:\n - ';
const completion = await parseSetup(content, 1, 4);

expect(completion.items.length).equal(2);
expect(completion.items[0]).to.be.deep.equal(
createExpectedCompletion('item', 'item: ', 1, 4, 1, 4, 10, 2, {
documentation: '',
})
);
expect(completion.items[1]).to.be.deep.equal(
createExpectedCompletion('item2', 'item2:\n prop1: $1\n prop2: $2', 1, 4, 1, 4, 10, 2, {
documentation: '',
})
);
});
it('array indent on the second item', async () => {
languageService.addSchema(SCHEMA_ID, schema);
const content = 'objectWithArray:\n - item: first line\n ';
const completion = await parseSetup(content, 2, 4);

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

0 comments on commit 47c840a

Please sign in to comment.