@exercism/codemirror-lang-powershell
v1.0.2
Published
PowerShell language support for CodeMirror
Maintainers
Keywords
Readme
PowerShell language support for CodeMirror
This package gives the CodeMirror 6 editor a Lezer grammar for PowerShell, with highlighting, indentation, folding and completion. It is written for the online editor of Exercism's PowerShell 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-powershell - Yarn:
yarn add @exercism/codemirror-lang-powershell - Bun:
bun add @exercism/codemirror-lang-powershell
Usage
import { EditorView, basicSetup } from "codemirror";
import { powershell } from "@exercism/codemirror-lang-powershell";
new EditorView({
extensions: [basicSetup, powershell()],
parent: document.body,
});powershell() includes the completion.
The package also exports powershellLanguage, the completion source on its own, and the lists the completion uses (keywords, cmdlets, automaticVariables, types, operators and snippets).
What the completion offers
- Keywords, common cmdlets such as
Write-OutputandForEach-Object, and snippets for a function with aparamblock,if/elseif/else,foreach,for,while,do/until,switch,try/catch,classandenum. - Automatic variables such as
$_,$PSItemand$args. - The functions, variables and parameters declared in the file. A variable assigned inside a function or script block is offered inside that function or block and nowhere else.
- Types and attributes inside
[, including the classes and enums declared in the file. - After
$this., the members of the class. After[Name]::, the static members of a class or the members of an enum. - Operators such as
-eqand-likeafter a dash in an expression.
It offers nothing inside comments or single-quoted strings.
Inside a double-quoted string it offers variables after $.
How the grammar reads PowerShell
PowerShell reads a line in one of two modes.
A line that starts with a value ($x, 1, "a", (...), [int]) is an expression, and -eq is an operator.
A line that starts with a bare word (Get-Item) is a command, and -Path is a parameter.
The grammar follows the same rule.
A newline ends a statement wherever the statement can end.
A line that ends with |, =, , or an operator carries on to the next line, and so does a line that starts with |, else, elseif, catch or finally.
src/tokens.ts makes these decisions.
Keywords and operators are case-insensitive, so Function, IF and -EQ all work.
Known gaps
- The grammar reads a line that starts with an operator, such as
-and, as a new statement. It doesn't join that line to the one before. - A here-string is highlighted as one string, without its
$variables. - The grammar doesn't read a bare word that starts with something other than a letter, such as
.\script.ps1or*.txt, as a command argument.
Development
You need Bun.
bun installinstalls the dependencies.bun run testbuilds the parser fromsrc/syntax.grammarand runs the tests.bun run preparebuilds the package intodist/.bun run devstarts a playground at http://localhost:5173/ (runbun run build:grammarfirst).
The grammar tests are in test/cases, one file per topic.
Each test gives some PowerShell and the tree it should parse to.
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.
