From 8d889746fc080f0d7d1d022488fdecf60ca485f3 Mon Sep 17 00:00:00 2001 From: KapitanOczywisty <44417092+KapitanOczywisty@users.noreply.github.com> Date: Thu, 16 Jul 2020 10:48:12 +0200 Subject: [PATCH 1/2] Support for defaultSnippets in patternProperties and additionalProperties --- src/services/jsonCompletion.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/services/jsonCompletion.ts b/src/services/jsonCompletion.ts index c03fa834..a117dbf4 100644 --- a/src/services/jsonCompletion.ts +++ b/src/services/jsonCompletion.ts @@ -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); } } From 31aa45bf6a7ff46dad72db2cf010459ba7ba437e Mon Sep 17 00:00:00 2001 From: KapitanOczywisty <44417092+KapitanOczywisty@users.noreply.github.com> Date: Thu, 16 Jul 2020 10:51:14 +0200 Subject: [PATCH 2/2] Tests for defaultSnippets in patternProperties and additionalProperties --- src/test/completion.test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/test/completion.test.ts b/src/test/completion.test.ts index ef4b83ae..6b023b58 100644 --- a/src/test/completion.test.ts +++ b/src/test/completion.test.ts @@ -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',