You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/commands/fmt.md
+99-3Lines changed: 99 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: Format
3
3
sidebar_position: 15
4
4
---
5
5
6
+
import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7
+
6
8
Scala CLI supports formatting your code using [Scalafmt](https://scalameta.org/scalafmt/):
7
9
8
10
```bash
@@ -23,18 +25,112 @@ scala-cli fmt --check
23
25
Scala CLI also supports dialects that are passed to the formatter.
24
26
This value is only used if there is no `.scalafmt.conf` file.
25
27
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):
27
30
28
31
```bash
29
32
scala-cli fmt --dialect scala212
30
33
```
31
34
32
35
### Scalafmt version
33
36
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.
35
40
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:
37
43
38
44
```
39
45
version = "3.5.0"
40
46
```
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
0 commit comments