Skip to content

Commit 14a26f8

Browse files
committed
json5 insert text, prefer defaults (#48)
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
1 parent 8ebcb78 commit 14a26f8

File tree

11 files changed

+496
-101
lines changed

11 files changed

+496
-101
lines changed

.changeset/fast-steaks-thank.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"codemirror-json-schema": patch
3+
---
4+
5+
fix nested json4 completion bug (#55)
6+
7+
- fix #54, expand properties inside nested objects as expected in json4
8+
- always advance cursor after property completions
9+
- add more test coverage

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
33
"editor.formatOnSave": true,
4-
"files.insertFinalNewline": true
4+
"files.insertFinalNewline": true,
5+
"coverage-gutters.coverageBaseDir": "**",
6+
"coverage-gutters.coverageFileNames": ["clover.xml"]
57
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ This approach allows you to configure the json5 mode and parse linter, as well a
153153
import { EditorState } from "@codemirror/state";
154154
import { linter } from "@codemirror/lint";
155155
import { json5, json5ParseLinter, json5Language } from "codemirror-json5";
156-
import { jsonCompletion } from "codemirror-json-schema";
157156
import {
158157
json5SchemaLinter,
159158
json5SchemaHover,
159+
json5Completion,
160160
} from "codemirror-json-schema/json5";
161161

162162
const schema = {
@@ -182,7 +182,7 @@ const json5State = EditorState.create({
182182
linter(json5SchemaLinter(schema)),
183183
hoverTooltip(json5SchemaHover(schema)),
184184
json5Language.data.of({
185-
autocomplete: jsonCompletion(schema),
185+
autocomplete: json5Completion(schema),
186186
}),
187187
],
188188
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"dev": "vite ./dev --port 3000",
8787
"build": "pnpm tsc && vite build ./dev --outDir ../public --emptyOutDir",
8888
"test": "vitest --dom",
89-
"test:coverage": "vitest run --dom --coverage",
89+
"test:coverage": "vitest run --dom --coverage ",
9090
"tsc": "tsc && pnpm replace:env",
9191
"version-packages": "changeset version && pnpm typedoc && pnpm prettier:write CHANGELOG.md && git add package.json pnpm-lock.yaml CHANGELOG.md",
9292
"release": "pnpm build && changeset publish",

src/__tests__/__fixtures__/schemas.ts

+15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ export const testSchema2 = {
4141
{ $ref: "#/definitions/fancyObject2" },
4242
],
4343
},
44+
arrayOfObjects: {
45+
type: "array",
46+
items: {
47+
$ref: "#/definitions/fancyObject",
48+
},
49+
},
50+
arrayOfOneOf: {
51+
type: "array",
52+
items: {
53+
oneOf: [
54+
{ $ref: "#/definitions/fancyObject" },
55+
{ $ref: "#/definitions/fancyObject2" },
56+
],
57+
},
58+
},
4459
enum1: {
4560
description: "an example enum with default bar",
4661
enum: ["foo", "bar"],

src/__tests__/__helpers__/completion.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, it, expect, vitest, Mock } from "vitest";
22

33
import { json, jsonLanguage } from "@codemirror/lang-json";
4+
import { json5, json5Language } from "codemirror-json5";
5+
46
import { EditorState } from "@codemirror/state";
57
import {
68
Completion,
@@ -38,19 +40,26 @@ type MockedCompletionResult = CompletionResult["options"][0] & {
3840
export async function expectCompletion(
3941
doc: string,
4042
results: MockedCompletionResult[],
41-
schema?: JSONSchema7,
42-
conf: { explicit?: boolean } = {}
43+
44+
conf: {
45+
explicit?: boolean;
46+
schema?: JSONSchema7;
47+
mode?: "json" | "json5";
48+
} = {}
4349
) {
4450
let cur = doc.indexOf("|"),
45-
currentSchema = schema || testSchema2;
51+
currentSchema = conf?.schema || testSchema2;
4652
doc = doc.slice(0, cur) + doc.slice(cur + 1);
53+
const jsonMode = conf?.mode === "json5" ? json5 : json;
54+
const jsonLang = conf?.mode === "json5" ? json5Language : jsonLanguage;
55+
4756
let state = EditorState.create({
4857
doc,
4958
selection: { anchor: cur },
5059
extensions: [
51-
json(),
52-
jsonLanguage.data.of({
53-
autocomplete: jsonCompletion(currentSchema),
60+
jsonMode(),
61+
jsonLang.data.of({
62+
autocomplete: jsonCompletion(currentSchema, { mode: conf.mode }),
5463
}),
5564
],
5665
});

0 commit comments

Comments
 (0)