Editor support for your own language as a VS Code extension
vscodeextensiontextmatelanguage-serverlspdiagnosticstaskscompilers
A new programming language gets a usable IDE fastest as a VS Code extension, not a custom
editor: VS Code already provides the editing, terminals, tasks and Problems panel, and the
extension only has to describe the language and call its compiler. It can be built in layers, each
useful on its own. Justin's H language (~/dev/h, compiler hc) got layers 1 and 2 in one
evening on 2026-10-31, as a dependency-free extension in ~/dev/h/ide.
The layers
- Grammar + language configuration: syntax highlighting, comments, brackets, indentation. Pure JSON, no code.
- Errors and Run by calling the compiler: a small
extension.jspipes the buffer to the compiler and turns its error lines into diagnostics; a Run command compiles and runs in a terminal. - Language server (LSP): hover types, go-to-definition, rename, completion. Best written in the compiler's own language reusing its lexer, parser and checker, and it then works in any LSP editor, not only VS Code. Start it only once the parser has stopped changing shape.
- Debugger (DAP): only once the compiler emits debug info (e.g. DWARF line tables).
Layer 1: package.json contributions
contributes.languages: anid, fileextensions, an optionalfirstLineregex (e.g. a#!line naming the compiler) and the path tolanguage-configuration.json(line/block comment tokens,brackets,autoClosingPairs,wordPattern,indentationRules,onEnterRules).contributes.grammars: a TextMate grammar (syntaxes/<lang>.tmLanguage.json) with a uniquescopeNamesuch assource.h-lang. Patterns are Oniguruma regexes that assign scopes (keyword.control,string.quoted,entity.name.function...); the theme colors the scopes.- Mark constructs the compiler rejects with an
invalid.illegalscope, so they are visibly red before any compiler runs (H flagsfor (,continue;,2x, bad escapes,{}). - Keywords and built-ins are hand-listed in the grammar, so every new keyword means a grammar edit. Error checking needs no change, because it is whatever the compiler says.
activationEventscan be[]: since VS Code 1.74, contributed languages and commands activate the extension automatically.
Layer 2: errors as you type
- Pipe the unsaved buffer to the compiler on stdin rather than compiling the file on disk:
H's
hc -S -o /dev/null -reads-as stdin and prints-:LINE:COL: error: message. Adding a stdin mode to the compiler is the one compiler change this needs. - Debounce (250 ms after typing stops), plus check on open and save.
- Keep one child process per document: kill the previous one when a new check starts, and drop a
result if
document.versionchanged while it ran, or stale errors flash up. - Handle a missing compiler (
ENOENTfromspawn) once, with a message saying how to fix it. - Find the compiler in a predictable order: a setting (
h.compilerPath), thenbuild/hcin any directory above the file or in a workspace folder (the repo layout), then the bare name onPATH. - A compiler that stops at the first error gives one squiggle at a time. That is fine for a start.
Layer 2: Run
- Build a
vscode.Taskwith aShellExecution(compile to a temp path, then run it), shown in a dedicated terminal panel withfocus: trueso keyboard input reaches the program's stdin. - A task with a custom
typemust have a matching entry incontributes.taskDefinitions, or VS Code refuses it. - Contribute a problem matcher (
contributes.problemMatchers, a regex with file/line/column/ message groups). Compile errors and runtime errors from the task then land in the Problems panel, and users can reuse the matcher ($hc) in their owntasks.json. - Expose it as a command with an
editor/titlemenu entry ("icon": "$(play)") and a keybinding, both gated by"when": "editorLangId == <id>".
Packaging and installing
npx @vscode/vsce package --skip-license --allow-missing-repository --out lang.vsix
code --install-extension lang.vsix # then Developer: Reload Window
No marketplace account is needed for a local install. Keep *.vsix in .gitignore and list
dev-only files in .vscodeignore. Bump version in package.json for each reinstall.
From a shell with no Node on PATH (a headless session): VS Code is itself a Node runtime, so ELECTRON_RUN_AS_NODE=1 /usr/share/code/code script.js runs a test script, and packaging works by putting an nvm-installed Node's bin directory on PATH and using npx --offline @vscode/vsce package .... code --install-extension lang.vsix --force installs over the same version.
Keeping the grammar up with the language
A hand-written TextMate grammar knows nothing the compiler knows, so every new keyword, built-in and member has to be added by hand, in the same change as the language feature or soon after.
- Contextual words: match a word as a keyword only in the position where it is one (emit only when a value follows, type only before Name {), so a variable with that name stays a variable.
- A convention can stand in for semantics: treating capitalized names as types highlights record types correctly without a symbol table.
- A catch-all such as "any name after a single | is a pipeline stage" covers user-defined stages.
- What a grammar cannot do (module prefixes, telling < in a definition's options from a comparison) is the argument for the language-server layer.
- Check the invalid-token rules whenever number syntax changes; 1_000 was being flagged as an error.
Testing without opening VS Code
- Grammar: tokenize sample files with the
vscode-textmateandvscode-onigurumanpm packages (the same engine VS Code uses) and assert the scopes of tricky tokens. - Diagnostics: run
extension.jsunder Node against a hand-written mockvscodemodule (put it onNODE_PATH) and the real compiler binary, then check the produced ranges and messages.
Gotchas
- Working in a subdirectory of a repo that another session is also committing to: a session that
commits everything (
git add -A) will sweep in your half-written files. Expect it, keep to your own directory, and never touch files outside it (H, 2026-10-31). - Diagnostics need a range; when the compiler gives only a column, underline the word starting there (or one character) so the squiggle is visible.
See also
- Running several AI coding sessions on one repo (worktrees, locks, fresh memory)
- Justin's standing rules for anything built for him