Skip to content

Commit

Permalink
fix: doNotSuggest for property and value completion
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed May 29, 2024
1 parent 7203630 commit 769f53f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@ export class YamlCompletion {
});
}
for (const schema of matchingSchemas) {
if (schema.schema.doNotSuggest) {
continue;
}

if (
((schema.node.internalNode === node && !matchOriginal) ||
(schema.node.internalNode === originalNode && !hasColon) ||
Expand All @@ -716,7 +720,7 @@ export class YamlCompletion {
if (Object.prototype.hasOwnProperty.call(schemaProperties, key)) {
const propertySchema = schemaProperties[key];

if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema.doNotSuggest) {
let identCompensation = '';
if (nodeParent && isSeq(nodeParent) && node.items.length <= 1 && !hasOnlyWhitespace) {
// because there is a slash '-' to prevent the properties generated to have the correct
Expand Down Expand Up @@ -1309,6 +1313,10 @@ export class YamlCompletion {
isArray?: boolean
): void {
if (typeof schema === 'object') {
if (schema.doNotSuggest) {
return;
}

this.addEnumValueCompletions(schema, separatorAfter, collector, isArray);
this.addDefaultValueCompletions(schema, separatorAfter, collector);
this.collectTypes(schema, types);
Expand Down
66 changes: 66 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,4 +1329,70 @@ test1:
expect(completion.items[0].insertText).to.be.equal('${1:property}: ');
expect(completion.items[0].documentation).to.be.equal('Property Description');
});

describe('doNotSuggest schema', () => {
it('should not autocomplete schema with doNotSuggest - property completion', async () => {
const schema: JSONSchema = {
properties: {
prop1: { type: 'string' },
},
doNotSuggest: true,
};
schemaProvider.addSchema(SCHEMA_ID, schema);
const content = '';
const completion = await parseSetup(content, 0, 1);

expect(completion.items.length).equal(0);
});
it('should not autocomplete schema with doNotSuggest - value completion', async () => {
const schema: JSONSchema = {
properties: {
prop1: {
anyOf: [
{
type: 'string',
default: 'value_default',
doNotSuggest: true,
},
{
type: 'object',
defaultSnippets: [
{
label: 'snippet',
body: {
value1: 'value_snippet',
},
},
],
doNotSuggest: true,
},
],
},
},
};
schemaProvider.addSchema(SCHEMA_ID, schema);
const content = 'prop1: ';
const completion = await parseSetup(content, 0, content.length);

expect(completion.items.length).equal(0);
});
it('should autocomplete inside schema in doNotSuggest', async () => {
const schema: JSONSchema = {
properties: {
obj1: {
properties: {
item1: { type: 'string' },
},
},
},
doNotSuggest: true,
};
schemaProvider.addSchema(SCHEMA_ID, schema);
const content = 'obj1:\n | |';
const completion = await parseCaret(content);

expect(completion.items.length).equal(1);
expect(completion.items[0].label).equal('item1');
});
});
});

0 comments on commit 769f53f

Please sign in to comment.