Ballerina language support for CodeMirror

This package gives the CodeMirror 6 editor a Lezer grammar for Ballerina, with highlighting, indentation, folding and completion.
Exercism uses it in the editor for its Ballerina track.
To keep the package working on the Exercism website, don't change the versions of these packages:
"@codemirror/language": "^6.10.1",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"
Install
- NPM:
npm install @exercism/codemirror-lang-ballerina
- Yarn:
yarn add @exercism/codemirror-lang-ballerina
- Bun:
bun add @exercism/codemirror-lang-ballerina
Usage
import { EditorView, basicSetup } from "codemirror";
import { ballerina } from "@exercism/codemirror-lang-ballerina";
new EditorView({
doc: 'import ballerina/io;\n\npublic function main() {\n io:println("Hello, World!");\n}\n',
extensions: [basicSetup, ballerina()],
parent: document.body,
});
ballerina() includes the completion. The package also exports ballerinaLanguage, the two completion sources (localCompletionSource and ballerinaCompletion) and the snippets.
What it covers
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.
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.
A statement can start with a type (Foo[] xs = [];) or with an expression (foo[i] = 1;).
The parser tries both readings where they overlap and keeps the one that parses.
Two small tokenizers in src/tokens.js help the grammar:
- 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.
- The other reads the
/ between the segments of a client resource access as a path separator, so it isn't read as a division.
The completion offers:
- keywords, built-in types,
true, false and null.
- snippets for
function, main, if, foreach, while, match, queries, record types, classes, services, resource functions, do and on fail, and import.
- the functions, types, classes, constants, enum members, variables, parameters and import prefixes declared in the file, each only where it is in scope.
- 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.
- 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.
- after
import, the common standard library modules.
It offers nothing inside strings and comments, or while you type a new name.
Prerequisites
Setup
Run bun install to install all dependencies.
Developing
To help with development, run bun run dev.
This will start a Vite dev server (usually at http://localhost:5173/) that renders the index.html file.
The #editor element shows some sample Ballerina code, highlighted by the grammar in src/syntax.grammar.
Any changes to the grammar will auto-refresh the dev server's rendered contents.
Structure
.
├── src
│ ├── index.ts (the language, with its highlighting, indentation and folding)
│ ├── complete.ts (the completion)
│ ├── syntax.grammar (the Lezer grammar)
│ ├── syntax.grammar.d.ts (the grammar typings)
│ └── tokens.js (the tokenizers for module prefixes and resource paths)
├── test
│ ├── cases (grammar test cases)
│ ├── grammar.test.ts (runs the grammar test cases)
│ ├── complete.test.ts (completion tests)
│ ├── editor.test.ts (indentation and folding tests)
│ └── lezer-plugin.ts (lets the tests import the grammar directly)
├── index.html (dev server file)
├── rollup.config.js (bundling config)
├── tsconfig.json (typescript config)
└── vite.config.js (vite dev server config)
Testing
Run bun test.
The tests build the parser straight from src/syntax.grammar, so they don't need a build first.
Building
Run bun run prepare to build the package into dist/.
It runs Rollup under Bun's runtime, because one of the Rollup plugins doesn't load in recent versions of Node.
Publishing
To publish a new version, set version in package.json and commit it.
Then push a tag made of v followed by that version, for example v1.0.0.
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
If the tag differs from version, the workflow stops before it publishes.