Skip to content

Commit

Permalink
json5 insert text, prefer defaults (#48)
Browse files Browse the repository at this point in the history
this allows us to:

- automatically complete the json5 string based on the type of quotes or
lack thereof that the user is using
- prefers the schema.default over other guesses
- fixes an apparent bug where guessed solutions are overwritten

I tested this with `packageJson.types`, which is the only value in the
schema that has a default
  • Loading branch information
acao committed Aug 12, 2023
1 parent 8ebcb78 commit 14a26f8
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 101 deletions.
9 changes: 9 additions & 0 deletions .changeset/fast-steaks-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"codemirror-json-schema": patch
---

fix nested json4 completion bug (#55)

- fix #54, expand properties inside nested objects as expected in json4
- always advance cursor after property completions
- add more test coverage
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"files.insertFinalNewline": true
"files.insertFinalNewline": true,
"coverage-gutters.coverageBaseDir": "**",
"coverage-gutters.coverageFileNames": ["clover.xml"]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ This approach allows you to configure the json5 mode and parse linter, as well a
import { EditorState } from "@codemirror/state";
import { linter } from "@codemirror/lint";
import { json5, json5ParseLinter, json5Language } from "codemirror-json5";
import { jsonCompletion } from "codemirror-json-schema";
import {
json5SchemaLinter,
json5SchemaHover,
json5Completion,
} from "codemirror-json-schema/json5";

const schema = {
Expand All @@ -182,7 +182,7 @@ const json5State = EditorState.create({
linter(json5SchemaLinter(schema)),
hoverTooltip(json5SchemaHover(schema)),
json5Language.data.of({
autocomplete: jsonCompletion(schema),
autocomplete: json5Completion(schema),
}),
],
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"dev": "vite ./dev --port 3000",
"build": "pnpm tsc && vite build ./dev --outDir ../public --emptyOutDir",
"test": "vitest --dom",
"test:coverage": "vitest run --dom --coverage",
"test:coverage": "vitest run --dom --coverage ",
"tsc": "tsc && pnpm replace:env",
"version-packages": "changeset version && pnpm typedoc && pnpm prettier:write CHANGELOG.md && git add package.json pnpm-lock.yaml CHANGELOG.md",
"release": "pnpm build && changeset publish",
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/__fixtures__/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ export const testSchema2 = {
{ $ref: "#/definitions/fancyObject2" },
],
},
arrayOfObjects: {
type: "array",
items: {
$ref: "#/definitions/fancyObject",
},
},
arrayOfOneOf: {
type: "array",
items: {
oneOf: [
{ $ref: "#/definitions/fancyObject" },
{ $ref: "#/definitions/fancyObject2" },
],
},
},
enum1: {
description: "an example enum with default bar",
enum: ["foo", "bar"],
Expand Down
21 changes: 15 additions & 6 deletions src/__tests__/__helpers__/completion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, it, expect, vitest, Mock } from "vitest";

import { json, jsonLanguage } from "@codemirror/lang-json";
import { json5, json5Language } from "codemirror-json5";

import { EditorState } from "@codemirror/state";
import {
Completion,
Expand Down Expand Up @@ -38,19 +40,26 @@ type MockedCompletionResult = CompletionResult["options"][0] & {
export async function expectCompletion(
doc: string,
results: MockedCompletionResult[],
schema?: JSONSchema7,
conf: { explicit?: boolean } = {}

conf: {
explicit?: boolean;
schema?: JSONSchema7;
mode?: "json" | "json5";
} = {}
) {
let cur = doc.indexOf("|"),
currentSchema = schema || testSchema2;
currentSchema = conf?.schema || testSchema2;
doc = doc.slice(0, cur) + doc.slice(cur + 1);
const jsonMode = conf?.mode === "json5" ? json5 : json;
const jsonLang = conf?.mode === "json5" ? json5Language : jsonLanguage;

let state = EditorState.create({
doc,
selection: { anchor: cur },
extensions: [
json(),
jsonLanguage.data.of({
autocomplete: jsonCompletion(currentSchema),
jsonMode(),
jsonLang.data.of({
autocomplete: jsonCompletion(currentSchema, { mode: conf.mode }),
}),
],
});
Expand Down
Loading

0 comments on commit 14a26f8

Please sign in to comment.