Skip to content

Commit

Permalink
Fix completion of last property when maxProperties is set (#614)
Browse files Browse the repository at this point in the history
* Fix completion of last property when maxProperties is set

* Add test for completion with maxProperties

* Fix test with new behavior from upstream
  • Loading branch information
sfalmo authored Dec 17, 2021
1 parent 8ad37dd commit 5fcaa1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ export class YamlCompletion {
): void {
const matchingSchemas = doc.getMatchingSchemas(schema.schema);
const existingKey = textBuffer.getText(overwriteRange);
const hasColumn = textBuffer.getLineContent(overwriteRange.start.line).indexOf(':') === -1;
const lineContent = textBuffer.getLineContent(overwriteRange.start.line);
const hasOnlyWhitespace = lineContent.trim().length === 0;
const hasColon = lineContent.indexOf(':') !== -1;

const nodeParent = doc.getParent(node);

Expand All @@ -442,7 +444,7 @@ export class YamlCompletion {
maxProperties === undefined ||
node.items === undefined ||
node.items.length < maxProperties ||
isMapContainsEmptyPair(node)
(node.items.length === maxProperties && !hasOnlyWhitespace)
) {
for (const key in schemaProperties) {
if (Object.prototype.hasOwnProperty.call(schemaProperties, key)) {
Expand Down Expand Up @@ -500,7 +502,7 @@ export class YamlCompletion {
}

let insertText = key;
if (!key.startsWith(existingKey) || hasColumn) {
if (!key.startsWith(existingKey) || !hasColon) {
insertText = this.getInsertTextForProperty(
key,
propertySchema,
Expand Down
24 changes: 24 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,30 @@ describe('Auto Completion Tests', () => {
.then(done, done);
});

it('Provide the 2 types when one is provided and the second is typed', (done) => {
const schema = require(path.join(__dirname, './fixtures/testArrayMaxProperties.json'));
languageService.addSchema(SCHEMA_ID, schema);
const content = '- prop1:\n p';
const completion = parseSetup(content, content.length);
completion
.then(function (result) {
assert.equal(result.items.length, 2);
assert.deepEqual(
result.items[0],
createExpectedCompletion('prop2', 'prop2: ', 1, 2, 1, 3, 10, 2, {
documentation: '',
})
);
assert.deepEqual(
result.items[1],
createExpectedCompletion('prop3', 'prop3: ', 1, 2, 1, 3, 10, 2, {
documentation: '',
})
);
})
.then(done, done);
});

it('Provide no completion when maxProperties reached', (done) => {
const schema = require(path.join(__dirname, './fixtures/testArrayMaxProperties.json'));
languageService.addSchema(SCHEMA_ID, schema);
Expand Down

0 comments on commit 5fcaa1b

Please sign in to comment.