Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed auto completion not suggest const for array #670

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class YamlCompletion {
const hasColon = lineContent.indexOf(':') !== -1;

const nodeParent = doc.getParent(node);

const isArray = nodeParent && isSeq(nodeParent) && nodeParent.items.length > 0 ? true : false;
msivasubramaniaan marked this conversation as resolved.
Show resolved Hide resolved
const matchOriginal = matchingSchemas.find((it) => it.node.internalNode === originalNode && it.schema.properties);
for (const schema of matchingSchemas) {
if (
Expand Down Expand Up @@ -663,7 +663,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, {}, isArray);
msivasubramaniaan marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -1137,10 +1137,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 +1232,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