@exercism/codemirror-lang-unison
v1.0.2
Published
Unison language support for CodeMirror
Maintainers
Keywords
Readme
Unison language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for Unison, with highlighting, folding, indentation and autocompletion. Exercism uses it in the in-browser editor for the Unison track.
⚠️ 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-unison - Yarn:
yarn add @exercism/codemirror-lang-unison - Bun:
bun add @exercism/codemirror-lang-unison
Usage
import { EditorView, basicSetup } from "codemirror";
import { unison } from "@exercism/codemirror-lang-unison";
new EditorView({
doc: 'main = do printLine "Hello"',
extensions: [basicSetup, unison()],
parent: document.body,
});What it supports
The grammar reads type signatures and definitions, including namespaced names such as List.map, operator definitions and bindings that take a value apart, such as (a, b) = pair.
It reads cases, match ... with (with several patterns and guards), if ... then ... else, lambdas, do, let, handle ... with, delayed computations ('expr) and forcing them (!expr).
It also reads use clauses, type declarations with constructors or records, ability declarations, handler patterns such as { ask -> resume }, watches (> expr), tests (test> ...), docs ({{ ... }}) and the --- line that folds away the rest of a file.
In types, it reads functions, ability lists such as {IO, Exception}, delayed types such as '{Random} Nat, lists, tuples and forall.
Unison uses indentation to open and close blocks. The tokenizer in src/tokens.ts follows the indentation and tells the grammar where each block opens, where each item starts and where each block ends.
Completion offers:
- the keywords
- snippets for functions,
cases,match, guards,if,do,let, lambdas, folds, types, records, abilities, handlers,useand tests - common functions, types and constructors from base, such as
printLine,OptionalandSome - the common members of
List,Text,Nat,Int,Optional,Map,CharandSetafter their name and a dot, as inList. - the names declared in the file that are in scope at the cursor, such as definitions, types, constructors, ability operations, parameters, pattern variables, lambda arguments and the bindings in blocks
- a definition's short name as well as its full name. For example,
sublist.isSuperlistis offered in full, asisSuperliston its own, and asisSuperlistafter typingsublist.
Completion switches off inside texts, comments and docs.
Credits
The layout tokenizer follows the approach of the PureScript grammar. That grammar is adapted from codemirror-lang-haskell by CodinCod, which is released under the MIT licence.
Prerequisites
Structure
.
├── src
│ ├── index.ts (the language support: highlighting, folding and indentation)
│ ├── complete.ts (the completion sources and snippets)
│ ├── syntax.grammar (the Lezer grammar)
│ ├── tokens.ts (the layout, comment, name and doc tokenizers)
│ └── syntax.grammar*.d.ts (the grammar typings)
├── test
│ ├── cases (grammar test cases)
│ ├── grammar.test.ts (runs the grammar test cases)
│ ├── complete.test.ts (completion tests)
│ └── editor.test.ts (folding, indentation and highlighting 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
Run bun run dev to start a Vite dev server, usually at http://localhost:5173/.
It shows index.html, which loads some sample Unison into an editor that uses this package.
The page reloads when the grammar changes.
Building
Run bun run prepare to build the package into dist.
bun install also runs this script.
Testing
Run bun run test. It builds the package and then runs the tests, because the grammar tests use the built parser.
Each file in test/cases holds grammar test cases. Keep each case small and about one feature.
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.
