Skip to content

Commit 6bfad8f

Browse files
committed
add scalafmtAll hook
1 parent aec5d0f commit 6bfad8f

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

.pre-commit-hooks.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@
3030
pass_filenames: false
3131
always_run: true
3232
minimum_pre_commit_version: '0.19.0'
33+
- id: sbt-scalafmt-apply
34+
name: scalafmt formatting fix
35+
stages: [commit,push]
36+
language: python
37+
entry: scalafmt-apply
38+
pass_filenames: false
39+
always_run: true
40+
minimum_pre_commit_version: '0.19.0'

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Currently, they include the following:
2626
- `sbt-unused-imports` - as above, but also adds the "unused imports" warning.
2727
- `sbt-scalafmt` - runs `scalafmtCheckAll`.
2828
- `sbt-wartremover` - runs the wartremover plugin.
29+
- `sbt-scalafmt-apply` - runs `scalafmtAll`.
2930

3031
To add one or more of the hooks into your repo:
3132

@@ -45,6 +46,7 @@ repos:
4546
- id: sbt-unused-imports #includes fatal warnings, arguments optional
4647
args: [--scope={defaultScope}]
4748
- id: sbt-scalafmt
49+
- id: sbt-scalafmt-apply
4850
- id: sbt-wartremover #arguments are optional
4951
args: [--warts=Warts.unsafe, --scope={defaultScope}]
5052
----
@@ -53,7 +55,7 @@ repos:
5355

5456
[NOTE]
5557
--
56-
All hooks except for `sbt-scalafmt` have the optional `scope` argument, which allows to define what source scopes
58+
All hooks except for `sbt-scalafmt` and `sbt-scalafmt-apply` have the optional `scope` argument, which allows to define what source scopes
5759
are relevant for the hook's check. The default is `{defaultScope}`.
5860
--
5961

pre_commit_hooks/scalafmt_apply.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pre_commit_hooks.runner import run_sbt_command
2+
from colorama import init as colorama_init, Fore
3+
4+
TASK_SCALAFMT = 'scalafmtAll'
5+
MISSING_PLUGIN_CHECK_STRING = 'Not a valid key: scalafmtAll'
6+
MISSING_PLUGIN_ERROR_MSG = f'{Fore.RED}ERROR: scalafmt SBT plugin not present! See {Fore.BLUE}https://scalameta.org/scalafmt/docs/installation.html#sbt{Fore.RED} for installation instructions.'
7+
8+
9+
def main(argv=None):
10+
colorama_init()
11+
12+
return run_sbt_command(f'; clean ; {TASK_SCALAFMT}', MISSING_PLUGIN_CHECK_STRING, MISSING_PLUGIN_ERROR_MSG)
13+
14+
15+
if __name__ == '__main__':
16+
exit(main())

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ console_scripts =
1313
sbt-wartremover = pre_commit_hooks.sbt_wartremover:main
1414
scalafmt = pre_commit_hooks.scalafmt:main
1515
sbt-fatal-warnings = pre_commit_hooks.sbt_fatal_warnings:main
16+
scalafmt-apply = pre_commit_hooks.scalafmt_apply:main

0 commit comments

Comments
 (0)