@exercism/codemirror-lang-ballerina 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 +21 -0
- package/README.md +112 -2
- package/dist/index.cjs +728 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +722 -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,113 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Ballerina language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This package gives the [CodeMirror 6](https://codemirror.net/) editor a [Lezer](https://lezer.codemirror.net/) grammar for [Ballerina](https://ballerina.io/), with highlighting, indentation, folding and completion.
|
|
6
|
+
Exercism uses it in the editor for its Ballerina track.
|
|
7
|
+
|
|
8
|
+
To keep the package working on the Exercism website, don't change the versions of these packages:
|
|
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-ballerina`
|
|
19
|
+
- Yarn: `yarn add @exercism/codemirror-lang-ballerina`
|
|
20
|
+
- Bun: `bun add @exercism/codemirror-lang-ballerina`
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
26
|
+
import { ballerina } from "@exercism/codemirror-lang-ballerina";
|
|
27
|
+
|
|
28
|
+
new EditorView({
|
|
29
|
+
doc: 'import ballerina/io;\n\npublic function main() {\n io:println("Hello, World!");\n}\n',
|
|
30
|
+
extensions: [basicSetup, ballerina()],
|
|
31
|
+
parent: document.body,
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`ballerina()` includes the completion. The package also exports `ballerinaLanguage`, the two completion sources (`localCompletionSource` and `ballerinaCompletion`) and the `snippets`.
|
|
36
|
+
|
|
37
|
+
## What it covers
|
|
38
|
+
|
|
39
|
+
The grammar parses Ballerina Swan Lake: imports, functions (with block, `=>` and `external` bodies), types (records, objects, unions, tuples, arrays, maps, tables, function types and singleton types), classes, services and resource functions, constants, enums, annotations and every statement, including `match`, `do` and `on fail`, `lock`, transactions and workers.
|
|
40
|
+
Expressions include `check`, type tests and casts, lambdas, `let`, query expressions and query actions, template strings with `${...}`, remote calls such as `cl->get("/")`, client resource access such as `cl->/orders/[id].post(x)`, and XML navigation.
|
|
41
|
+
|
|
42
|
+
A statement can start with a type (`Foo[] xs = [];`) or with an expression (`foo[i] = 1;`).
|
|
43
|
+
The parser tries both readings where they overlap and keeps the one that parses.
|
|
44
|
+
|
|
45
|
+
Two small tokenizers in `src/tokens.js` help the grammar:
|
|
46
|
+
|
|
47
|
+
- One reads a module prefix such as `io` in `io:println`. Ballerina allows no space around the colon of a qualified name, so this tokenizer looks for a name, a colon and another name with nothing between them.
|
|
48
|
+
- The other reads the `/` between the segments of a client resource access as a path separator, so it isn't read as a division.
|
|
49
|
+
|
|
50
|
+
The completion offers:
|
|
51
|
+
|
|
52
|
+
- keywords, built-in types, `true`, `false` and `null`.
|
|
53
|
+
- snippets for `function`, `main`, `if`, `foreach`, `while`, `match`, queries, record types, classes, services, resource functions, `do` and `on fail`, and `import`.
|
|
54
|
+
- the functions, types, classes, constants, enum members, variables, parameters and import prefixes declared in the file, each only where it is in scope.
|
|
55
|
+
- after a dot, the langlib methods for the value's type when the file declares it (such as `push` for an `int[]` or `toUpperAscii` for a `string`), the fields of a record type or class declared in the file, and the methods every value has. After `self.` it offers the fields and methods of the class.
|
|
56
|
+
- after a module prefix such as `io:`, `int:`, `string:` or `array:`, that module's functions, types and constants. It follows import aliases, so `import ballerina/lang.array as arr;` makes `arr:` offer the array functions.
|
|
57
|
+
- after `import`, the common standard library modules.
|
|
58
|
+
|
|
59
|
+
It offers nothing inside strings and comments, or while you type a new name.
|
|
60
|
+
|
|
61
|
+
## Prerequisites
|
|
62
|
+
|
|
63
|
+
- [Bun](https://bun.sh/)
|
|
64
|
+
|
|
65
|
+
## Setup
|
|
66
|
+
|
|
67
|
+
Run `bun install` to install all dependencies.
|
|
68
|
+
|
|
69
|
+
## Developing
|
|
70
|
+
|
|
71
|
+
To help with development, run `bun run dev`.
|
|
72
|
+
This will start a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders the `index.html` file.
|
|
73
|
+
The `#editor` element shows some sample Ballerina code, highlighted by the grammar in `src/syntax.grammar`.
|
|
74
|
+
Any changes to the grammar will auto-refresh the dev server's rendered contents.
|
|
75
|
+
|
|
76
|
+
## Structure
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
.
|
|
80
|
+
├── src
|
|
81
|
+
│ ├── index.ts (the language, with its highlighting, indentation and folding)
|
|
82
|
+
│ ├── complete.ts (the completion)
|
|
83
|
+
│ ├── syntax.grammar (the Lezer grammar)
|
|
84
|
+
│ ├── syntax.grammar.d.ts (the grammar typings)
|
|
85
|
+
│ └── tokens.js (the tokenizers for module prefixes and resource paths)
|
|
86
|
+
├── test
|
|
87
|
+
│ ├── cases (grammar test cases)
|
|
88
|
+
│ ├── grammar.test.ts (runs the grammar test cases)
|
|
89
|
+
│ ├── complete.test.ts (completion tests)
|
|
90
|
+
│ ├── editor.test.ts (indentation and folding tests)
|
|
91
|
+
│ └── lezer-plugin.ts (lets the tests import the grammar directly)
|
|
92
|
+
├── index.html (dev server file)
|
|
93
|
+
├── rollup.config.js (bundling config)
|
|
94
|
+
├── tsconfig.json (typescript config)
|
|
95
|
+
└── vite.config.js (vite dev server config)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Testing
|
|
99
|
+
|
|
100
|
+
Run `bun test`.
|
|
101
|
+
The tests build the parser straight from `src/syntax.grammar`, so they don't need a build first.
|
|
102
|
+
|
|
103
|
+
## Building
|
|
104
|
+
|
|
105
|
+
Run `bun run prepare` to build the package into `dist/`.
|
|
106
|
+
It runs Rollup under Bun's runtime, because one of the Rollup plugins doesn't load in recent versions of Node.
|
|
107
|
+
|
|
108
|
+
## Publishing
|
|
109
|
+
|
|
110
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
111
|
+
Then push a tag made of `v` followed by that version, for example `v1.0.0`.
|
|
112
|
+
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
|
|
113
|
+
If the tag differs from `version`, the workflow stops before it publishes.
|