@exercism/codemirror-lang-vlang 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,126 @@
1
- # Temporary Holding Version
1
+ # V 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-vlang)
4
+
5
+ This package gives CodeMirror 6 a Lezer grammar for [V](https://vlang.io), with highlighting, indentation, folding and autocomplete.
6
+ Exercism's in-browser editor uses it for the V track.
7
+
8
+ ⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:
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-vlang`
19
+ - Yarn: `yarn add @exercism/codemirror-lang-vlang`
20
+ - Bun: `bun add @exercism/codemirror-lang-vlang`
21
+
22
+ ## Usage
23
+
24
+ ```js
25
+ import { EditorView, basicSetup } from "codemirror";
26
+ import { vlang } from "@exercism/codemirror-lang-vlang";
27
+
28
+ new EditorView({
29
+ doc: "fn main() {\n\tprintln('hello')\n}\n",
30
+ extensions: [basicSetup, vlang()],
31
+ parent: document.body,
32
+ });
33
+ ```
34
+
35
+ `vlang()` returns the language with its three completion sources:
36
+
37
+ - Keywords, built-in functions and types, and snippets for common constructs (functions, methods, structs, enums, `if`, `for`, `match` and `or` blocks).
38
+ - Functions, constants and types after `arrays.`, `strings.`, `math.` and `os.`, including through an import alias such as `import strings as s`. After the name of an enum or type declared in the file, this source offers the enum's values and the type's static methods.
39
+ - Names declared in the file: functions, types, constants, imports, parameters, local variables, loop variables, `if` guards and lambda parameters. Each name is offered inside its scope.
40
+
41
+ Completion stays quiet inside strings and comments, and works again inside `${...}` interpolations.
42
+
43
+ ## How the grammar works
44
+
45
+ V ends a statement at the end of a line. The external tokenizer in `src/tokens.ts` inserts a statement separator at a line end when the last token can end a statement, as `@lezer/go` does for Go. It leaves the statement open when the next line starts with `.name` (a method chain), `|`, `&&`, `||` or, after a `}`, `else`.
46
+
47
+ V needs struct, enum and interface names to start with a capital letter, and other names to start with a small one.
48
+ The grammar uses this to tell a struct literal such as `Point{x: 1}` from a block such as `if x {`.
49
+
50
+ ## Prerequisites
51
+
52
+ - [Bun](https://bun.sh/)
53
+
54
+ ## Resources
55
+
56
+ - [CodeMirror](https://codemirror.net/docs/)
57
+ - [Lezer](https://lezer.codemirror.net/docs/guide/)
58
+ - [Writing a Grammar](https://lezer.codemirror.net/docs/guide/#writing-a-grammar)
59
+ - [Examples](https://lezer.codemirror.net/examples/)
60
+ - [Existing grammar files](https://github.com/search?q=org%3Alezer-parser+path%3A%2F.grammar%24%2F&type=code)
61
+
62
+ ## Structure
63
+
64
+ The repo's source files are written in [TypeScript](https://www.typescriptlang.org/).
65
+
66
+ The repo is structured as follows:
67
+
68
+ ```text
69
+ .
70
+ ├── src
71
+ │ ├── index.ts (the language, with highlighting, indentation and folding)
72
+ │ ├── complete.ts (the completion sources and snippets)
73
+ │ ├── builtins.ts (keywords and built-ins for completion)
74
+ │ ├── vlib-members.ts (module members for completion, generated from vlib)
75
+ │ ├── tokens.ts (the external tokenizers)
76
+ │ ├── syntax.grammar (the Lezer grammar)
77
+ │ └── syntax.grammar.d.ts (the grammar typings)
78
+ ├── test
79
+ │ ├── cases (grammar test cases)
80
+ │ ├── grammar.test.ts (runs the grammar test cases)
81
+ │ ├── editor.test.ts (highlighting, indentation and folding tests)
82
+ │ └── completion.test.ts (completion tests)
83
+ ├── scripts
84
+ │ └── vlib-members.ts (generates src/vlib-members.ts)
85
+ ├── index.html (dev server file)
86
+ ├── rollup.config.js (bundling config)
87
+ ├── tsconfig.json (typescript config)
88
+ └── vite.config.js (vite dev server config)
89
+ ```
90
+
91
+ ## Setup
92
+
93
+ Run `bun install` to install all dependencies.
94
+
95
+ ## Developing
96
+
97
+ To help with development, run `bun run dev`.
98
+ This starts a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders the `index.html` file.
99
+ The `#editor` element gets populated with some sample V code, highlighted by the grammar in `src/syntax.grammar`.
100
+ Any changes to the grammar will auto-refresh the dev server's rendered contents.
101
+
102
+ ## Building
103
+
104
+ Run `bun run prepare` to build the package into `dist`.
105
+ `bun install` also runs this script.
106
+
107
+ ## Testing
108
+
109
+ Run `bun run test`. It builds the package and then runs the tests, because the tests use the built parser.
110
+ The `test/cases` directory holds the grammar test cases.
111
+
112
+ ## Updating the module members
113
+
114
+ `scripts/vlib-members.ts` reads the `pub fn`, `pub const`, `pub struct`, `pub enum`, `pub interface` and `pub type` declarations of `arrays`, `strings`, `math` and `os` in V's source, and writes `src/vlib-members.ts`.
115
+ To pick up a newer V, clone V and run the script on the clone:
116
+
117
+ ```shell
118
+ git clone --depth 1 https://github.com/vlang/v /tmp/v
119
+ bun scripts/vlib-members.ts /tmp/v
120
+ ```
121
+
122
+ ## Publishing
123
+
124
+ To publish a new version, set `version` in `package.json` and commit it.
125
+ Then push a git tag named `v` followed by that version, such as `v1.0.1` for version 1.0.1.
126
+ The publish workflow builds and tests the package, then publishes it to npm.