@exercism/codemirror-lang-cfml
v1.0.2
Published
CFML language support for CodeMirror, with completion, as used on Exercism
Maintainers
Keywords
Readme
CFML language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for CFScript, the script syntax of CFML (Adobe ColdFusion and Lucee), with highlighting, indentation, folding and completion. Exercism uses it in the editor for its CFML 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-cfml - Yarn:
yarn add @exercism/codemirror-lang-cfml - Bun:
bun add @exercism/codemirror-lang-cfml
Usage
import { EditorView, basicSetup } from "codemirror";
import { cfml } from "@exercism/codemirror-lang-cfml";
new EditorView({
doc: "component {\n\tfunction hello() {\n\t\treturn 'Hello, World!';\n\t}\n}",
extensions: [basicSetup, cfml()],
parent: document.body,
});cfml() includes the completion. The package also exports cfmlLanguage, the two completion sources (localCompletionSource and cfmlCompletion) and the snippets.
What it covers
The grammar reads .cfc components and plain CFScript:
componentandinterfacedeclarations with attributes, as incomponent extends="Base" accessors=true { }.propertydeclarations, in both theproperty name="x" type="string";andproperty string x;forms.- functions with access modifiers, return types,
requiredand typed arguments, defaults and attributes, as inpublic numeric function add(required numeric a, b = 1) output=false { }. - closures (
function(x) { }) and arrow functions ((x) => x * 2,x => x). var,if,else if, Lucee'selseif,for,for (x in y),while,do,switch,try,catch,finally,throwandrethrow.- tags written as script, as in
cfloop(index="i" from="1" to="#n#") { },lock name="x" { },include "page.cfm";andabort;. - operators, including the word operators
EQ,NEQ,GT,LT,GTE,LTE,IS,CONTAINS,MOD,AND,OR,NOT,XOR,EQVandIMP,&for joining strings, and the?:Elvis operator. - strings with
#expression#interpolation, and"",''and##escapes. - structs (
{a: 1, "b" = 2}), ordered structs (["a": 1],[:]), arrays,new, and member functions such asname.len().
Keywords are case-insensitive, so FUNCTION, Function and function all work.
Semicolons are optional at the end of a line, as Lucee allows.
The rule that lets a line break end a statement is adapted in src/tokens.js from @lezer/javascript (MIT licence, by Marijn Haverbeke and others).
The completion offers:
- keywords, CFML's built-in functions (such as
arrayLen,listToArrayandstructKeyExists) and the scopes (arguments,variables,localand the others). - snippets for
component,function,publicandprivatefunctions, closures,if,if/else,for,for in,while,do,switch,try/catch,var,propertyandthrow. The statement snippets appear at the start of a statement. - types for return types and arguments, such as
numeric,stringandstruct. - the names declared in the file, each only where it is in scope. Inside a function that means its arguments, and the
varandlocal.names, loop variables and caught errors declared before the cursor. Everywhere in the component, it means the functions, the properties, and the variables set outside a function's own scope (total = 0,variables.total = 0orthis.total = 0). - after
arguments.,local.,variables.orthis., the names that scope holds. - after any other dot, CFML's member functions, such as
len,each,mapandkeyExists.
It offers nothing inside strings (outside #...#) and comments, or while you type a new name.
The grammar reads CFScript only, so .cfm pages written in tags (<cfset>, <cfoutput>) will show errors.
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 CFML 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 indentation and folding)
│ ├── syntax.grammar (the Lezer grammar)
│ ├── tokens.js (the case-insensitive keywords and the line break rule)
│ ├── highlight.js (the highlighting)
│ ├── complete.ts (the completion)
│ └── builtins.ts (the names of the built-in functions, member functions and types)
├── 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 (builds the grammar for the tests)
├── 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 to run the tests.
Building
Run bun run prepare to build the package into dist/.
bun install runs the same command, so a fresh install also builds the package.
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.
