#apex #formatter #salesforce #language #afmt

bin+lib sf-afmt

An Salesforce Apex lang formatter

2 unstable releases

new 0.7.0 Jan 19, 2025
0.6.0 Jan 19, 2025

#42 in Value formatting

Download history 67/week @ 2025-01-13

71 downloads per month

MIT/Apache

420KB
9K SLoC

🚀 A Fast Configurable Salesforce Apex Formatter

Release License Stars

afmt_logo

Table of Contents


📘 Introduction

afmt (Apex formatting tool) is written in Rust 🦀 and leverages the tree-sitter sfapex parser.


⭐ Features

  • Performant
  • Configurable: via .afmt.toml.
  • Standalone: CLI with no dependencies.
  • Open Source

✨ vs. Prettier Apex

While both afmt and Prettier Apex aim to format Salesforce Apex code, they differ fundamentally in their design philosophies:

  • Prettier Apex: Maintains an opinionated approach with limited customization to ensure consistency.
  • afmt: Focuses on extensibility, offering more configuration options to cater to diverse user preferences.

This means afmt will progressively introduce more configuration options, addressing user customization needs that Prettier's design intentionally avoids.

Other Highlights:

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

📥 Installation

1. Script Install

The script downloads afmt latest version to a user's folder and prompts whether adding it to the Path.

[!tip] Always review the installation script before executing it on your system.

For Linux/MacOS

curl -sL https://raw.githubusercontent.com/xixiaofinland/afmt/main/scripts/install-afmt.sh | bash

For Windows

iwr -useb https://raw.githubusercontent.com/xixiaofinland/afmt/main/scripts/install-afmt.ps1 | iex

2. Cargo Install

afmt is published in creates.io here. Run cmd below if you have the Cargo tool.

cargo install sf-afmt

3. Manual Download

Visit the release page and download the appropriate binary for your operating system (Linux, macOS, or Windows).


💻 Usage

Create a file.cls file with valid Apex code.

Dry Run:

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];
    }
}

Format and Write:

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.

🔧 Configuration:

in .afmt.toml config file, two options are supported

# Maximum line width
max_width = 80

# Indentation size in spaces
indent_size = 4

❓ FAQ

  • "How do I set up afmt in VSCode?" Setup in VSCode

  • "Can afmt formats exactly the same as Prettier Apex?" No.

  • "How to load a .afmt.toml file?" afmt -c path_to_file. You can refer to afmt -h for more help info.


🤝 Contribution

We greatly value contributions! You can help by reporting issues or submitting PRs.

PR Contribution Guidelines

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:

  1. Name your branch: feature/what_ever_short_name
  2. Use conventional commit for commit messages. Example: the project commit history
  3. Ensure code passes rustfmt and clippy: cargo fmt -- --check and cargo clippy
  4. Run and pass all unit tests: cargo test --all-features
  5. Pass battle tests by running afmt on a list of popular Apex repos

Dependencies

~15MB
~412K SLoC