From b191d5e549f770e5d1ed116d43e9ac680b8823a9 Mon Sep 17 00:00:00 2001 From: Samuel Imolorhe Date: Wed, 29 May 2024 15:25:04 +0200 Subject: [PATCH] fix autocomplete for schema with top level complex type --- src/__tests__/json-completion.spec.ts | 30 ++++++++++++++++++++++----- src/json-completion.ts | 4 +++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/__tests__/json-completion.spec.ts b/src/__tests__/json-completion.spec.ts index 16bd661..8b36be3 100644 --- a/src/__tests__/json-completion.spec.ts +++ b/src/__tests__/json-completion.spec.ts @@ -2,7 +2,7 @@ import { describe, it } from "vitest"; import { expectCompletion } from "./__helpers__/completion.js"; import { MODES } from "../constants.js"; -import { testSchema3 } from "./__fixtures__/schemas.js"; +import { testSchema3, testSchema4 } from "./__fixtures__/schemas.js"; describe.each([ { @@ -143,10 +143,8 @@ describe.each([ // TODO: should provide true/false completions // { // name: "include value completions for boolean", - // mode: MODES.JSON, - // docs: ['{ "booleanWithDefault": | }', - // }, - // ], + // mode: MODES.JSON, + // docs: ['{ "booleanWithDefault": | }'], // expectedResults: [ // { // detail: "boolean", @@ -364,6 +362,28 @@ describe.each([ ], schema: testSchema3, }, + { + name: "autocomplete for a schema with top level complex type", + mode: MODES.JSON, + docs: ['{ "| }'], + expectedResults: [ + { + type: "property", + detail: "string", + info: "", + label: "foo", + template: '"foo": "#{}"', + }, + { + type: "property", + detail: "number", + info: "", + label: "bar", + template: '"bar": #{0}', + }, + ], + schema: testSchema4, + }, // JSON5 { name: "return bare property key when no quotes are used", diff --git a/src/json-completion.ts b/src/json-completion.ts index 08def88..bcabf61 100644 --- a/src/json-completion.ts +++ b/src/json-completion.ts @@ -270,6 +270,7 @@ export class JSONCompletion { // Get matching schemas const schemas = this.getSchemas(schema, ctx); + debug.log("xxx", "propertyCompletion schemas", schemas); schemas.forEach((s) => { if (typeof s !== "object") { @@ -814,8 +815,9 @@ export class JSONCompletion { debug.log("xxx", "pointer..", JSON.stringify(pointer)); // For some reason, it returns undefined schema for the root pointer + // We use the root schema in that case as the relevant (sub)schema if (!pointer || pointer === "/") { - return [schema]; + subSchema = this.expandSchemaProperty(schema, schema) ?? schema; } // const subSchema = new Draft07(this.schema).getSchema(pointer); debug.log("xxx", "subSchema..", subSchema);