Skip to content

Commit

Permalink
Merge pull request #670 from msivasubramaniaan/fix-autoCompletion-arr…
Browse files Browse the repository at this point in the history
…ay-should-not-suggest-const

fixed auto completion not suggest const for array
  • Loading branch information
msivasubramaniaan authored Mar 13, 2022
2 parents 072b42f + ba09954 commit 7a2cb7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ export class YamlCompletion {
const hasColon = lineContent.indexOf(':') !== -1;

const nodeParent = doc.getParent(node);

const matchOriginal = matchingSchemas.find((it) => it.node.internalNode === originalNode && it.schema.properties);
for (const schema of matchingSchemas) {
if (
Expand Down Expand Up @@ -663,7 +662,7 @@ export class YamlCompletion {
// - item1
// it will treated as a property key since `:` has been appended
if (nodeParent && isSeq(nodeParent) && schema.schema.type !== 'object') {
this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {});
this.addSchemaValueCompletions(schema.schema, separatorAfter, collector, {}, Array.isArray(nodeParent.items));
}
}

Expand Down Expand Up @@ -1137,10 +1136,11 @@ export class YamlCompletion {
schema: JSONSchemaRef,
separatorAfter: string,
collector: CompletionsCollector,
types: unknown
types: unknown,
isArray?: boolean
): void {
if (typeof schema === 'object') {
this.addEnumValueCompletions(schema, separatorAfter, collector);
this.addEnumValueCompletions(schema, separatorAfter, collector, isArray);
this.addDefaultValueCompletions(schema, separatorAfter, collector);
this.collectTypes(schema, types);
if (Array.isArray(schema.allOf)) {
Expand Down Expand Up @@ -1231,8 +1231,13 @@ export class YamlCompletion {
}
}

private addEnumValueCompletions(schema: JSONSchema, separatorAfter: string, collector: CompletionsCollector): void {
if (isDefined(schema.const)) {
private addEnumValueCompletions(
schema: JSONSchema,
separatorAfter: string,
collector: CompletionsCollector,
isArray: boolean
): void {
if (isDefined(schema.const) && !isArray) {
collector.add({
kind: this.getSuggestionKind(schema.type),
label: this.getLabelForValue(schema.const),
Expand Down
23 changes: 23 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,29 @@ describe('Auto Completion Tests', () => {
);
});

it('Array completion - should not suggest const', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
test: {
type: 'array',
items: {
type: 'object',
properties: {
constProp: {
type: 'string',
const: 'const1',
},
},
},
},
},
});
const content = 'test:\n - constProp:\n ';
const result = await parseSetup(content, content.length);
expect(result.items.length).to.be.equal(0);
});

it('Object in array with 4 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
Expand Down

0 comments on commit 7a2cb7e

Please sign in to comment.