Skip to content

Commit 0dd4344

Browse files
authored
Merge pull request #7 from numtide/feat/improve-docs
doc: refine docs and usage
2 parents e5a6cc6 + 6ec924c commit 0dd4344

14 files changed

+414
-334
lines changed

.github/workflows/release.yml

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
name: goreleaser
22

33
on:
4-
pull_request:
5-
push:
6-
# run only against tags
7-
tags:
8-
- "v*"
4+
push:
5+
# run only against tags
6+
tags:
7+
- "v*"
98

109
permissions:
11-
contents: write
10+
contents: write
1211

1312
jobs:
14-
goreleaser:
15-
runs-on: ubuntu-latest
16-
steps:
17-
- name: Checkout
18-
uses: actions/checkout@v4
19-
with:
20-
fetch-depth: 0
21-
- name: Setup Go
22-
uses: actions/setup-go@v4
23-
with:
24-
go-version: 1.22.x
25-
- name: Run goreleaser
26-
uses: goreleaser/goreleaser-action@v4
27-
with:
28-
args: release --clean
29-
env:
30-
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
13+
goreleaser:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
- name: Setup Go
21+
uses: actions/setup-go@v4
22+
with:
23+
go-version: 1.22.x
24+
- name: Run goreleaser
25+
uses: goreleaser/goreleaser-action@v4
26+
with:
27+
args: release --clean
28+
env:
29+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

.goreleaser.yaml

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ version: 1
1111
project_name: treefmt
1212

1313
before:
14-
hooks:
15-
- go mod tidy
14+
hooks:
15+
- go mod tidy
1616

1717
builds:
18-
- env:
19-
- CGO_ENABLED=0
20-
ldflags:
21-
- -s -w -X git.numtide.com/numtide/treefmt/build.Version=v{{.Version}}
22-
goos:
23-
- linux
24-
- darwin
18+
- env:
19+
- CGO_ENABLED=0
20+
ldflags:
21+
- -s -w -X git.numtide.com/numtide/treefmt/build.Version=v{{.Version}}
22+
goos:
23+
- linux
24+
- darwin
2525

2626
changelog:
27-
sort: asc
28-
use: github
29-
filters:
30-
exclude:
31-
- "^chore:"
32-
- "^docs:"
33-
- "^test:"
27+
sort: asc
28+
use: github
29+
filters:
30+
exclude:
31+
- "^chore:"
32+
- "^docs:"
33+
- "^test:"

cli/cli.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import (
99
var Cli = Format{}
1010

1111
type Format struct {
12-
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
13-
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
14-
NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"`
15-
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
16-
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
12+
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing."`
13+
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory."`
14+
NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI."`
15+
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough."`
16+
ConfigFile string `type:"existingfile" default:"./treefmt.toml" help:"The config file to use."`
1717
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`
1818
Formatters []string `help:"Specify formatters to apply. Defaults to all formatters."`
19-
TreeRoot string `type:"existingdir" default:"."`
19+
TreeRoot string `type:"existingdir" default:"." help:"The root directory from which treefmt will start walking the filesystem."`
2020
Walk walk.Type `enum:"auto,git,filesystem" default:"auto" help:"The method used to traverse the files within --tree-root. Currently supports 'auto', 'git' or 'filesystem'."`
2121
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
22-
Version bool `name:"version" short:"V" help:"Print version"`
23-
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml"`
22+
Version bool `name:"version" short:"V" help:"Print version."`
23+
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml."`
2424

2525
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
26-
Stdin bool `help:"Format the context passed in via stdin"`
26+
Stdin bool `help:"Format the context passed in via stdin."`
2727

28-
CpuProfile string `optional:"" help:"file into which a cpu profile will be written"`
28+
CpuProfile string `optional:"" help:"The file into which a cpu profile will be written."`
2929
}
3030

3131
func ConfigureLogging() {

config/formatter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package config
22

33
type Formatter struct {
4-
// Command is the command invoke when applying this Formatter.
4+
// Command is the command to invoke when applying this Formatter.
55
Command string
66
// Options are an optional list of args to be passed to Command.
77
Options []string

docs/.vitepress/config.mts

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ export default defineConfig({
2323

2424
sidebar: [
2525
{ text: 'Quick Start', link: '/quick-start' },
26-
{ text: 'Installation', link: '/installation' },
27-
{ text: 'Overview', link: '/overview' },
28-
{ text: 'Usage', link: '/usage' },
29-
{ text: 'Formatter Specification', link: '/formatter-spec' },
26+
{ text: 'Install Treefmt', link: '/install' },
27+
{ text: 'Configure Treefmt', link: '/configure' },
28+
{ text: 'Run Treefmt', link: '/usage' },
29+
{ text: 'Motivation', link: '/about' },
30+
{ text: 'Formatter Spec', link: '/formatter-spec' },
3031
{ text: 'Contributing', link: '/contributing' },
3132
{ text: 'FAQ', link: '/faq' },
3233
],

docs/overview.md docs/about.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The tool can be invoked manually or integrated into your CI. There's currently n
6969
- **IDE integration:** Most of developers are used to formatting a file upon save in the IDE. So far, you can't use `treefmt` for this purpose, but we're working on it 😀
7070
- **Pre-commit hook:** It's good to have your code checked for adherence to the project's standards before commit. `treefmt` pre-commit hook won't let you commit if you have formatting issues.
7171

72-
As a next step, learn how to [install] and [use] `treefmt`.
72+
As a next step, learn how to [install] and [run] `treefmt`.
7373

74-
[install]: installation.md
75-
[use]: usage.md
74+
[install]: install
75+
[run]: usage

docs/configure.md

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# Configure Treefmt
6+
7+
The `treefmt.toml` configuration file consists of a mixture of global options and formatter sections:
8+
9+
```toml
10+
excludes = ["*.md", "*.dat"]
11+
12+
[formatter.elm]
13+
command = "elm-format"
14+
options = ["--yes"]
15+
includes = ["*.elm"]
16+
17+
[formatter.go]
18+
command = "gofmt"
19+
options = ["-w"]
20+
includes = ["*.go"]
21+
22+
[formatter.python]
23+
command = "black"
24+
includes = ["*.py"]
25+
26+
# run shellcheck first
27+
[formatter.shellcheck]
28+
command = "shellcheck"
29+
includes = ["*.sh"]
30+
pipeline = "sh"
31+
priority = 0
32+
33+
# shfmt second
34+
[formatter.shfmt]
35+
command = "shfmt"
36+
options = ["-s", "-w"]
37+
includes = ["*.sh"]
38+
pipeline = "sh"
39+
priority = 1
40+
```
41+
42+
## Global Options
43+
44+
- `excludes` - an optional list of glob patters used to exclude certain files from all formatters.
45+
46+
## Formatter Options
47+
48+
- `command` - the command to invoke when applying the formatter.
49+
- `options` - an optional list of args to be passed to `command`.
50+
- `includes` - a list of glob patterns used to determine whether the formatter should be applied against a given path.
51+
- `excludes` - an optional list of glob patterns used to exclude certain files from this formatter.
52+
- `pipeline` - an optional key used to group related formatters together, ensuring they are executed sequentially
53+
against a given path.
54+
- `priority` - indicates the order of execution when this formatter is operating as part of a pipeline. Greater
55+
precedence is given to lower numbers, with the default being `0`.
56+
57+
> When two or more formatters in a pipeline have the same priority, they are executed in lexicographical order to
58+
> ensure deterministic behaviour over multiple executions.
59+
60+
## Supported Formatters
61+
62+
Here is a list of all the formatters we tested. Feel free to send a PR to add other ones!
63+
64+
### [prettier](https://prettier.io/)
65+
66+
An opinionated code formatter that supports many languages.
67+
68+
```toml
69+
command = "prettier"
70+
options = ["--write"]
71+
includes = [
72+
"*.css",
73+
"*.html",
74+
"*.js",
75+
"*.json",
76+
"*.jsx",
77+
"*.md",
78+
"*.mdx",
79+
"*.scss",
80+
"*.ts",
81+
"*.yaml",
82+
]
83+
```
84+
85+
### [Black](https://github.com/psf/black)
86+
87+
A python formatter.
88+
89+
```toml
90+
command = "black"
91+
includes = ["*.py"]
92+
```
93+
94+
### [clang-format](https://clang.llvm.org/docs/ClangFormat.html)
95+
96+
A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
97+
98+
```toml
99+
command = "clang-format"
100+
options = [ "-i" ]
101+
includes = [ "*.c", "*.cpp", "*.cc", "*.h", "*.hpp" ]
102+
```
103+
104+
Note: This example focuses on C/C++ but can be modified to use with other languages.
105+
106+
### Elm
107+
108+
```toml
109+
command = "elm-format"
110+
options = ["--yes"]
111+
includes = ["*.elm"]
112+
```
113+
114+
### Go
115+
116+
```toml
117+
command = "gofmt"
118+
options = ["-w"]
119+
includes = ["*.go"]
120+
```
121+
122+
### [Ormolu](https://github.com/tweag/ormolu)
123+
124+
Haskell formatter. Make sure to use ormolu 0.1.4.0+ as older versions don't
125+
adhere to the spec.
126+
127+
```toml
128+
command = "ormolu"
129+
options = [
130+
"--ghc-opt", "-XBangPatterns",
131+
"--ghc-opt", "-XPatternSynonyms",
132+
"--ghc-opt", "-XTypeApplications",
133+
"--mode", "inplace",
134+
"--check-idempotence",
135+
]
136+
includes = ["*.hs"]
137+
```
138+
139+
### [stylish-haskell](https://github.com/jaspervdj/stylish-haskell)
140+
141+
Another Haskell formatter.
142+
143+
```toml
144+
command = "stylish-haskell"
145+
options = [ "--inplace" ]
146+
includes = [ "*.hs" ]
147+
```
148+
149+
### [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt)
150+
151+
Nix code formatter.
152+
153+
```toml
154+
command = "nixpkgs-fmt"
155+
includes = ["*.nix"]
156+
```
157+
158+
### rustfmt
159+
160+
```toml
161+
command = "rustfmt"
162+
options = ["--edition", "2018"]
163+
includes = ["*.rs"]
164+
```
165+
166+
### [rufo](https://github.com/ruby-formatter/rufo)
167+
168+
Rufo is an opinionated ruby formatter. By default it exits with status 3 on
169+
file change so we have to pass the `-x` option.
170+
171+
```toml
172+
command = "rufo"
173+
options = ["-x"]
174+
includes = ["*.rb"]
175+
```
176+
177+
### cargo fmt
178+
179+
`cargo fmt` is not supported as it doesn't follow the spec. It doesn't allow
180+
to pass arbitrary files to be formatted, which treefmt relies on. Use `rustfmt`
181+
instead (which is what `cargo fmt` uses under the hood).
182+
183+
### [shfmt](https://github.com/mvdan/sh)
184+
185+
A shell code formatter.
186+
187+
```toml
188+
command = "shfmt"
189+
options = [
190+
"-i",
191+
"2", # indent 2
192+
"-s", # simplify the code
193+
"-w", # write back to the file
194+
]
195+
includes = ["*.sh"]
196+
```
197+
198+
### terraform
199+
200+
terraform fmt only supports formatting one file at the time. See
201+
https://github.com/hashicorp/terraform/pull/28191

0 commit comments

Comments
 (0)