Skip to content

Commit ea5e13e

Browse files
committed
Improve the fmt doc
1 parent 8b9663f commit ea5e13e

File tree

1 file changed

+99
-3
lines changed

1 file changed

+99
-3
lines changed

website/docs/commands/fmt.md

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Format
33
sidebar_position: 15
44
---
55

6+
import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7+
68
Scala CLI supports formatting your code using [Scalafmt](https://scalameta.org/scalafmt/):
79

810
```bash
@@ -23,18 +25,112 @@ scala-cli fmt --check
2325
Scala CLI also supports dialects that are passed to the formatter.
2426
This value is only used if there is no `.scalafmt.conf` file.
2527
However, if it exists, then all configuration should be placed there.
26-
For a list of all possible values, consult the [official Scala Dialects documentation](https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects):
28+
For a list of all possible values, consult
29+
the [official Scala Dialects documentation](https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects):
2730

2831
```bash
2932
scala-cli fmt --dialect scala212
3033
```
3134

3235
### Scalafmt version
3336

34-
At this time, Scala CLI reads a `scalafmt` version from `.scalafmt.conf` files. If the version is missing, Scala CLI throws an error, stating that users should declare an explicit Scalafmt version. Since Scalafmt `3.5.0`, this parameter is mandatory.
37+
At this time, Scala CLI reads a `scalafmt` version from `.scalafmt.conf` files. If the version is missing, Scala CLI
38+
throws an error, stating that users should declare an explicit Scalafmt version. Since Scalafmt `3.5.0`, this parameter
39+
is mandatory.
3540

36-
To configure the Scalafmt version, add the following to `.scalafmt.conf`. For example, to set the version to `3.5.0`, add the following line:
41+
To configure the Scalafmt version, add the following to `.scalafmt.conf`. For example, to set the version to `3.5.0`,
42+
add the following line:
3743

3844
```
3945
version = "3.5.0"
4046
```
47+
48+
### Scalafmt options
49+
50+
It is possible to pass native `scalafmt` options with the `-F` (short for `--scalafmt-arg`), for example:
51+
52+
<ChainedSnippets>
53+
54+
```bash
55+
scala-cli fmt -F --version
56+
```
57+
58+
```text
59+
scalafmt 3.5.2
60+
```
61+
62+
</ChainedSnippets>
63+
64+
For the available options please refer to `scalafmt` help, which can be viewed with the `--scalafmt-help` option (which
65+
is just an alias for `-F --help`):
66+
67+
<ChainedSnippets>
68+
69+
```bash
70+
scala-cli fmt --scalafmt-help
71+
```
72+
73+
```text
74+
scalafmt 3.5.2
75+
Usage: scalafmt [options] [<file>...]
76+
77+
-h, --help prints this usage text
78+
-v, --version print version
79+
(...)
80+
```
81+
82+
</ChainedSnippets>
83+
84+
### Excluding sources
85+
86+
Because of the way Scala CLI invokes `scalafmt` under the hood, sources are always being passed to it explicitly. This
87+
in turn means that regardless of how the sources were passed, `scalafmt` exclusion paths (the `project.excludePaths`)
88+
would be ignored. In order to prevent that from happening, the `--respect-project-filters` option is set to `true` by
89+
default.
90+
91+
```text title=.scalafmt.conf
92+
version = 3.5.2
93+
runner.dialect = scala3
94+
project {
95+
includePaths = [
96+
"glob:**.scala",
97+
"regex:.*\\.sc"
98+
]
99+
excludePaths = [
100+
"glob:**/should/not/format/**.scala"
101+
]
102+
}
103+
```
104+
105+
<ChainedSnippets>
106+
107+
```bash
108+
scala-cli fmt . --check
109+
```
110+
111+
```text
112+
All files are formatted with scalafmt :)
113+
```
114+
115+
</ChainedSnippets>
116+
117+
You can explicitly set it to false if you want to disregard any filters configured in the `project.excludePaths` setting
118+
in your `.scalafmt.conf` for any reason.
119+
120+
<ChainedSnippets>
121+
122+
```bash
123+
scala-cli fmt . --check --respect-project-filters=false
124+
```
125+
126+
```text
127+
--- a/.../should/not/format/ShouldNotFormat.scala
128+
+++ b/.../should/not/format/ShouldNotFormat.scala
129+
@@ -1,3 +1,3 @@
130+
class ShouldNotFormat {
131+
- println()
132+
+ println()
133+
}
134+
```
135+
136+
</ChainedSnippets>

0 commit comments

Comments
 (0)