@exercism/codemirror-lang-reasonml
v1.0.2
Published
ReasonML language support for CodeMirror
Maintainers
Keywords
Readme
ReasonML language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for ReasonML 3, with highlighting, indentation, folding and autocompletion. Exercism uses it in the in-browser editor for the ReasonML track.
⚠️ To keep the package working on the Exercism website, don't change the versions of these packages:
"@codemirror/language": "^6.10.1",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"Install
- NPM:
npm install @exercism/codemirror-lang-reasonml - Yarn:
yarn add @exercism/codemirror-lang-reasonml - Bun:
bun add @exercism/codemirror-lang-reasonml
Usage
import { EditorView, basicSetup } from "codemirror";
import { reasonml } from "@exercism/codemirror-lang-reasonml";
new EditorView({
extensions: [basicSetup, reasonml()],
parent: document.body,
});What it does
- It reads
.reand.reifiles: let bindings, types, modules, functors, module types, externals, attributes such as[@bs.val]and extensions such as[%re "/a/"]. - It highlights keywords, strings, numbers, comments, definitions, function calls, types, constructors, record fields, labels and operators.
- It indents inside braces, brackets and parentheses, and folds blocks, modules, records, lists, arrays and
switchexpressions. - Its autocompletion offers:
- keywords, and snippets for
let, a function,let rec,switch,ifandelse, record and variant types,module,module type,fun, an arrow function,for,while,tryandexception. - the built-in values, types and constructors, such as
string_of_int,optionandSome. - a module's members after its name and a dot, such as
List.map,Js.String.splitorBelt.Option.getWithDefault. This covers the OCaml 4.06 standard library shipped with BuckleScript, and everyJsandBeltmodule. Each member shows its type, such as(list('a), int) => 'aforList.nth.Option,Result,IntandFloatcomplete as Belt's modules, which solutions use afteropen Belt. - the members of a module declared in the file.
- the names declared in the file that are in scope at the cursor: values, functions, types, constructors, record fields, exceptions and modules declared above it, and the parameters and pattern variables of the functions,
switchcases and loops around it.
- keywords, and snippets for
How the grammar reads semicolons
Reason separates items with semicolons, but people often leave them out between top-level items.
When the next token can't continue the current item, the fallback tokenizer in src/tokens.ts ends the item there.
So let a = 1 on one line and let b = 2 on the next parse as two items, while f(x) followed by (y) on the next line still reads as one call.
The cases of a fun have no braces around them, so each case body is a single expression.
A switch case body can hold several items separated by semicolons, but a type declaration inside a case body needs braces.
Updating the module lists
src/modules.ts is generated from the interface files in bs-platform 9, which is the compiler used by the ReasonML track. The script reads the OCaml .mli and .ml files and writes each type in Reason syntax. To regenerate it:
npm pack bs-platform@9
tar xzf bs-platform-9.0.2.tgz
node scripts/generate-modules.mjs packageThe script prints how many members each module has.
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 ReasonML 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.
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
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.
The tests use the built package, because the grammar needs the external tokenizers in src/tokens.ts.
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.
