one CLI to format your repo
Go to file
Brian McGee 39bc4464b1
Some checks are pending
gh-pages / build (push) Waiting to run
gh-pages / deploy (push) Blocked by required conditions
Nix Flake Actions / nix-matrix (push) Waiting to run
Nix Flake Actions / nix-build (push) Blocked by required conditions
Merge pull request #329 from numtide/reduce-readme
README: drop unecessary meme
2024-07-02 07:52:53 +01:00
.github run deploy only on main 2024-07-02 07:43:24 +02:00
build feat: support --version 2024-02-15 13:59:56 +00:00
cache feat: demote some cache related logging to debug level 2024-05-17 09:43:38 +01:00
cli fix: only try opening the cache if cache is enabled 2024-07-01 17:40:12 +03:00
config fix: configure toml tags for config fields 2024-07-01 16:30:34 +01:00
docs README: drop unecessary meme 2024-07-02 07:08:21 +02:00
format Merge pull request #321 from numtide/softer-cancel 2024-06-15 11:28:48 +01:00
nix feat: remove CGO dependencies and disable 2024-06-30 12:25:00 +01:00
stats chore: do a golangci-lint pass 2024-06-14 12:32:57 +02:00
test feat: improve unmatched logic 2024-06-14 10:31:47 +01:00
walk chore: do a golangci-lint pass 2024-06-14 12:32:57 +02:00
.env chore: disable devshell motd 2024-05-08 08:46:10 +01:00
.envrc feat: use numtide/blueprint instead of flake.parts 2024-06-30 12:25:00 +01:00
.gitignore ci: handle releases with goreleaser 2024-05-09 22:16:57 +02:00
.goreleaser.yaml fixup! feat: configure goreleaser to mark releases as pre-release (#305) 2024-05-29 15:25:07 +01:00
default.nix feat: use numtide/blueprint instead of flake.parts 2024-06-30 12:25:00 +01:00
flake.lock feat: use numtide/blueprint instead of flake.parts 2024-06-30 12:25:00 +01:00
flake.nix feat: use numtide/blueprint instead of flake.parts 2024-06-30 12:25:00 +01:00
go.mod fix: support core.untrackedCache in git 2024-05-17 09:38:22 +01:00
go.sum fix: support core.untrackedCache in git 2024-05-17 09:38:22 +01:00
init.toml feat: implement init 2024-02-28 09:18:45 +00:00
LICENSE doc: fix copyright notice 2024-05-13 10:07:18 +01:00
main.go cli: remove global state, init function usage 2024-06-06 20:28:37 +02:00
README.md README: drop unecessary meme 2024-07-02 07:08:21 +02:00
shell.nix feat: use numtide/blueprint instead of flake.parts 2024-06-30 12:25:00 +01:00


logo
treefmt — one CLI to format your repo

Support room on Matrix

Status: beta

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

Motivation

Its common to format code according to the projects 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. Its easy to configure and fast to execute.

Treefmt Init

Its main features are:

  • Providing a unified CLI and output
    • You dont need to remember which formatters are necessary for each project.
    • Once you specify the formatters in the config file, you can trigger all of them with one command and get a standardized output.
  • Running all the formatters in parallel
    • A standard script loops over your folders and runs each formatter sequentially.
    • In contrast, treefmt runs formatters in parallel. This way, the formatting job takes less time.
  • Tracking file changes
    • When formatters are run in a script, they process all the files they encounter, regardless of whether or not they have changed.
    • treefmt tracks file changes, and only attempts to format files which have changed.

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 tools 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 its 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:

  • clang-format for C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C#
  • gofmt for Golang
  • Prettier for JavaScript/HTML/CSS

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:

  • More IDE integration
  • Pre-commit hooks
  • EditorConfig: unifies file indentations configuration on a per-project basis.
  • prettier: an opinionated code formatter for a number of languages.
  • Super-Linter: a project by GitHub to lint all of your code.
  • pre-commit: a framework for managing and maintaining multi-language pre-commit hooks.

Contributing

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

Moving from Rust To Go

You may be familiar with Version 1, which is written in Rust. So, why re-write it in Go?

Ultimately, treefmt is spending most of it's time shelling out calls to the underlying formatters. This process is just as fast/performant in Go as it is in Rust.

The remaining tasks are processing some cli args and parsing a config file. Do we really need something as heavy duty as Rust for that?

Despite all this, you can make good, sane arguments for continuing with Version 1 in Rust instead of a re-write. So here's a bad argument.

Brian wanted to improve performance by moving away from a Toml cache file, introduce pipelines for applying multiple formatters against the same file set, and add an extensible approach for how treefmt walks file systems. He knows Go much better than Rust.

zimbatm thought it was a good idea too.

So here we are 🤷.

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.