VS Code Extension
This note covers the Visual Studio Code extension for Compact, which provides syntax highlighting, snippets, and integrated compilation.
Docs: VS Code Extension
Intuition First
The VS Code extension adds Compact language support to Visual Studio Code. It highlights keywords, provides code snippets, and integrates with the compiler for error reporting.
Installed via a VSIX file, not the VS Code marketplace. The extension is maintained by the Midnight team.
Installation
Download the VSIX
Download the latest release:
curl -LO https://raw.githubusercontent.com/midnight-ntwrk/releases/gh-pages/artifacts/vscode-extension/compact-0.2.13/compact-0.2.13.vsix
Install in VS Code
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Click Install from VSIX…
- Select the downloaded file
Features
| Feature | What it does |
|---|---|
| Syntax highlighting | Keywords, types, circuits colored |
| Code snippets | Insert common patterns |
| Error highlighting | Inline compiler errors |
| Build integration | Compile from VS Code |
| File templates | New Compact files |
Syntax Highlighting
The extension recognizes:
- Keywords:
circuit,witness,export,ledger,enum,struct,module,import,assert - Types:
Uint<n>,Uint<0..n>,Field,Boolean,Bytes<n>,Map,Vector - Literals: strings, numbers, booleans
- Comments:
//and/* */
Code Snippets
Type the snippet prefix and press Tab:
| Prefix | Inserts |
|---|---|
ledger | State declaration |
constructor | Constructor block |
circuit | Circuit function |
witness | Witness function |
stdlib | Standard library import |
if | If statement |
for | For loop |
fold | Fold expression |
enum | Enum definition |
struct | Struct definition |
module | Module definition |
assert | Assert statement |
compact | Full contract template |
Building from VS Code
Add a Build Script
In package.json:
{
"scripts": {
"compact": "compact compile --vscode ./contracts/myContract.compact ./contracts/managed/myContract"
}
}
The --vscode flag formats errors for VS Code.
Compile
yarn compact
Errors appear in the Problems panel.
Task Configuration
For integrated building, create .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile Compact",
"type": "shell",
"command": "npx compact compile --vscode --skip-zk ${file} ${workspaceFolder}/contracts/managed",
"group": "build",
"problemMatcher": [
"$compactException",
"$compactInternal",
"$compactCommandNotFound"
]
}
]
}
Then press Ctrl+Shift+B to build.
Creating a New Contract
- Open the command palette (Ctrl+Shift+P)
- Type Snippets: Fill File with Snippet
- Select Compact
This creates a full contract template:
pragma language_version 0.22;
import CompactStandardLibrary;
export ledger state: State;
enum State { UNSET, SET }
constructor() {
}
export circuit init(): [] {
}
export circuit interact(): [] {
}
Quick Recap
- Install via VSIX file.
- Syntax highlighting works automatically.
- Use snippets for common patterns.
- Add
--vscodeto compile command for error integration. - Use Ctrl+Shift+B to build.
Cross-Links
- Previous: Neovim Setup Neovim editor
- Next: Testing and Debugging Error handling
- See also: Setting Up the Compiler Toolchain installation