@exercism/codemirror-lang-vimscript 0.0.0-stage → 1.0.1

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,103 @@
1
- # Temporary Holding Version
1
+ # Vim script 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-vimscript)
4
+
5
+ This package gives the CodeMirror 6 editor a Lezer grammar for Vim script, with highlighting, indentation, folding and autocompletion.
6
+ It reads legacy Vim script and the common parts of Vim9 script.
7
+ Exercism uses it in the in-browser editor for the Vim script track.
8
+
9
+ ⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:
10
+
11
+ ```json
12
+ "@codemirror/language": "^6.10.1",
13
+ "@lezer/highlight": "^1.2.0",
14
+ "@lezer/lr": "^1.4.0"
15
+ ```
16
+
17
+ ## Install
18
+
19
+ - NPM: `npm install @exercism/codemirror-lang-vimscript`
20
+ - Yarn: `yarn add @exercism/codemirror-lang-vimscript`
21
+ - Bun: `bun add @exercism/codemirror-lang-vimscript`
22
+
23
+ ## Usage
24
+
25
+ ```js
26
+ import { EditorView, basicSetup } from "codemirror";
27
+ import { vimscript } from "@exercism/codemirror-lang-vimscript";
28
+
29
+ new EditorView({
30
+ extensions: [basicSetup, vimscript()],
31
+ parent: document.body,
32
+ });
33
+ ```
34
+
35
+ ## What it does
36
+
37
+ - It highlights commands, keywords, strings, numbers, comments, function definitions, calls, variables, dictionary members, Vim9 types and operators.
38
+ - It indents the body of `function`, `def`, `if`, `for`, `while`, `try` and `class` blocks, and lines up `else`, `catch` and the closing word with the line that opened the block. It folds those blocks, and lists, dictionaries and argument lists.
39
+ - Its autocompletion offers:
40
+ - the Ex commands, and snippets for `function!`, `def`, `if`, `if`/`else`, `elseif`, `for`, a `for` over `items()`, `while`, `try`/`catch` and `let`, where a command can start.
41
+ - the built-in functions, the `v:` variables, the scope prefixes (`g:`, `s:`, `l:`, `a:` and so on) and a lambda snippet inside expressions.
42
+ - `abort`, `range`, `dict` and `closure` after a function header.
43
+ - the names declared in the file that are in scope at the cursor. These are the file's functions, its `g:` and `s:` variables, the variables set at the top of the script, and inside a function its arguments (as `a:name`, or by name in a Vim9 `def`) and its locals. Lambda parameters are offered inside the lambda.
44
+ - the dictionary keys used in the file after a `.`, as in `self.items`.
45
+
46
+ ## How the grammar reads lines
47
+
48
+ The grammar reads Vim script line by line, so some of it can't be written as token rules. The external tokenizer in `src/tokens.ts` handles these parts:
49
+
50
+ - A line break ends a command. A line that starts with `\` carries on the line before it, and so does a Vim9 line that starts with `->`, `&&`, `||` or `..`. A line break inside an open bracket doesn't end the command either, which Vim9 script allows. A context tracker counts the open brackets.
51
+ - `"` starts a comment where a string can't go, as after `endif` or after a complete expression, and at the start of a line. Everywhere else it starts a string. A string that isn't closed on its line is read as a comment.
52
+ - `#` followed by a space starts a Vim9 comment.
53
+ - If a `.` comes straight after a name and a letter follows it, the grammar reads a dictionary member such as `self.items`. Any other `.` joins two strings.
54
+ - Command words such as `let`, `if` and `endfunction` are keywords only where a command can go. Elsewhere they stay names, so `function('Name')`, `range(5)` and `call(F, [])` read as calls. The tokenizer knows the short forms Vim accepts, such as `fu`, `endf` and `retu`.
55
+ - The tokenizer keeps the arguments of commands such as `normal`, `set` and `autocmd` as plain text.
56
+
57
+ ## What it doesn't read yet
58
+
59
+ - Ranges in front of a command, such as `:1,$d` or `%s/a/b/g`.
60
+ - Heredocs (`let x =<< END`).
61
+ - Curly-brace names such as `foo{n}bar`, and `<SID>` in front of a name.
62
+ - Vim9 block lambdas (`(x) => {`), `enum`, `interface`, and `public` or `static` class members.
63
+
64
+ ## Prerequisites
65
+
66
+ - [Bun](https://bun.sh/)
67
+
68
+ ## Setup
69
+
70
+ Run `bun install` to install all dependencies.
71
+
72
+ ## Developing
73
+
74
+ To help with development, run `bun run dev`.
75
+ This starts a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders `index.html` with some sample Vim script.
76
+
77
+ The grammar is in `src/syntax.grammar`, the external tokenizer is in `src/tokens.ts`, and the completion is in `src/complete.ts`.
78
+
79
+ `src/builtins.ts` lists Vim's built-in functions, Ex commands and `v:` variables. `scripts/builtins.mjs` writes it from the help files of a Vim install, for example `node scripts/builtins.mjs /opt/homebrew/share/vim/vim92/doc`.
80
+
81
+ ## Building
82
+
83
+ Run `bun run prepare` to build the package into `dist`.
84
+ `bun install` also runs this script.
85
+
86
+ ## Testing
87
+
88
+ Run `bun run test`. It builds the package and then runs the tests in `test/`. The tests read the built package, because the grammar's external tokenizer can't be loaded when the grammar is built from source inside a test.
89
+
90
+ - `test/cases/*.txt` hold the grammar tests, in [Lezer's test format](https://lezer.codemirror.net/docs/guide/#running-the-grammar).
91
+ - `test/completion.test.ts` checks what the completion offers at a given position.
92
+ - `test/editor.test.ts` checks highlighting, indentation and folding.
93
+
94
+ ## Resources
95
+
96
+ - [CodeMirror](https://codemirror.net/docs/)
97
+ - [Lezer](https://lezer.codemirror.net/docs/guide/)
98
+
99
+ ## Publishing
100
+
101
+ To publish a new version, set `version` in `package.json` and commit it.
102
+ Then push a git tag named `v` followed by that version, such as `v1.0.1` for version 1.0.1.
103
+ The publish workflow builds and tests the package, then publishes it to npm.