@exercism/codemirror-lang-yamlscript
v1.0.2
Published
YAMLScript language support for CodeMirror
Maintainers
Keywords
Readme
YAMLScript language support for CodeMirror
This repo contains the CodeMirror 6 language support for YAMLScript (YS), the language taught on Exercism's YAMLScript track. It has a Lezer grammar, highlighting, indentation, folding and autocomplete.
How the grammar works
A YAMLScript file is a YAML document in which the keys and values are code, as in defn score(x y): or if x > 0: 'positive'.
So the grammar has two layers:
- The external tokenizers in
src/tokens.tshandle the YAML layer. They track indentation, decide which lines are mapping keys (a line with:outside quotes and brackets), and read block scalars and data-mode text. src/syntax.grammardescribes the YAMLScript code inside keys and values: calls,.and:chains, infix operators, operator-first lists such as(+ a b), literals and collections.
After :: (or =::), a value is data, and plain text such as north is a string.
Another :: inside data switches back to code.
The rules follow Exercism's tree-sitter grammar for YAMLScript, which is used by the representer.
Autocomplete
The completion offers:
- the functions in YAMLScript's
ys::stdlibrary, and theclojure.corenames that can be called from YAMLScript. - snippets for
defn,defn-,fn,if,when,cond,each,for,loopand=:, and the!YS-v0header at the start of a file. - the functions and bindings defined in the file, where they are in scope.
Top-level
defnnames and bindings are offered everywhere. A function's parameters, the bindings in a block, and the names bound byloop,for,each,letandfnare offered inside them.
Completion switches off inside strings, comments, regular expressions and data-mode text, and while a new function name or parameter is being typed.
What it doesn't parse yet
- YAML flow mappings with
:inside braces, such as{a: 1}. - YAML anchors, aliases and document markers (
---,...). - Tags such as
!codeand!dataparse as tags, and leave the mode as it is. Only::switches between code and data. - String interpolation (
"$name") is part of the string, so names inside strings aren't completed or highlighted.
⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:
"@codemirror/language": "^6.10.1",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"Install
- NPM:
npm install @exercism/codemirror-lang-yamlscript - Yarn:
yarn add @exercism/codemirror-lang-yamlscript - Bun:
bun add @exercism/codemirror-lang-yamlscript
Usage
import { EditorView, basicSetup } from "codemirror";
import { yamlscript } from "@exercism/codemirror-lang-yamlscript";
new EditorView({
doc: "!YS-v0\n\ndefn square(x): x * x\n",
extensions: [basicSetup, yamlscript()],
parent: document.querySelector("#editor"),
});Prerequisites
Resources
Structure
The repo's source files are written in TypeScript.
The repo is structured as follows:
.
├── src
│ ├── index.ts (the main plugin)
│ ├── complete.ts (the completion sources)
│ ├── builtins.ts (the ys::std and clojure.core names)
│ ├── tokens.ts (the tokenizers for the YAML layer)
│ ├── syntax.grammar (the Lezer grammar)
│ ├── syntax.grammar.d.ts (the grammar typings)
│ └── syntax.grammar.terms.d.ts (the typings for the grammar's terms)
├── test
│ ├── cases (test cases)
│ │ └ *.txt (test case)
│ ├── grammar.test.ts (the grammar tests)
│ ├── editor.test.ts (the highlighting, indentation and folding tests)
│ └── completion.test.ts (the completion tests)
├── index.html (dev server file)
├── rollup.config.js (bundling config)
├── tsconfig.json (TypeScript config)
└── vite.config.js (vite dev server config)Setup
Run bun install to install all dependencies.
Developing
To help with development, run bun run dev.
This starts a Vite dev server (usually at http://localhost:5173/) that renders the index.html file.
The #editor element holds some sample source code of your choosing, which the grammar in src/syntax.grammar highlights.
Any changes to the grammar will auto-refresh the dev server's rendered contents.
Building
Run bun run prepare to build the package into dist.
bun install also runs this script.
Testing
The test/cases directory contains the grammar's test files.
The grammar uses the tokenizers in src/tokens.ts, so the tests use the built parser in dist/.
Run bun run test to build the package and run the tests.
Note: test (case) files should be relatively small and focus on a single aspect of a grammar.
Publishing
To publish a new version, set version in package.json and commit it.
Then push a git tag named v followed by that version, such as v1.0.1 for version 1.0.1.
The publish workflow builds and tests the package, then publishes it to npm.
