@@ -201,6 +201,45 @@ Phases provide 3 major benefits:
201201
202202See [Customizable Phase](docs/customizable_phase.md) for more info.
203203
204+ # # Scala Format
205+ A phase extension `phase_scalafmt` can format Scala source code via [Scalafmt](https://scalameta.org/scalafmt/).
206+
207+ Add this snippet to `WORKSPACE`
208+ ```
209+ load("//scala/scalafmt: scalafmt_repositories .bzl", "scalafmt_default_config", "scalafmt_repositories")
210+
211+ scalafmt_default_config()
212+
213+ scalafmt_repositories()
214+ ```
215+
216+ To add this phase to the rules, you have to pass the extension to rule macros. Take `scala_binary` for example,
217+ ```
218+ load("//scala: advanced_usage /scala.bzl", "make_scala_binary")
219+ load("//scala/scalafmt: phase_scalafmt_ext .bzl", "ext_scalafmt")
220+
221+ scalafmt_scala_binary = make_scala_binary(ext_scalafmt)
222+ ```
223+ Then use `scalafmt_scala_binary` as normal.
224+
225+ The extension adds 2 additional attributes to the rule
226+ - `format`: enable formatting
227+ - `config`: the Scalafmt configuration file
228+
229+ When `format` is set to `true`, you can do
230+ ```
231+ bazel run <TARGET >.format
232+ ```
233+ to format the source code, and do
234+ ```
235+ bazel run <TARGET >.format-test
236+ ```
237+ to check the format (without modifying source code).
238+
239+ The extension provides default configuration, but there are 2 ways to use custom configuration.
240+ - Put `.scalafmt.conf` at root of your workspace
241+ - Pass `.scalafmt.conf` in via `config` attribute
242+
204243## Building from source
205244Test & Build:
206245```
0 commit comments