The Clarity formatter is a tool designed to automatically format your Clarity smart contract code according to a standardized style. Using a consistent style improves code readability and maintainability, making it easier for developers to understand, collaborate on, and learn from existing Clarity projects.
As Clarity is a relatively young language, establishing formatting standards helps reduce friction for both new and experienced developers.
The formatter applies an opinionated standard with the following default settings:
Line Length: Lines wrap at 80 characters.
Indentation: Uses 2 spaces for indentation.
These defaults are configurable to match your preferences, although the specific configuration method depends on whether you are using VS Code or Clarinet.
Code is indented using 2 spaces per level. Long expressions exceeding the configured line length (default 80 characters) are automatically broken across multiple lines.
Functions (define-public, define-private, define-read-only) always span multiple lines. Arguments are double-indented if there is more than one. If a single argument fits on the first line, it remains there. The function body is single-indented, aligning with the closing parenthesis of the arguments. A trailing newline is added after each function definition.
Bindings within a let expression are placed on separate lines, indented once. The body of the let expression is indented at the same level as the bindings.
match expressions are always multi-line. Each match arm (pattern and corresponding expression) is placed on a new line, with the pattern and expression aligned.
Trailing comments ( ;; comment) are preserved and placed after the expression on the same line. They do not count towards the maximum line length calculation.
You can prevent the formatter from modifying specific sections of your code by adding a ;; @format-ignore comment immediately before the block you wish to exclude.
The Clarinet CLI allows you to format contracts from the command line using clarinet format (or the alias clarinet fmt). You can format individual files or all contracts defined in your project's Clarinet.toml manifest.
Format all checkable contracts and print the result without saving:
Terminal
$
clarinet format --dry-run
Format all checkable contracts (--in-place):
Terminal
$
clarinet format --in-place
Format a specific file using tabs and print the result without saving (--dry-run):
Terminal
$
clarinet format -f contracts/my-contract.clar -t --dry-run
Format a specific file, overwriting it (--in-place) with 4-space indents and 120 max line length:
Terminal
$
clarinet format -f contracts/my-contract.clar -i 4 -l 120 --in-place
The Clarity formatter is continually evolving based on community feedback. Please try it out and share your thoughts to help improve the standards for the ecosystem.