@exercism/codemirror-lang-purescript
v1.0.2
Published
PureScript language support for CodeMirror
Maintainers
Keywords
Readme
PureScript language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for PureScript, with highlighting, folding, indentation and autocompletion. Exercism uses it in the in-browser editor for the PureScript 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-purescript - Yarn:
yarn add @exercism/codemirror-lang-purescript - Bun:
bun add @exercism/codemirror-lang-purescript
Usage
import { EditorView, basicSetup } from "codemirror";
import { purescript } from "@exercism/codemirror-lang-purescript";
new EditorView({
doc: "main = log \"Hello\"",
extensions: [basicSetup, purescript()],
parent: document.body,
});What it supports
The grammar reads modules, imports and exports, functions with signatures and guards, where, let, do, ado, case (including several values at once), if, lambdas, operator sections and backtick operators.
It also reads data types, newtypes, type synonyms, kind signatures, classes, instances and instance chains, derived instances, foreign imports and fixity declarations.
For records, it reads literals, puns, patterns, updates, field access such as p.name and accessor sections such as _.name.
In types, it reads rows, open records, forall, constraints and the Unicode spellings of ::, ->, <-, => and forall.
PureScript 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, guards,
case,if,let,where,do, lambdas, folds, data types, newtypes, records, classes, instances, imports and the module header - the names exported by Prelude, and the common functions, types and constructors from modules such as
Data.Array,Data.Maybe,Data.String,Data.Tuple,Data.MapandEffect.Console - the names declared in the file that are in scope at the cursor, such as functions, types, constructors, parameters, pattern variables, and bindings from
let,where,doandado - record field names from the file after a dot
It offers nothing inside strings and comments.
Credits
The grammar and the layout tokenizer are 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, record and name 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 PureScript 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/.
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, 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 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.
