Skip to content

Commit 3040b5b

Browse files
authored
Highlight CodeMirror JSON snippets the same as non-interactive ones (#2145)
1 parent 4c55544 commit 3040b5b

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
"dependencies": {
2727
"@codemirror/autocomplete": "^6.18.6",
2828
"@codemirror/commands": "^6.3.3",
29-
"@codemirror/lang-json": "^6.0.1",
3029
"@codemirror/language": "^6.10.0",
3130
"@codemirror/lint": "^6.8.5",
3231
"@codemirror/state": "^6.4.0",
3332
"@codemirror/view": "^6.24.0",
3433
"@graphql-tools/schema": "10.0.25",
34+
"@hasparus/lezer-json-shikified": "1.1.3",
3535
"@headlessui/react": "^2.2.4",
3636
"@igorkowalczyk/is-browser": "^5.1.0",
3737
"@lezer/highlight": "^1.2.1",

pnpm-lock.yaml

Lines changed: 12 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { parser } from "@hasparus/lezer-json-shikified"
2+
import {
3+
continuedIndent,
4+
indentNodeProp,
5+
foldNodeProp,
6+
foldInside,
7+
LRLanguage,
8+
LanguageSupport,
9+
} from "@codemirror/language"
10+
11+
export const jsonLanguage = LRLanguage.define({
12+
name: "json",
13+
parser: parser.configure({
14+
props: [
15+
indentNodeProp.add({
16+
Object: continuedIndent({ except: /^\s*\}/ }),
17+
Array: continuedIndent({ except: /^\s*\]/ }),
18+
}),
19+
foldNodeProp.add({
20+
"Object Array": foldInside,
21+
}),
22+
],
23+
}),
24+
languageData: {
25+
closeBrackets: { brackets: ["[", "{", '"'] },
26+
indentOnInput: /^\s*[}\]]$/,
27+
},
28+
})
29+
30+
export function json() {
31+
return new LanguageSupport(jsonLanguage)
32+
}

src/components/interactive-code-block/result-viewer.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Component } from "react"
22
import { EditorView } from "@codemirror/view"
33
import { EditorState } from "@codemirror/state"
4-
// todo: perhaps custom grammar to match the shiki highlighting?
5-
import { json } from "@codemirror/lang-json"
4+
import { json } from "./codemirror-json"
65
import { codeMirrorThemeExtension } from "./codemirror-theme"
76

87
import "./syntax-highlighting.css"

src/components/interactive-code-block/variable-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from "react"
22
import { EditorView } from "@codemirror/view"
33
import { EditorState } from "@codemirror/state"
4-
import { json } from "@codemirror/lang-json"
4+
import { json } from "./codemirror-json"
55
import { history } from "@codemirror/commands"
66
import { syntaxHighlighting } from "@codemirror/language"
77
import { codeMirrorThemeExtension } from "./codemirror-theme"

0 commit comments

Comments
 (0)