From c6944518e72aef0a2b81952dff6bc0114b8c6be0 Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 23 Feb 2024 03:16:00 +0100 Subject: [PATCH] Added YAML support (#85) Closes #84 - Added YAML support - Also updated the DOM rendering bits to use markdown instead of setting the HTML directly, which is a safer approach - Also slightly improved the demo UI (it didn't look good with black text on black background) - Also added a bunch of tests To support YAML (and potentially other formats), I updated the logic to "resolve" tokens before using them. This is important since the [YAML grammar/tokens](https://github.com/lezer-parser/yaml/blob/main/src/yaml.grammar) is different from the [JSON grammar/tokens](https://github.com/lezer-parser/json/blob/main/src/json.grammar) (which is very similar to the [JSON5 grammar](https://github.com/dimfeld/lezer-json5/blob/master/src/json5.grammar)). So we map the tokens from the different grammars to the equivalent token in the "base" grammar (currently this is the JSON grammar tokens but I think we can/should map to some other arbitrary token set which will force us to properly implement the mappings, otherwise additional changes made may work in JSON mode but without proper testing, may not work in YAML mode, if the mappings were not added). --- .changeset/silly-oranges-worry.md | 5 + dev/index.html | 10 + dev/index.ts | 18 +- dev/sample-text.ts | 9 + package.json | 20 +- pnpm-lock.yaml | 112 ++- src/__tests__/__helpers__/completion.ts | 21 +- src/__tests__/__helpers__/index.ts | 17 + src/__tests__/json-completion.spec.ts | 764 +++++++++++------- src/__tests__/json-hover.spec.ts | 216 +++-- src/__tests__/json-validation.spec.ts | 354 ++++++-- src/constants.ts | 33 +- src/json-completion.ts | 249 ++++-- src/json-hover.ts | 36 +- src/json-validation.ts | 48 +- src/json5.ts | 11 - src/{json5-bundled.ts => json5/bundled.ts} | 10 +- src/json5/completion.ts | 16 + src/{json5-hover.ts => json5/hover.ts} | 7 +- src/json5/index.ts | 11 + .../validation.ts} | 6 +- src/types.ts | 3 + src/utils/__tests__/jsonPointers.spec.ts | 394 ++++++--- src/utils/__tests__/node.spec.ts | 183 +++++ .../__tests__/parse-yaml-document.spec.ts | 24 + src/utils/debug.ts | 6 + src/utils/dom.ts | 8 - src/utils/formatting.ts | 2 +- src/utils/jsonPointers.ts | 67 +- src/utils/markdown.ts | 4 + src/utils/node.ts | 102 ++- src/utils/parse-yaml-document.ts | 23 + src/utils/parseJSON5Document.ts | 3 +- src/utils/parseJSONDocument.ts | 3 +- src/yaml/bundled.ts | 28 + src/yaml/completion.ts | 14 + src/yaml/hover.ts | 22 + src/yaml/index.ts | 11 + src/yaml/validation.ts | 22 + 39 files changed, 2164 insertions(+), 728 deletions(-) create mode 100644 .changeset/silly-oranges-worry.md create mode 100644 src/__tests__/__helpers__/index.ts delete mode 100644 src/json5.ts rename src/{json5-bundled.ts => json5/bundled.ts} (71%) create mode 100644 src/json5/completion.ts rename src/{json5-hover.ts => json5/hover.ts} (75%) create mode 100644 src/json5/index.ts rename src/{json5-validation.ts => json5/validation.ts} (73%) create mode 100644 src/utils/__tests__/node.spec.ts create mode 100644 src/utils/__tests__/parse-yaml-document.spec.ts create mode 100644 src/utils/markdown.ts create mode 100644 src/utils/parse-yaml-document.ts create mode 100644 src/yaml/bundled.ts create mode 100644 src/yaml/completion.ts create mode 100644 src/yaml/hover.ts create mode 100644 src/yaml/index.ts create mode 100644 src/yaml/validation.ts diff --git a/.changeset/silly-oranges-worry.md b/.changeset/silly-oranges-worry.md new file mode 100644 index 0000000..da16fa5 --- /dev/null +++ b/.changeset/silly-oranges-worry.md @@ -0,0 +1,5 @@ +--- +"codemirror-json-schema": minor +--- + +Added YAML support, switched back to markdown for messages, provide markdown rendering, and fix some autocompletion issues diff --git a/dev/index.html b/dev/index.html index eecd032..f81ced3 100644 --- a/dev/index.html +++ b/dev/index.html @@ -29,6 +29,7 @@