@exercism/codemirror-lang-nim 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 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,112 @@
1
- # Temporary Holding Version
1
+ # Nim language support for CodeMirror
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ![NPM Version](https://img.shields.io/npm/v/@exercism/codemirror-lang-nim)
4
+
5
+ This package gives the [CodeMirror 6](https://codemirror.net/) editor a [Lezer](https://lezer.codemirror.net/) grammar for [Nim](https://nim-lang.org/), with highlighting, indentation, folding and completion.
6
+ Exercism uses it in the editor for its Nim track.
7
+
8
+ Keep these dependency versions as they are, so the package works with 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-nim`
19
+ - Yarn: `yarn add @exercism/codemirror-lang-nim`
20
+ - Bun: `bun add @exercism/codemirror-lang-nim`
21
+
22
+ ## Usage
23
+
24
+ ```ts
25
+ import { EditorView, basicSetup } from "codemirror";
26
+ import { nim } from "@exercism/codemirror-lang-nim";
27
+
28
+ new EditorView({
29
+ doc: 'echo "Hello, World!"',
30
+ extensions: [basicSetup, nim()],
31
+ parent: document.body,
32
+ });
33
+ ```
34
+
35
+ `nim()` includes the completion. The package also exports `nimLanguage`, the two completion sources (`localCompletionSource` and `nimCompletion`) and the `snippets`.
36
+
37
+ ## What it covers
38
+
39
+ The grammar parses Nim's statements and expressions: imports, `let`, `var` and `const` sections, `type` sections with objects, enums, tuples and variant objects, procs and the other routines, lambdas, pragmas, `if`, `when`, `case`, `try` and `block` (as statements and as values), loops, command calls such as `echo x` and `result.add y`, raw strings such as `fmt"..."`, and every kind of literal and comment.
40
+
41
+ Nim marks blocks by indentation. The tokenizer in `src/tokens.js` looks at each line break and decides what it means:
42
+
43
+ - If the next line is indented further and the grammar can open a block there, it emits `indent`.
44
+ - If the next line is indented less, it first emits a `dedent` for each block that ends.
45
+ - It skips the line break if the next line starts with `elif`, `else`, `of`, `except` or `finally` at the same depth, if the next line starts with `.name`, or if the current line ends in the middle of an expression.
46
+ - Otherwise it emits `newline`, which separates statements.
47
+
48
+ Inside brackets it skips line breaks, except in the body of a `proc` written inside a call.
49
+
50
+ The completion offers:
51
+
52
+ - keywords, built-in types and the most used procs from `system`, `strutils`, `sequtils`, `math`, `algorithm`, `tables` and `sets`.
53
+ - snippets for `proc`, `func`, `iterator`, `if`, `case`, `for`, `while`, `try` and `type`.
54
+ - the procs, variables, parameters, types and enum members declared in the file, each only where it is in scope. Inside a proc with a return type it also offers `result`, and inside `mapIt` and the other `...It` templates it offers `it`.
55
+ - after a dot, the fields and procs declared in the file and the common standard library procs. Procs are offered because Nim treats `x.len` as a call to `len(x)`.
56
+ - in an `import`, the standard library's modules.
57
+
58
+ It offers nothing inside strings and comments, or while you type a new name.
59
+
60
+ ## Prerequisites
61
+
62
+ - [Bun](https://bun.sh/)
63
+
64
+ ## Setup
65
+
66
+ Run `bun install` to install all dependencies.
67
+
68
+ ## Developing
69
+
70
+ To help with development, run `bun run dev`.
71
+ This starts a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders the `index.html` file.
72
+ The `#editor` element shows some sample Nim code, highlighted by the grammar in `src/syntax.grammar`.
73
+ Changes to the grammar refresh the page automatically.
74
+
75
+ ## Structure
76
+
77
+ ```text
78
+ .
79
+ ├── src
80
+ │ ├── index.ts (the language, with its highlighting, indentation and folding)
81
+ │ ├── complete.ts (the completion)
82
+ │ ├── syntax.grammar (the Lezer grammar)
83
+ │ ├── syntax.grammar.d.ts (the grammar typings)
84
+ │ └── tokens.js (the tokenizers for indentation, operators and calls)
85
+ ├── test
86
+ │ ├── cases (grammar test cases)
87
+ │ ├── grammar.test.ts (runs the grammar test cases)
88
+ │ ├── complete.test.ts (completion tests)
89
+ │ ├── editor.test.ts (indentation and folding tests)
90
+ │ └── lezer-plugin.ts (lets the tests import the grammar directly)
91
+ ├── index.html (dev server file)
92
+ ├── rollup.config.js (bundling config)
93
+ ├── tsconfig.json (typescript config)
94
+ └── vite.config.js (vite dev server config)
95
+ ```
96
+
97
+ ## Testing
98
+
99
+ Run `bun test`.
100
+ The tests build the parser straight from `src/syntax.grammar`, so they don't need a build first.
101
+
102
+ ## Building
103
+
104
+ Run `bun run prepare` to build the package into `dist/`.
105
+ It runs Rollup under Bun's runtime, because one of the Rollup plugins doesn't load in recent versions of Node.
106
+
107
+ ## Publishing
108
+
109
+ To publish a new version, set `version` in `package.json` and commit it.
110
+ Then push a tag made of `v` followed by that version, for example `v1.0.0`.
111
+ The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
112
+ If the tag differs from `version`, the workflow stops before it publishes.