numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
620 stars 38 forks source link
buildbot-numtide cli formatter treefmt unifies
# treefmt **One CLI to format your repo** _A numtide project._

Coverage Status Static Badge

treefmt streamlines the process of applying formatters to your project, making it a breeze with just one command line.

Motivation

It’s common to format code according to the project’s standards before making contributions to any project. This task seems trivial at first sight — you can set up the required language formatter in your IDE.

However, contributing to multiple projects requires more effort: you must change your formatter configurations each time you switch between projects or call them manually.

Formatting requires less effort with a universal formatter that supports multiple languages but is still project-specific.

About treefmt

treefmt runs all your formatters with one command. It’s easy to configure and fast to execute.

Treefmt Init

Its main features are:

To reformat the whole source tree, just type treefmt in any folder. This is a fast and simple formatting solution.

Installation

You can install treefmt by downloading the binary. Find the binaries for different architectures here. Otherwise, you can install the package from source code — either with Go, or with the help of nix.

We describe the installation process in detail in the docs.

Usage

In order to use treefmt in your project, make sure the config file treefmt.toml is present in the root folder and is edited to suit your needs.

You can generate it with:

$ treefmt --init

You can then run treefmt in your project root folder like this:

$ treefmt

To explore the tool’s flags and options, type:

$ treefmt --help

Additionally, there's a wrapper called treefmt-nix for using treefmt with nix.

Configuration

Formatters are specified in the config file treefmt.toml, which is usually located in the project root folder. The generic way to specify a formatter is like this:

[formatter.<name>]
command = "<formatter-command>"
options = ["<formatter-option-1>"...]
includes = ["<glob>"]

For example, if you want to use nixpkgs-fmt on your Nix project and rustfmt on your Rust project, then treefmt.toml will look as follows:

[formatter.nix]
command = "nixpkgs-fmt"
includes = ["*.nix"]

[formatter.rust]
command = "rustfmt"
options = ["--edition", "2018"]
includes = ["*.rs"]

Before specifying the formatter in the config, make sure it’s installed.

To find and share existing formatter recipes, take a look at the docs.

If you are a Nix user, you might also be interested in treefmt-nix to use Nix to configure and bring in formatters.

Compatibility

treefmt works with any formatter that adheres to the following specification.

For instance, you can go for:

Find the full list of supported formatters here.

IDE Integration

treefmt currently has support for vscode via an extension:

Upcoming features

This project is still pretty new. Down the line we also want to add support for:

Related projects

Contributing

All contributions are welcome! We try to keep the project simple and focused. Please refer to the Contributing guidelines for more information.

Commercial support

Looking for help or customization?

Get in touch with Numtide to get a quote. We make it easy for companies to work with Open Source projects: https://numtide.com/contact

License

Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion will be licensed under the MIT license without any additional terms or conditions.