Futhark language support for CodeMirror

This package gives the CodeMirror 6 editor a Lezer grammar for Futhark, with highlighting, indentation, folding and autocompletion.
Exercism uses it in the in-browser editor for the Futhark track.
⚠️ Keep the versions of these dependencies as they are, so that the package works on the Exercism website:
"@codemirror/language": "^6.10.1",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"
Install
- NPM:
npm install @exercism/codemirror-lang-futhark
- Yarn:
yarn add @exercism/codemirror-lang-futhark
- Bun:
bun add @exercism/codemirror-lang-futhark
Usage
import { EditorView, basicSetup } from "codemirror";
import { futhark } from "@exercism/codemirror-lang-futhark";
new EditorView({
extensions: [basicSetup, futhark()],
parent: document.body,
});
What it does
- It highlights keywords, strings, characters, numbers, comments, attributes, definitions, function calls, types, constructors, record fields and operators.
- It indents a new line under
=, ->, then, else, do, in, an open bracket or a trailing operator.
It lines in up with its let, then and else with their if, and case with its match.
- It folds declarations,
let, if, loop and match expressions, lambdas, modules, records, tuples and arrays.
- Its autocompletion offers:
- keywords, and snippets for
def, entry, let ... in, loop with for i < n, for x in xs and while, if ... then ... else, match, a lambda, type and module.
- the prelude's functions, such as
map, reduce, scan, filter, iota, zip and length, and the primitive types.
- a numeric module's functions after its name and a dot, such as
i32.sum or f64.sqrt.
- the names declared in the file that are in scope at the cursor. These are the definitions above it, the parameters of the function it is in, and the names bound by
let, loop, case and lambdas around it.
- record field names after a dot, as in
queen.row, and constructors after a #.
How the grammar reads Futhark
Futhark has no layout rule, so the grammar reads a file as a run of declarations.
An if, let, loop, match or lambda extends as far as it can.
Two things need the tokenizer in src/tokens.ts.
- A
[ straight after a name or a closing bracket indexes an array, as in xs[i].
A [ after a space starts an array literal, so f [1, 2] passes an array to f.
- An operator is any run of operator characters, such as
+, |> or a user's own +++.
It takes its precedence from its first characters, as Futhark's parser does.
Prerequisites
Resources
Structure
The repo's source files are written in TypeScript.
The repo is structured as follows:
.
├── src
│ ├── index.ts (the language, highlighting, indentation and folding)
│ ├── complete.ts (the autocompletion)
│ ├── prelude.ts (the names Futhark's prelude puts in scope)
│ ├── tokens.ts (the tokenizer for index brackets and operators)
│ ├── syntax.grammar (the Lezer grammar)
│ ├── syntax.grammar.d.ts (the grammar typings)
│ └── syntax.grammar.terms.d.ts (the typings for the tokenizer's terms)
├── test
│ ├── cases (grammar test cases)
│ │ └ *.txt (test case)
│ ├── grammar.test.ts (grammar tests)
│ ├── completion.test.ts (completion tests)
│ └── editor.test.ts (highlighting, indentation and folding 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 index.html.
The #editor element holds some sample source code, which you can change, and the grammar in src/syntax.grammar highlights it.
Any change to the grammar refreshes the page.
Building
Run bun run prepare to build the package into dist/.
It runs Rollup under Bun's runtime, because one of the Rollup plugins doesn't load in recent versions of Node.
Testing
The test/cases directory holds the grammar tests, and test/completion.test.ts and test/editor.test.ts test the completion, highlighting, indentation and folding.
The grammar uses the tokenizer in src/tokens.ts, so the tests use the built package.
Run bun run test to build it and run the tests.
Keep each test case file small and focused on one aspect of the grammar.
Publishing
To publish a new version, set version in package.json and commit it.
Then push a tag made of v followed by that version, for example v1.0.0.
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
If the tag differs from version, the workflow stops before it publishes.