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

Add support for defaultSnippets in patternProperties and additionalProperties #62

Merged
merged 2 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/services/jsonCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,27 @@ export class JSONCompletion {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types);
}
}
if (s.schema.properties && parentKey !== undefined) {
const propertySchema = s.schema.properties[parentKey];
if (propertySchema) {
if (parentKey !== undefined) {
let propertyMatched = false;
if (s.schema.properties) {
const propertySchema = s.schema.properties[parentKey];
if (propertySchema) {
propertyMatched = true;
this.addSchemaValueCompletions(propertySchema, separatorAfter, collector, types);
}
}
if (s.schema.patternProperties && !propertyMatched) {
for (const pattern of Object.keys(s.schema.patternProperties)) {
const regex = new RegExp(pattern);
if (regex.test(parentKey)) {
propertyMatched = true;
const propertySchema = s.schema.patternProperties[pattern];
this.addSchemaValueCompletions(propertySchema, separatorAfter, collector, types);
}
}
}
if (s.schema.additionalProperties && !propertyMatched) {
const propertySchema = s.schema.additionalProperties;
this.addSchemaValueCompletions(propertySchema, separatorAfter, collector, types);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,40 @@ suite('JSON Completion', () => {
});
});

test('Default snippets for complex properties', async function () {
const schema: JSONSchema = {
type: 'object',
properties: {
property_test: {
type: 'object',
defaultSnippets: [ { label: 'property snippet', bodyText: '{}' } ]
}
},
patternProperties: {
pattern: {
type: 'object',
defaultSnippets: [ { label: 'pattern snippet', bodyText: '{}' } ]
}
},
additionalProperties: {
type: 'object',
defaultSnippets: [ { label: 'additional snippet', bodyText: '{}' } ]
}
};

await testCompletionsFor('{"property_test":|', schema, {
items: [{ label: 'property snippet'}]
});

await testCompletionsFor('{"pattern_test":|', schema, {
items: [{ label: 'pattern snippet'}]
});

await testCompletionsFor('{"additional_test":|', schema, {
items: [{ label: 'additional snippet'}]
});
});

test('Deprecation message', async function () {
const schema: JSONSchema = {
type: 'object',
Expand Down