@exercism/codemirror-lang-d 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 +93 -2
- package/dist/index.cjs +814 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +805 -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,94 @@
|
|
|
1
|
-
#
|
|
1
|
+
# D language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This package gives the CodeMirror 6 editor a Lezer grammar for D, with highlighting, indentation, folding and autocompletion.
|
|
6
|
+
Exercism uses it in the in-browser editor for the D track.
|
|
7
|
+
|
|
8
|
+
⚠️ Keep the versions of these dependencies as they are, so that the package works on the Exercism website:
|
|
9
|
+
|
|
10
|
+
```json
|
|
11
|
+
"@codemirror/language": "^6.10.1",
|
|
12
|
+
"@lezer/highlight": "^1.2.0",
|
|
13
|
+
"@lezer/lr": "^1.4.0"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
- NPM: `npm install @exercism/codemirror-lang-d`
|
|
19
|
+
- Yarn: `yarn add @exercism/codemirror-lang-d`
|
|
20
|
+
- Bun: `bun add @exercism/codemirror-lang-d`
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
26
|
+
import { d } from "@exercism/codemirror-lang-d";
|
|
27
|
+
|
|
28
|
+
new EditorView({
|
|
29
|
+
extensions: [basicSetup, d()],
|
|
30
|
+
parent: document.body,
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## What it does
|
|
35
|
+
|
|
36
|
+
- It highlights keywords, strings, numbers, comments, declarations, function calls, types, attributes such as `@safe`, and operators.
|
|
37
|
+
- It indents the lines inside blocks, and puts `case` labels one step into a `switch` and their statements two steps in.
|
|
38
|
+
- It folds blocks, aggregates, enums, array literals and comments. A function, struct or class also folds from its first line, because D code usually puts the opening brace on a line of its own.
|
|
39
|
+
- Its autocompletion offers:
|
|
40
|
+
- keywords, the built-in types such as `string` and `size_t`, and snippets for a module, an import, a function, a struct, a class, an enum, `if` and `if`/`else`, `foreach`, `for`, `while`, `switch`, `unittest` and `main`.
|
|
41
|
+
- the commonly used functions from Phobos (D's standard library), such as `writeln`, `map`, `iota`, `strip` and `to`. `src/phobos.ts` lists them by module.
|
|
42
|
+
- after a dot, the properties every value has (such as `length` and `dup`), the Phobos functions (because uniform call syntax lets `xs.sort` call `sort(xs)`), and the fields, methods and enum members declared in the file.
|
|
43
|
+
- module names in an `import`, and after `import std.algorithm :` the names that module offers.
|
|
44
|
+
- the names declared in the file that are in scope at the cursor: top-level declarations, the members of a class inside its methods, parameters and template parameters, loop variables, lambda parameters, and local variables declared above the cursor.
|
|
45
|
+
- Autocompletion is switched off inside strings and comments.
|
|
46
|
+
|
|
47
|
+
## How the grammar reads types
|
|
48
|
+
|
|
49
|
+
D can't tell a type from an expression by looking at one token. `a[i] = 3;` assigns to an element, but `int[] a;` declares an array.
|
|
50
|
+
So the grammar reads types with the same forms as expressions, such as `Foo!int`, `std.stdio.File` and `string[]`.
|
|
51
|
+
If a name follows one of these forms, the form is a type. If an operator or `;` follows it, it is an expression.
|
|
52
|
+
A few cases stay ambiguous until later in the line, such as `int* p;` against `a * b;`, and `(x) => x` against `(x)`. The parser tries both readings there and keeps the one that works.
|
|
53
|
+
|
|
54
|
+
The external tokenizers in `src/tokens.ts` read the parts of D that token rules can't: nested `/+ +/` comments, `q{...}` and `q"(...)"` strings, the bodies of `asm` blocks, and `!is` and `!in` (which `!isDigit` must not match).
|
|
55
|
+
|
|
56
|
+
## Prerequisites
|
|
57
|
+
|
|
58
|
+
- [Bun](https://bun.sh/)
|
|
59
|
+
|
|
60
|
+
## Setup
|
|
61
|
+
|
|
62
|
+
Run `bun install` to install all dependencies.
|
|
63
|
+
|
|
64
|
+
## Developing
|
|
65
|
+
|
|
66
|
+
To help with development, run `bun run dev`.
|
|
67
|
+
This starts a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders `index.html` with a sample of D code.
|
|
68
|
+
Any change to the grammar refreshes the page.
|
|
69
|
+
|
|
70
|
+
## Testing
|
|
71
|
+
|
|
72
|
+
Run `bun run test`. It builds the package and then runs the tests in `test/`:
|
|
73
|
+
|
|
74
|
+
- `grammar.test.ts` checks the trees for the cases in `test/cases`.
|
|
75
|
+
- `completion.test.ts` checks which completions appear at a given position.
|
|
76
|
+
- `editor.test.ts` checks highlighting, indentation and folding.
|
|
77
|
+
|
|
78
|
+
## Building
|
|
79
|
+
|
|
80
|
+
Run `bun run prepare` to build the package into `dist/`.
|
|
81
|
+
`bun install` runs the same command, so a fresh install also builds the package.
|
|
82
|
+
|
|
83
|
+
## Publishing
|
|
84
|
+
|
|
85
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
86
|
+
Then push a tag made of `v` followed by that version, for example `v1.0.0`.
|
|
87
|
+
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
|
|
88
|
+
If the tag differs from `version`, the workflow stops before it publishes.
|
|
89
|
+
|
|
90
|
+
## Resources
|
|
91
|
+
|
|
92
|
+
- [CodeMirror](https://codemirror.net/docs/)
|
|
93
|
+
- [Lezer](https://lezer.codemirror.net/docs/guide/)
|
|
94
|
+
- [The D grammar](https://dlang.org/spec/grammar.html)
|