Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Open VS Code
  2. Go to Extensions (Ctrl+Shift+X)
  3. Click Install from VSIX…
  4. Select the downloaded file

Features

FeatureWhat it does
Syntax highlightingKeywords, types, circuits colored
Code snippetsInsert common patterns
Error highlightingInline compiler errors
Build integrationCompile from VS Code
File templatesNew 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:

PrefixInserts
ledgerState declaration
constructorConstructor block
circuitCircuit function
witnessWitness function
stdlibStandard library import
ifIf statement
forFor loop
foldFold expression
enumEnum definition
structStruct definition
moduleModule definition
assertAssert statement
compactFull 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

  1. Open the command palette (Ctrl+Shift+P)
  2. Type Snippets: Fill File with Snippet
  3. 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 --vscode to compile command for error integration.
  • Use Ctrl+Shift+B to build.