@exercism/codemirror-lang-elm
v1.0.2
Published
Elm language support for CodeMirror
Maintainers
Keywords
Readme
Elm language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for Elm 0.19, with highlighting, indentation, folding and autocompletion. Exercism uses it in the in-browser editor for the Elm 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-elm - Yarn:
yarn add @exercism/codemirror-lang-elm - Bun:
bun add @exercism/codemirror-lang-elm
Usage
import { EditorView, basicSetup } from "codemirror";
import { elm } from "@exercism/codemirror-lang-elm";
new EditorView({
extensions: [basicSetup, elm()],
parent: document.body,
});elm-format indents by four spaces. To match it, add indentUnit.of(" ") from @codemirror/language to the editor's extensions.
What it does
- It highlights keywords, strings, numbers, comments, declarations, function calls, types, constructors, record fields and operators.
- It indents a new line under
=,->,let,of,thenandelse, and folds declarations,letblocks,caseexpressions, records and lists. - Its autocompletion offers:
- keywords, and snippets for a function with its annotation,
case,if,let, a lambda, a custom type, a record type alias, a module header and an import. - the names imported into every Elm module by default, such as
List,Maybe,JustandmodBy. - a module's functions, types and constructors after its name and a dot, such as
List.maporDict.insert. This works for elm/core, elm/regex, elm/parser, elm/time, elm/random and elm/json, and for an alias set byimport Dict as D. - the names exposed by an import, such as
insertafterimport Dict exposing (insert). - the names declared in the file that are in scope at the cursor: top-level functions, types and constructors, parameters, and the names bound by
let,caseand lambdas. - record field names after a dot, as in
model.countor.count.
- keywords, and snippets for a function with its annotation,
How the grammar reads indentation
Elm marks its blocks with indentation, so the grammar can't be written with token rules alone.
The layout tokenizer in src/tokens.ts adds three invisible tokens for the grammar to use. One opens a block, one separates its items, and one closes it.
- The file is a block at column 0, so a line at the left margin starts a new declaration.
- A
letor anofopens a block at the column of the token after it. - A line at that column starts the next item, and a line further left closes the block.
- A block also closes in front of a token that can't go inside it. That is how
let x = 1 in xworks on one line.
The same file reads nested block comments, the module part of a qualified name such as List. in List.map, and the dot in model.count.
A dot written straight after a name reads a field, and a dot with a space in front of it starts an accessor function such as .count.
Prerequisites
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 with some sample Elm code.
The grammar is in src/syntax.grammar, the external tokenizers are in src/tokens.ts, and the completion is in src/complete.ts and src/modules.ts.
Testing
Run bun run test. It builds the package and then runs the tests in test/.
test/cases/*.txthold the grammar tests, in Lezer's test format.test/completion.test.tschecks what the completion offers at a given position.test/editor.test.tschecks highlighting, indentation and folding.
Building
Run bun run prepare to build the package into dist/.
bun install runs the same command, so a fresh install also builds the package.
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.
