Full language support for the Mercury logic/functional
programming language — the first VS Code extension to provide live diagnostics
straight from the mmc compiler, alongside a complete editing experience.
Features
- Live diagnostics from
mmc— errors and warnings appear as you save (or as you type), grouped correctly across the compiler's multi-line messages, with the right severity andrelatedInformationfor secondary locations. Two strategies:- single (default) — fast per-file
--errorcheck-only(runs--make-interfacefirst so imports resolve). - make — whole-program
mmc --make, reading the generated.errfiles; the reliable choice for multi-module projects.
- single (default) — fast per-file
- Syntax highlighting — a TextMate grammar ported from the official Vim
syntax file, including embedded C / C# / Java / Erlang highlighting inside
pragma foreign_proc/foreign_code/foreign_decl. - Outline / document symbols — predicates, functions, types (with constructors and fields nested), insts, modes, type classes and instances (with their methods).
- Workspace symbols, with module-qualified names.
- Go to definition — to
:- pred/:- funcdeclarations and clause heads. - Hover — the declaration plus its leading
%doc comment. - Completion — workspace symbols, keywords, determinism categories,
pragmas, purity annotations, module names in
import_modulecontext, and an optional Mercury standard-library set. - Semantic tokens — consistent colouring of declared types, predicates, functions and constructors.
- Formatting (document and range) — whitespace-only normalisation to the
Mercury coding standard: 4-space indentation (clause bodies one level;
bracket contents one past their opening line;
;/then/elseat the opening line's indent;--->constructor blocks anchored on the constructor column; clause-head continuations two levels), trailing-whitespace removal and a single final newline. Comment lines, multi-line strings (foreign_procbodies) and hand-aligned continuations are preserved; it never reflows tokens. - Snippets for modules, predicates, functions, types, type classes,
foreign_proc, DCG rules and more. - Build tasks (
mercurytask type) and a problem matcher formmcoutput.
Requirements
- The Mercury compiler (
mmc) must be installed and on yourPATH, or setmercury.mmc.path. Diagnostics shell out tommc. mmakeis optional; used by the build task when anMmakefileis present.
.m also denotes Objective-C and MATLAB files. This extension registers the
.m extension for Mercury unconditionally; the first-line pattern
(:- module …) only breaks ties when another installed extension also claims
.m. If a file is misdetected, use Mercury: Mark File as Mercury (or the
language picker in the status bar) to switch it.
Settings
| Setting | Default | Description |
|---|---|---|
mercury.mmc.path |
mmc |
Path to the compiler. |
mercury.mmc.args |
[] |
Extra args appended to every mmc invocation. |
mercury.mmake.path |
mmake |
Path to mmake. |
mercury.diagnostics.enable |
true |
Enable compiler diagnostics. |
mercury.diagnostics.run |
onSave |
onSave | onType | off. |
mercury.diagnostics.mode |
single |
single (fast) | make (multi-module). |
mercury.diagnostics.makeInterfaceFirst |
true |
Run --make-interface before the single-file check. |
mercury.diagnostics.mainModule |
"" |
Main module (no .m) for make mode. |
mercury.diagnostics.debounce |
500 |
Debounce (ms) for onType. |
mercury.grade |
"" |
Grade passed as --grade. |
mercury.useSubdirs / mercury.useGradeSubdirs |
false |
Pass --use-subdirs / --use-grade-subdirs. |
mercury.includeDirs |
[] |
Directories passed with -I. |
mercury.workingDirectory |
"" |
Override the compiler's working directory. |
mercury.maxNumberOfProblems |
1000 |
Cap on reported diagnostics per file. |
mercury.symbols.moduleQualifiedNames |
true |
Module-qualified workspace symbols. |
mercury.completion.enableStdlib |
true |
Offer stdlib names in completion. |
mercury.semanticTokens.enable |
true |
Enable semantic-token highlighting. |
mercury.format.enable |
true |
Enable document/range formatting. |
The server always injects --no-color-diagnostics --no-max-error-line-width
(and forces MERCURY_ENABLE_COLOR=never, LC_ALL=C) so that diagnostics are
machine-parsable and never leak ANSI colour codes; mercury.mmc.args is
appended on top.
Commands
- Mercury: Build —
mmc --make(ormmakewhen anMmakefileexists, or a custommercury.build.command). - Mercury: Check Current File — save and re-run diagnostics.
- Mercury: Mark File as Mercury — set a
.mbuffer's language. - Mercury: Restart Language Server / Show Language Server Output.
Development
npm install
npm run build # bundle client + server (esbuild)
npm run watch # rebuild on change
npm run typecheck # tsc --noEmit
npm run lint
npm test # unit + grammar tokenization + live-server (needs mmc)
npm run test:vscode # full VS Code host suite (needs a compatible VS Code + display)
npm run package # produce mercury-lang.vsix
Press F5 (Run Extension) to launch an Extension Development Host. The compound Run Extension + Attach to Server also attaches the debugger to the language server on port 6009.
Tests
npm run test:unit— Mocha unit tests for the diagnostics parser (checked against the compiler's own.err_expgolden files) and the symbol scanner.npm run test:grammar— tokenizes the sample files withvscode-textmate, asserting scopes and embedded-language regions.npm run test:server— spins up the built language server over LSP stdio and drives realmmcdiagnostics, outline, definition, hover, completion and semantic tokens end-to-end.
Acknowledgements
The syntax grammar is ported from vim/syntax/mercury.vim and the symbol
scanner from scripts/mtags, both part of the Mercury distribution.
License
MIT. See the LICENSE file in this repository.