Skip to content

Commit a08101a

Browse files
committed
simpler, bundled extension exports
1 parent b2bc859 commit a08101a

9 files changed

+153
-39
lines changed

.changeset/wicked-llamas-confess.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"codemirror-json-schema": patch
3+
---
4+
5+
simpler export patterns

README.md

+67-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ npm install --save @codemirror/lang-json codemirror-json-schema @codemirror/lang
4646

4747
#### Minimal Usage
4848

49+
This sets up `@codemirror/lang-json` and our extension for you.
50+
If you'd like to have more control over the related configurations, see custom usage below
51+
52+
```ts
53+
import { EditorState } from "@codemirror/state";
54+
import { jsonSchema } from "codemirror-json-schema";
55+
56+
const schema = {
57+
type: "object",
58+
properties: {
59+
example: {
60+
type: "boolean",
61+
},
62+
},
63+
};
64+
65+
const json5State = EditorState.create({
66+
doc: "{ example: true }",
67+
extensions: [jsonSchema(schema)],
68+
});
69+
```
70+
71+
#### Custom Usage
72+
73+
This approach allows you to configure the json mode and parse linter, as well as our linter, hovers, etc more specifically.
74+
4975
```ts
5076
import { EditorState } from "@codemirror/state";
5177
import { linter } from "@codemirror/lint";
@@ -65,7 +91,10 @@ const state = EditorState.create({
6591
doc: `{ "example": true }`,
6692
extensions: [
6793
json(),
68-
linter(jsonParseLinter()),
94+
linter(jsonParseLinter(), {
95+
// default is 750ms
96+
delay: 300
97+
}),
6998
linter(jsonSchemaLinter(schema)),
7099
jsonLanguage.data.of({
71100
autocomplete: jsonCompletion(schema),
@@ -91,6 +120,35 @@ npm install --save codemirror-json5 codemirror-json-schema @codemirror/language
91120

92121
#### Minimal Usage
93122

123+
This sets up `codemirror-json5` mode for you.
124+
If you'd like to have more control over the related configurations, see custom usage below
125+
126+
```ts
127+
import { EditorState } from "@codemirror/state";
128+
import { json5Schema } from "codemirror-json-schema/json5";
129+
130+
const schema = {
131+
type: "object",
132+
properties: {
133+
example: {
134+
type: "boolean",
135+
},
136+
},
137+
};
138+
139+
const json5State = EditorState.create({
140+
doc: `{
141+
example: true,
142+
// json5 is awesome!
143+
}`,
144+
extensions: [json5Schema(schema)],
145+
});
146+
```
147+
148+
#### Custom Usage
149+
150+
This approach allows you to configure the json5 mode and parse linter, as well as our linter, hovers, etc more specifically.
151+
94152
```ts
95153
import { EditorState } from "@codemirror/state";
96154
import { linter } from "@codemirror/lint";
@@ -111,10 +169,16 @@ const schema = {
111169
};
112170

113171
const json5State = EditorState.create({
114-
doc: `{ example: true }`,
172+
doc: `{
173+
example: true,
174+
// json5 is awesome!
175+
}`,
115176
extensions: [
116177
json5(),
117-
linter(json5ParseLinter()),
178+
linter(json5ParseLinter(), {
179+
// the default linting delay is 750ms
180+
delay: 300,
181+
}),
118182
linter(json5SchemaLinter(schema)),
119183
hoverTooltip(json5SchemaHover(schema)),
120184
json5Language.data.of({

dev/index.ts

+17-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { EditorState } from "@codemirror/state";
2-
import {
3-
gutter,
4-
EditorView,
5-
lineNumbers,
6-
hoverTooltip,
7-
} from "@codemirror/view";
2+
import { gutter, EditorView, lineNumbers } from "@codemirror/view";
83
import { basicSetup } from "codemirror";
94
import { history } from "@codemirror/commands";
105
import { autocompletion, closeBrackets } from "@codemirror/autocomplete";
11-
import { linter, lintGutter } from "@codemirror/lint";
6+
import { lintGutter } from "@codemirror/lint";
127
import { bracketMatching, syntaxHighlighting } from "@codemirror/language";
138
import { oneDarkHighlightStyle, oneDark } from "@codemirror/theme-one-dark";
149
import { JSONSchema7 } from "json-schema";
@@ -18,15 +13,17 @@ import { jsonText, json5Text } from "./sample-text";
1813
import packageJsonSchema from "./package.schema.json";
1914

2015
// json4
21-
import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json";
22-
import { jsonSchemaLinter, jsonSchemaHover, jsonCompletion } from "../src";
16+
import { jsonSchema } from "../src";
2317

2418
// json5
25-
import { json5, json5Language, json5ParseLinter } from "codemirror-json5";
26-
import { json5SchemaLinter, json5SchemaHover } from "../src/json5";
19+
import { json5Schema } from "../src/json5";
2720

2821
const schema = packageJsonSchema as JSONSchema7;
2922

23+
/**
24+
* none of these are required for json4 or 5
25+
* but they will improve the DX
26+
*/
3027
const commonExtensions = [
3128
gutter({ class: "CodeMirror-lint-markers" }),
3229
bracketMatching(),
@@ -42,37 +39,26 @@ const commonExtensions = [
4239
syntaxHighlighting(oneDarkHighlightStyle),
4340
];
4441

42+
/**
43+
* json4!
44+
*/
45+
4546
const state = EditorState.create({
4647
doc: jsonText,
47-
extensions: [
48-
...commonExtensions,
49-
json(),
50-
linter(jsonParseLinter()),
51-
linter(jsonSchemaLinter(schema)),
52-
jsonLanguage.data.of({
53-
autocomplete: jsonCompletion(schema),
54-
}),
55-
hoverTooltip(jsonSchemaHover(schema)),
56-
],
48+
extensions: [commonExtensions, jsonSchema(schema)],
5749
});
5850

5951
new EditorView({
6052
state,
6153
parent: document.querySelector("#editor")!,
6254
});
6355

56+
/**
57+
* json5!
58+
*/
6459
const json5State = EditorState.create({
6560
doc: json5Text,
66-
extensions: [
67-
...commonExtensions,
68-
json5(),
69-
linter(json5ParseLinter()),
70-
linter(json5SchemaLinter(schema)),
71-
json5Language.data.of({
72-
autocomplete: jsonCompletion(schema),
73-
}),
74-
hoverTooltip(json5SchemaHover(schema)),
75-
],
61+
extensions: [commonExtensions, json5Schema(schema)],
7662
});
7763

7864
new EditorView({

lefthook.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pre-commit:
1212
run: pnpm prettier:write {staged_files}
1313
stage_fixed: true
1414
test:
15-
run: pnpm vitest related --run --dom {staged_files}
15+
run: pnpm vitest --run --dom related {staged_files}
1616
glob: "*.{js,ts,jsx,tsx}"
1717
fail_text: Tests failed or not found on {staged_files}
1818
check:

src/bundled.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { JSONSchema7 } from "json-schema";
2+
import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json";
3+
import { hoverTooltip } from "@codemirror/view";
4+
import { jsonCompletion } from "./json-completion";
5+
import { jsonSchemaLinter } from "./json-validation";
6+
import { jsonSchemaHover } from "./json-hover";
7+
8+
import { linter } from "@codemirror/lint";
9+
10+
/**
11+
* Full featured cm6 extension for json, including `@codemirror/lang-json`
12+
* @group Bundled Codemirror Extensions
13+
*/
14+
export function jsonSchema(schema: JSONSchema7) {
15+
return [
16+
json(),
17+
linter(jsonParseLinter()),
18+
linter(jsonSchemaLinter(schema)),
19+
jsonLanguage.data.of({
20+
autocomplete: jsonCompletion(schema),
21+
}),
22+
hoverTooltip(jsonSchemaHover(schema)),
23+
];
24+
}

src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
export { jsonCompletion } from "./json-completion";
2+
23
export {
34
jsonSchemaLinter,
45
type JSONValidationOptions,
56
} from "./json-validation";
7+
68
export {
79
jsonSchemaHover,
810
type HoverOptions,
911
type FoundCursorData,
1012
type CursorData,
1113
} from "./json-hover";
1214

15+
export { jsonSchema } from "./bundled";
16+
1317
export type {
1418
JSONPointersMap,
1519
JSONPointerData,
1620
JSONPartialPointerData,
1721
} from "./types";
1822

19-
/**
20-
* @group Utilities
21-
*/
2223
export * from "./utils/parseJSONDocument";
2324
export * from "./utils/jsonPointers";

src/json5-bundled.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { JSONSchema7 } from "json-schema";
2+
import { json5, json5Language, json5ParseLinter } from "codemirror-json5";
3+
import { hoverTooltip } from "@codemirror/view";
4+
import { jsonCompletion } from "./json-completion";
5+
import { json5SchemaLinter } from "./json5-validation";
6+
import { json5SchemaHover } from "./json5-hover";
7+
8+
import { linter } from "@codemirror/lint";
9+
10+
/**
11+
* Full featured cm6 extension for json5, including `codemirror-json5`
12+
* @group Bundled Codemirror Extensions
13+
*/
14+
export function json5Schema(schema: JSONSchema7) {
15+
return [
16+
json5(),
17+
linter(json5ParseLinter()),
18+
linter(json5SchemaLinter(schema)),
19+
json5Language.data.of({
20+
autocomplete: jsonCompletion(schema),
21+
}),
22+
hoverTooltip(json5SchemaHover(schema)),
23+
];
24+
}

src/json5.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
export { json5SchemaLinter } from "./json5-validation";
33
export { json5SchemaHover } from "./json5-hover";
44

5+
/**
6+
* @group Bundled Codemirror Extensions
7+
*/
8+
export { json5Schema } from "./json5-bundled";
9+
510
export * from "./utils/parseJSON5Document";

typedoc.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"$schema": "https://typedoc.org/schema.json",
3-
"groupOrder": ["Codemirror Extensions", "Utilities", "*"],
3+
"groupOrder": [
4+
"Bundled Codemirror Extensions",
5+
"Codemirror Extensions",
6+
"Utilities",
7+
"*"
8+
],
49
"plugin": ["typedoc-plugin-markdown"],
510
"readme": "none"
611
}

0 commit comments

Comments
 (0)