Skip to content

Commit

Permalink
fix autocomplete for schema with top level complex type
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed May 29, 2024
1 parent 2c97572 commit b191d5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 25 additions & 5 deletions src/__tests__/json-completion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/json-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b191d5e

Please sign in to comment.