@exercism/codemirror-lang-roc 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/dist/index.cjs +556 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +547 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2024 by Erik Schierboom <erik_schierboom@hotmail.com> and others
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,101 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Roc language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This package gives the CodeMirror 6 editor a Lezer grammar for Roc, with highlighting, indentation, folding and autocompletion.
|
|
6
|
+
Exercism uses it in the in-browser editor for the Roc track.
|
|
7
|
+
|
|
8
|
+
It reads the syntax of the current Roc compiler, which is the one the Exercism track uses. This includes `|x|` lambdas, `match x { ... }`, `if a b else c`, method calls such as `list.map(f)`, type modules such as `Leap :: {}.{ ... }`, `var $x`, `for` and `while` loops, and `?` for errors.
|
|
9
|
+
It doesn't read the older syntax with `\x ->` lambdas, `when ... is` and `module [...]` headers.
|
|
10
|
+
|
|
11
|
+
⚠️ To keep the package working on the Exercism website, don't change the versions of these packages:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
"@codemirror/language": "^6.10.1",
|
|
15
|
+
"@lezer/highlight": "^1.2.0",
|
|
16
|
+
"@lezer/lr": "^1.4.0"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
- NPM: `npm install @exercism/codemirror-lang-roc`
|
|
22
|
+
- Yarn: `yarn add @exercism/codemirror-lang-roc`
|
|
23
|
+
- Bun: `bun add @exercism/codemirror-lang-roc`
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
29
|
+
import { roc } from "@exercism/codemirror-lang-roc";
|
|
30
|
+
|
|
31
|
+
new EditorView({
|
|
32
|
+
extensions: [basicSetup, roc()],
|
|
33
|
+
parent: document.body,
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## What it does
|
|
38
|
+
|
|
39
|
+
- It highlights keywords, strings, characters, numbers, comments, definitions, function and method calls, types, tags, record fields and operators.
|
|
40
|
+
- It indents new lines inside `{ }`, `[ ]` and `( )`, and after a line that leaves an expression unfinished. It folds blocks, records, `match` bodies, type modules, lists and tag unions.
|
|
41
|
+
- Its autocompletion offers:
|
|
42
|
+
- keywords, and snippets for a function with its annotation, a lambda, `match`, `if`, `for`, `while`, `var`, a type module, a nominal type, `expect` and an import.
|
|
43
|
+
- the builtin types and tags, such as `List`, `U64`, `Ok` and `True`.
|
|
44
|
+
- a builtin module's functions after its name and a dot, such as `List.map`, `Str.split_on` or `Dict.insert`. This covers `Str`, `List`, `Dict`, `Set`, `Try` (and `Result`), `Bool`, `Box`, `Iter`, `Stream`, `Hasher`, `Num` and each number type.
|
|
45
|
+
- the functions of a type defined in the file after its name, such as `Clock.create`.
|
|
46
|
+
- after a value and a dot, the record fields written in the file and then the builtin functions that can be called as methods. After a string or a list, it offers only that type's functions.
|
|
47
|
+
- the names declared in the file that are in scope at the cursor: functions and values, `var` variables, types and their tags, imported names, lambda parameters, and the names bound by `match`, `for` and destructuring.
|
|
48
|
+
|
|
49
|
+
`src/builtins.ts` lists the builtin functions. They come from `src/build/roc/Builtin.roc` in the Roc compiler, and the comment at the top of the file names the commit.
|
|
50
|
+
|
|
51
|
+
## How the grammar reads spaces
|
|
52
|
+
|
|
53
|
+
Roc ends a statement at the point where its expression can't go on, so the grammar treats new lines as spaces.
|
|
54
|
+
Spaces matter in a few places, and the external tokenizer in `src/tokens.ts` handles them:
|
|
55
|
+
|
|
56
|
+
- `f(x)` calls `f`, and `f (x)` is `f` followed by `(x)`.
|
|
57
|
+
- `a - 1` and `a-1` subtract, and `a -1` and `[-1]` start a negative number. This lets a `match` arm such as `-1 => 9` follow another arm.
|
|
58
|
+
- `x?` returns the error early, and `x ? |e| Other(e)` changes the error first.
|
|
59
|
+
- A capitalised name followed by a dot and a name, as in `List.map`, is a module or type name.
|
|
60
|
+
|
|
61
|
+
`{ x }` could be a block or a record. The parser tries both and keeps the record when both fit.
|
|
62
|
+
|
|
63
|
+
## Prerequisites
|
|
64
|
+
|
|
65
|
+
- [Bun](https://bun.sh/)
|
|
66
|
+
|
|
67
|
+
## Setup
|
|
68
|
+
|
|
69
|
+
Run `bun install` to install all dependencies.
|
|
70
|
+
|
|
71
|
+
## Developing
|
|
72
|
+
|
|
73
|
+
To help with development, run `bun run dev`.
|
|
74
|
+
This starts a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders `index.html` with some sample Roc code.
|
|
75
|
+
|
|
76
|
+
The grammar is in `src/syntax.grammar`, the external tokenizer is in `src/tokens.ts`, and the completion is in `src/complete.ts` and `src/builtins.ts`.
|
|
77
|
+
|
|
78
|
+
## Building
|
|
79
|
+
|
|
80
|
+
Run `bun run prepare` to build the package into `dist/`.
|
|
81
|
+
It runs Rollup under Bun's runtime, because one of the Rollup plugins doesn't load in recent versions of Node.
|
|
82
|
+
|
|
83
|
+
## Testing
|
|
84
|
+
|
|
85
|
+
Run `bun run test`. It builds the package and then runs the tests in `test/`.
|
|
86
|
+
|
|
87
|
+
- `test/cases/*.txt` hold the grammar tests, in [Lezer's test format](https://lezer.codemirror.net/docs/guide/#running-the-grammar).
|
|
88
|
+
- `test/completion.test.ts` checks what the completion offers at a given position.
|
|
89
|
+
- `test/editor.test.ts` checks highlighting, indentation and folding.
|
|
90
|
+
|
|
91
|
+
## Resources
|
|
92
|
+
|
|
93
|
+
- [CodeMirror](https://codemirror.net/docs/)
|
|
94
|
+
- [Lezer](https://lezer.codemirror.net/docs/guide/)
|
|
95
|
+
|
|
96
|
+
## Publishing
|
|
97
|
+
|
|
98
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
99
|
+
Then push a tag made of `v` followed by that version, for example `v1.0.0`.
|
|
100
|
+
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
|
|
101
|
+
If the tag differs from `version`, the workflow stops before it publishes.
|