Automatically format Leo source files according to the official Leo style guide. This command ensures consistent code formatting across your project.
Usage
Options
Check if files are formatted without making changes. Exit with code 1 if any files need formatting.
Specific file or directory to format. Defaults to current directory.
Examples
This is useful in CI/CD pipelines:
# In CI script
leo fmt --check || exit 1
leo fmt --path src/main.leo
Leo follows these formatting conventions:
Indentation
- Use 4 spaces for indentation
- No tabs
Line Length
- Maximum 120 characters per line
- Break long lines at logical points
Spacing
// Correct spacing around operators
let sum: u32 = a + b;
// Spacing in function calls
let result: u32 = compute(x, y, z);
// No trailing whitespace
Function Declarations
// Properly formatted function
fn transfer(
sender: address,
receiver: address,
amount: u64
) -> bool {
return true;
}
Imports
// Group imports at the top
import token.aleo;
import utilities.aleo;
Integration
Pre-commit Hook
Add to .git/hooks/pre-commit:
#!/bin/bash
leo fmt --check
if [ $? -ne 0 ]; then
echo "Code is not formatted. Run 'leo fmt' to format."
exit 1
fi
VS Code
Configure VS Code to format on save by adding to .vscode/settings.json:
{
"leo.formatOnSave": true
}
CI/CD Integration
Add formatting check to your CI pipeline:
# GitHub Actions example
- name: Check formatting
run: leo fmt --check
- Build - Compile your formatted code
- Test - Run tests on formatted code
Best Practices
Run leo fmt before committing code to maintain consistent style across the team.
The formatter preserves comments and applies minimal changes to maintain code readability.
Troubleshooting
If leo fmt --check fails in CI, run locally:
leo fmt
git add .
git commit -m "Format code"
Fix syntax errors before formatting:
leo build # Check for syntax errors
leo fmt # Format after fixing errors