| Crates.io | sf-afmt |
| lib.rs | sf-afmt |
| version | 0.12.2 |
| created_at | 2025-01-19 19:50:51.131609+00 |
| updated_at | 2025-04-07 19:30:02.797459+00 |
| description | An Salesforce Apex lang formatter |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1523128 |
| size | 5,971,683 |
afmt (Apex formatting tool) is written in Rust π¦ and leverages the tree-sitter sfapex parser.
[!NOTE] We're looking for contributors to help create a VSCode plugin! Feel free to join the discussion!
Try the browser version playground, and its source code here.
.afmt.toml.While both afmt and Prettier Apex aim to format Salesforce Apex code, they differ fundamentally in their design philosophies:
This means afmt will progressively introduce more configuration options, addressing user customization needs that Prettier's design intentionally avoids.
| Feature | afmt | Prettier Apex |
|---|---|---|
| Maturity | Brand new | Battle tested for years |
| Dependencies | N/A (standalone binary) | Node.js + Prettier package |
| Performance | Fast (Rust) | Relatively slower (Node.js) |
| Parser | sfapex (C / Open Source) | Jorje (Java / Closed Source) |
| Open Source | Yes | Yes |
curl -sL https://raw.githubusercontent.com/xixiaofinland/afmt/main/scripts/install-afmt.sh | bash
iwr -useb https://raw.githubusercontent.com/xixiaofinland/afmt/main/scripts/install-afmt.ps1 | iex
[!NOTE] If you see an error like "This script contains malicious content and has been blocked by your antivirus software", it means Microsoft Defender flagged it for downloading and executing content from the internet. To proceed, either lower Defenderβs protection or break the script into smaller steps:
# Step 1: Review the script manually
Invoke-WebRequest -Uri https://raw.githubusercontent.com/xixiaofinland/afmt/main/scripts/install-afmt.ps1 -OutFile install-afmt.ps1
notepad install-afmt.ps1 # Inspect the content
# Step 2: Run after trust
powershell -ExecutionPolicy Bypass -File install-afmt.ps1
afmt is published in creates.io here.
Run cmd below if you have the Cargo tool.
cargo install sf-afmt
Visit the release page and download the appropriate binary for your operating system (Linux, macOS, or Windows).
Create a file.cls file with valid Apex code.
Run afmt ./file.cls to preview the formatting result.
> afmt ./file.cls
Result 0: Ok
global class PluginDescribeResult {
{
[SELECT FIELDS(STANDARD) FROM Organization LIMIT 1];
}
}
Run afmt -w ./file.cls to format the file and overwrite it with the
formatted code.
> afmt -w ./file.cls
Formatted content written back to: ./file.cls
Afmt completed successfully.
-c parameter can read configuration settings from a toml file.
Example: afmt -c .afmt.toml
In .afmt.toml config file, two options are supported
# Maximum line width
max_width = 80
# Indentation size in spaces
indent_size = 4
"TLTR, what features afmt has?" Run afmt -h.
"How do I set up afmt in VSCode?" Setup in VSCode
"Can afmt formats exactly the same as Prettier Apex?" No.
We greatly value contributions! You can help by reporting issues or submitting PRs.
Scenarios (e.g., new features, bug fixes) must be covered by tests, and cargo test passes.
Refer to *.in (before format) and *.cls (after format) files in the test folder.
Also, our CI pipeline ensures high-quality contributions.
CI Rules:
cargo fmt -- --check and cargo clippycargo test --all-featuresafmt on a list of popular Apex repos