Skip to content

Commit

Permalink
Implement the respect of maxProperties for arrays ( fixes redhat-deve…
Browse files Browse the repository at this point in the history
…loper#269 )

Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Jul 3, 2020
1 parent b2729c0 commit cf8f438
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
47 changes: 25 additions & 22 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class YAMLCompletion extends JSONCompletion {
});
}

private getPropertyCompletions (schema: ResolvedSchema, doc: Parser.JSONDocument, node: ASTNode, addValue: boolean,
private getPropertyCompletions (schema: ResolvedSchema, doc: Parser.JSONDocument, node: ObjectASTNode, addValue: boolean,
separatorAfter: string, collector: CompletionsCollector, document): void {
const matchingSchemas = doc.getMatchingSchemas(schema.schema);
matchingSchemas.forEach(s => {
Expand All @@ -239,29 +239,32 @@ export class YAMLCompletion extends JSONCompletion {
});
const schemaProperties = s.schema.properties;
if (schemaProperties) {
Object.keys(schemaProperties).forEach((key: string) => {
const propertySchema = schemaProperties[key];
if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
let identCompensation = '';
if (node.parent && node.parent.type === 'array') {
// because there is a slash '-' to prevent the properties generated to have the correct
// indent
const sourceText = document.getText();
const indexOfSlash = sourceText.lastIndexOf('-', node.offset - 1);
if (indexOfSlash > 0) {
// add one space to compensate the '-'
identCompensation = ' ' + sourceText.slice(indexOfSlash + 1, node.offset);
const maxProperties = s.schema.maxProperties;
if (maxProperties === undefined || node.properties === undefined || node.properties.length <= maxProperties) {
Object.keys(schemaProperties).forEach((key: string) => {
const propertySchema = schemaProperties[key];
if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
let identCompensation = '';
if (node.parent && node.parent.type === 'array') {
// because there is a slash '-' to prevent the properties generated to have the correct
// indent
const sourceText = document.getText();
const indexOfSlash = sourceText.lastIndexOf('-', node.offset - 1);
if (indexOfSlash > 0) {
// add one space to compensate the '-'
identCompensation = ' ' + sourceText.slice(indexOfSlash + 1, node.offset);
}
}
collector.add({
kind: CompletionItemKind.Property,
label: key,
insertText: this.getInsertTextForProperty(key, propertySchema, addValue, separatorAfter, identCompensation + '\t'),
insertTextFormat: InsertTextFormat.Snippet,
documentation: propertySchema.description || ''
});
}
collector.add({
kind: CompletionItemKind.Property,
label: key,
insertText: this.getInsertTextForProperty(key, propertySchema, addValue, separatorAfter, identCompensation + '\t'),
insertTextFormat: InsertTextFormat.Snippet,
documentation: propertySchema.description || ''
});
}
});
});
}
}
// Error fix
// If this is a array of string/boolean/number
Expand Down
2 changes: 1 addition & 1 deletion test/autoCompletion.maxproperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ suite('Auto Completion Tests', () => {
}).then(done, done);
});

it('Provide the 3 types when none provided', done => {
it('Provide the 3 types when one is provided', done => {
const content = '- prop1:\n ';
const completion = parseSetup(content, content.length);
completion.then(function (result) {
Expand Down

0 comments on commit cf8f438

Please sign in to comment.